#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = double;

#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace

ld const pi = acos(-1.0);
ld const eps = 0.000000000001;
int const inf = int(1e9 + 7);

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

template<class T> bool mi(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool ma(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int ran(){
    return (rand() << 15) | rand();
}

struct decartach {
    struct node {
        int x, y, sz;
        node *l, *r;

        node() {
            l = r = nullptr;
            sz = 0;
        }

        node(int what) {
            x = what;
            y = ran();
            l = r = nullptr;
            sz = 1;
        }
    };

    int getsz(node *r) {
        if (!r)return 0;
        return r->sz;
    }

    void upd(node *r) {
        if (!r) {
            return;
        }
        r->sz = 1 + getsz(r->l) + getsz(r->r);
    }

    void split(node *root, node *&l, node *&r, int x) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (root->x <= x) {
            split(root->r, root->r, r, x);
            upd(l);
            l = root;
        } else {
            split(root->l, l, root->l, x);
            upd(r);
            r = root;
        }
    }

    void getfirstk(node *root, node *&l, node *&r, int k) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (k == 0) {
            l = nullptr;
            r = root;
            return;
        }
        if (getsz(root->l) >= k) {
            node *p1, *p2;
            getfirstk(root->l, p1, p2, k);
            root->l = p2;
            upd(root);
            l = p1;
            r = root;
        } else {
            node *p1, *p2;
            getfirstk(root->r, p1, p2, k - getsz(root->l) - 1);
            root->r = p1;
            upd(root);
            l = root;
            r = p2;
        }
    }

    void merge(node *&root, node *l, node *r) {
        if (!l) {
            root = r;
            return;
        }
        if (!r) {
            root = l;
            return;
        }
        if (l->y > r->y) {
            merge(l->r, l->r, r);
            upd(l);
            root = l;
        } else {
            merge(r->l, l, r->l);
            upd(r);
            root = r;
        }

    }

    void print(node *r) {
        if (!r)return;
        print(r->l);
        printf("%d ", r->x);
        print(r->r);
    }

};

int n,m;
struct edge{
    int a, b;
    int w;
    int id;
};
edge e[200005];
vector<pair<int,int> > t[200005];
int id[200005];
ll re[200005];
int p[200005][30];
int h[200005];
ll heavy[200005][30];

int get(int ind){
    if(id[ind] == ind)return ind;
    return id[ind] = get(id[ind]);
}

void con(int a,int b){
    a = get(a);
    b = get(b);
    id[b] = a;
}

ll getfat(int a, int b){
    ll re = LLONG_MIN;
    if(h[a] > h[b])swap(a,b);
    for(int j = 29; j>= 0; j--){
        if(((h[b] - h[a])&(1 << j)) > 0){
            ma(re, heavy[b][j]);
            b = p[b][j];
            if(h[b] - h[a]<0)printf("!%d %d %d %d!S", a, b, h[a], h[b]);

        }
    }

    if(a == b)return re;

    for(int j = 29; j >= 0; j--){
        if(p[a][j] != p[b][j]){
            ma(re, heavy[a][j]);
            ma(re, heavy[b][j]);
            a = p[a][j];
            b = p[b][j];
        }
    }

    ma(re, heavy[a][0]);
    ma(re, heavy[b][0]);
    return re;
}

void dfs0(int ind, int s){
    h[ind] = s;
    for(auto &i: t[ind]){
        if(h[i.first] == 0) {
            dfs0(i.first, s + 1);
            p[i.first][0] = ind;
            heavy[i.first][0] = i.second;
        }
    }
}

void prec(){
    for(int i = 0 ;i < 200005; i++)for(int j = 0 ;j <30; j++)p[i][j] = 0, heavy[i][j] = 0;
    dfs0(0, 1);
    for(int i = 1; i<n;i++){
        for(int j = 1; j< 30; j++){
            p[i][j] = p[p[i][j-1]][j-1];
            ma(heavy[i][j], max(heavy[i][j-1], heavy[p[i][j-1]][j-1]));
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("chocolate.in", "r", stdin);
    //freopen("chocolate.out", "w", stdout);
#endif

    scanf("%d %d", &n, &m);
    for(int i = 0; i <n; i++)id[i] = i;
    for(int i= 0; i <m ;i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--;
        b--;
        e[i].a=a;
        e[i].b=b;
        e[i].w=c;
        e[i].id=i;
    }
    sort(e, e + m, [&](edge &l, edge &r){return l.w < r.w;});
    ll yy = 0;
    for(int i = 0; i < m; i++){
        if(e[i].a == e[i].b)throw(1);
        if(get(e[i].a) == get(e[i].b))continue;
        con(e[i].a, e[i].b);
        yy += e[i].w;
        t[e[i].a].pb({e[i].b, e[i].w});
        t[e[i].b].pb({e[i].a, e[i].w});
    }
    for(auto &i: h)i = 0;
    int last = id[0];
    for(int i =1 ;i <n; i++)if(id[get(i)] != last)throw(1);
    prec();
    for(int i = 0; i <m; i++) {
        re[e[i].id] = yy - getfat(e[i].a, e[i].b) + e[i].w;
    }
    for(int i = 0 ;i < m;i++)printf("%lld\n", re[i]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = double;

#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace

ld const pi = acos(-1.0);
ld const eps = 0.000000000001;
int const inf = int(1e9 + 7);

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

template<class T> bool mi(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool ma(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int ran(){
    return (rand() << 15) | rand();
}

struct decartach {
    struct node {
        int x, y, sz;
        node *l, *r;

        node() {
            l = r = nullptr;
            sz = 0;
        }

        node(int what) {
            x = what;
            y = ran();
            l = r = nullptr;
            sz = 1;
        }
    };

    int getsz(node *r) {
        if (!r)return 0;
        return r->sz;
    }

    void upd(node *r) {
        if (!r) {
            return;
        }
        r->sz = 1 + getsz(r->l) + getsz(r->r);
    }

    void split(node *root, node *&l, node *&r, int x) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (root->x <= x) {
            split(root->r, root->r, r, x);
            upd(l);
            l = root;
        } else {
            split(root->l, l, root->l, x);
            upd(r);
            r = root;
        }
    }

    void getfirstk(node *root, node *&l, node *&r, int k) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (k == 0) {
            l = nullptr;
            r = root;
            return;
        }
        if (getsz(root->l) >= k) {
            node *p1, *p2;
            getfirstk(root->l, p1, p2, k);
            root->l = p2;
            upd(root);
            l = p1;
            r = root;
        } else {
            node *p1, *p2;
            getfirstk(root->r, p1, p2, k - getsz(root->l) - 1);
            root->r = p1;
            upd(root);
            l = root;
            r = p2;
        }
    }

    void merge(node *&root, node *l, node *r) {
        if (!l) {
            root = r;
            return;
        }
        if (!r) {
            root = l;
            return;
        }
        if (l->y > r->y) {
            merge(l->r, l->r, r);
            upd(l);
            root = l;
        } else {
            merge(r->l, l, r->l);
            upd(r);
            root = r;
        }

    }

    void print(node *r) {
        if (!r)return;
        print(r->l);
        printf("%d ", r->x);
        print(r->r);
    }

};

int n,m;
struct edge{
    int a, b;
    int w;
    int id;
};
edge e[200005];
vector<pair<int,int> > t[200005];
int id[200005];
ll re[200005];
int p[200005][30];
int h[200005];
ll heavy[200005][30];

int get(int ind){
    if(id[ind] == ind)return ind;
    return id[ind] = get(id[ind]);
}

void con(int a,int b){
    a = get(a);
    b = get(b);
    id[b] = a;
}

ll getfat(int a, int b){
    ll re = LLONG_MIN;
    if(h[a] > h[b])swap(a,b);
    for(int j = 29; j>= 0; j--){
        if((h[b] - h[a]) &(1 << j)){
            ma(re, heavy[b][j]);
            b = p[b][j];
            if(h[b] - h[a]<0)printf("!%d %d %d %d!S", a, b, h[a], h[b]);

        }
    }

    if(a == b)return re;

    for(int j = 29; j >= 0; j--){
        if(p[a][j] != p[b][j]){
            ma(re, heavy[a][j]);
            ma(re, heavy[b][j]);
            a = p[a][j];
            b = p[b][j];
        }
    }

    ma(re, heavy[a][0]);
    ma(re, heavy[b][0]);
    return re;
}

void dfs0(int ind, int s){
    h[ind] = s;
    for(auto &i: t[ind]){
        if(h[i.first] == 0) {
            dfs0(i.first, s + 1);
            p[i.first][0] = ind;
            heavy[i.first][0] = i.second;
        }
    }
}

void prec(){
    for(int i = 0 ;i < 200005; i++)for(int j = 0 ;j <30; j++)p[i][j] = 0, heavy[i][j] = 0;
    dfs0(0, 1);
    for(int i = 1; i<n;i++){
        for(int j = 1; j< 30; j++){
            p[i][j] = p[p[i][j-1]][j-1];
            ma(heavy[i][j], max(heavy[i][j-1], heavy[p[i][j-1]][j-1]));
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("chocolate.in", "r", stdin);
    //freopen("chocolate.out", "w", stdout);
#endif

    scanf("%d %d", &n, &m);
    for(int i = 0; i <n; i++)id[i] = i;
    for(int i= 0; i <m ;i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--;
        b--;
        e[i].a=a;
        e[i].b=b;
        e[i].w=c;
        e[i].id=i;
    }
    sort(e, e + m, [&](edge &l, edge &r){return l.w < r.w;});
    ll yy = 0;
    for(int i = 0; i < m; i++){
        if(e[i].a == e[i].b)throw(1);
        if(get(e[i].a) == get(e[i].b))continue;
        con(e[i].a, e[i].b);
        yy += e[i].w;
        t[e[i].a].eb(e[i].b, e[i].w);
        t[e[i].b].eb(e[i].a, e[i].w);
    }
    for(auto &i: h)i = 0;
    prec();
    for(int i = 0; i <m; i++) {
        re[e[i].id] = yy - getfat(e[i].a, e[i].b) + e[i].w;
    }
    for(int i = 0 ;i < m;i++)printf("%lld\n", re[i]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = double;

#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace

ld const pi = acos(-1.0);
ld const eps = 0.000000000001;
int const inf = int(1e9 + 7);

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

template<class T> bool mi(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool ma(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int ran(){
    return (rand() << 15) | rand();
}

struct decartach {
    struct node {
        int x, y, sz;
        node *l, *r;

        node() {
            l = r = nullptr;
            sz = 0;
        }

        node(int what) {
            x = what;
            y = ran();
            l = r = nullptr;
            sz = 1;
        }
    };

    int getsz(node *r) {
        if (!r)return 0;
        return r->sz;
    }

    void upd(node *r) {
        if (!r) {
            return;
        }
        r->sz = 1 + getsz(r->l) + getsz(r->r);
    }

    void split(node *root, node *&l, node *&r, int x) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (root->x <= x) {
            split(root->r, root->r, r, x);
            upd(l);
            l = root;
        } else {
            split(root->l, l, root->l, x);
            upd(r);
            r = root;
        }
    }

    void getfirstk(node *root, node *&l, node *&r, int k) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (k == 0) {
            l = nullptr;
            r = root;
            return;
        }
        if (getsz(root->l) >= k) {
            node *p1, *p2;
            getfirstk(root->l, p1, p2, k);
            root->l = p2;
            upd(root);
            l = p1;
            r = root;
        } else {
            node *p1, *p2;
            getfirstk(root->r, p1, p2, k - getsz(root->l) - 1);
            root->r = p1;
            upd(root);
            l = root;
            r = p2;
        }
    }

    void merge(node *&root, node *l, node *r) {
        if (!l) {
            root = r;
            return;
        }
        if (!r) {
            root = l;
            return;
        }
        if (l->y > r->y) {
            merge(l->r, l->r, r);
            upd(l);
            root = l;
        } else {
            merge(r->l, l, r->l);
            upd(r);
            root = r;
        }

    }

    void print(node *r) {
        if (!r)return;
        print(r->l);
        printf("%d ", r->x);
        print(r->r);
    }

};

int n,m;
struct edge{
    int a, b;
    int w;
    int id;
};
edge e[200005];
vector<pair<int,int> > t[200005];
int id[200005];
ll re[200005];
int p[200005][30];
int h[200005];
ll heavy[200005][30];

int get(int ind){
    if(id[ind] == ind)return ind;
    return id[ind] = get(id[ind]);
}

void con(int a,int b){
    a = get(a);
    b = get(b);
    id[b] = a;
}

ll getfat(int a, int b){
    ll re = LLONG_MIN;
    if(h[a] > h[b])swap(a,b);
    for(int j = 29; j>= 0; j--){
        if((h[b] - h[a])&(1 << j) > 0){
            ma(re, heavy[b][j]);
            b = p[b][j];
            if(h[b] - h[a]<0)printf("!%d %d %d %d!S", a, b, h[a], h[b]);

        }
    }

    if(a == b)return re;

    for(int j = 29; j >= 0; j--){
        if(p[a][j] != p[b][j]){
            ma(re, heavy[a][j]);
            ma(re, heavy[b][j]);
            a = p[a][j];
            b = p[b][j];
        }
    }

    ma(re, heavy[a][0]);
    ma(re, heavy[b][0]);
    return re;
}

void dfs0(int ind, int s){
    h[ind] = s;
    for(auto &i: t[ind]){
        if(h[i.first] == 0) {
            dfs0(i.first, s + 1);
            p[i.first][0] = ind;
            heavy[i.first][0] = i.second;
        }
    }
}

void prec(){
    for(int i = 0 ;i < 200005; i++)for(int j = 0 ;j <30; j++)p[i][j] = 0, heavy[i][j] = 0;
    dfs0(0, 1);
    for(int i = 1; i<n;i++){
        for(int j = 1; j< 30; j++){
            p[i][j] = p[p[i][j-1]][j-1];
            ma(heavy[i][j], max(heavy[i][j-1], heavy[p[i][j-1]][j-1]));
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("chocolate.in", "r", stdin);
    //freopen("chocolate.out", "w", stdout);
#endif

    scanf("%d %d", &n, &m);
    for(int i = 0; i <n; i++)id[i] = i;
    for(int i= 0; i <m ;i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--;
        b--;
        e[i].a=a;
        e[i].b=b;
        e[i].w=c;
        e[i].id=i;
    }
    sort(e, e + m, [&](edge &l, edge &r){return l.w < r.w;});
    ll yy = 0;
    for(int i = 0; i < m; i++){
        if(e[i].a == e[i].b)throw(1);
        if(get(e[i].a) == get(e[i].b))continue;
        con(e[i].a, e[i].b);
        yy += e[i].w;
        t[e[i].a].eb(e[i].b, e[i].w);
        t[e[i].b].eb(e[i].a, e[i].w);
    }
    for(auto &i: h)i = 0;
    prec();
    for(int i = 0; i <m; i++) {
        re[e[i].id] = yy - getfat(e[i].a, e[i].b) + e[i].w;
    }
    for(int i = 0 ;i < m;i++)printf("%lld\n", re[i]);
    return 0;
}


#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using ld = double;

#define all(a) a.begin(), a.end()
#define pii pair<int, int>
#define len(a) (int)a.size()
#define pb push_back
#define eb emplace_back
#define em emplace

ld const pi = acos(-1.0);
ld const eps = 0.000000000001;
int const inf = int(1e9 + 7);

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}
int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

template<class T> bool mi(T &a, T b) { return a > b ? (a = b, true) : false; }
template<class T> bool ma(T &a, T b) { return a < b ? (a = b, true) : false; }

inline int ran(){
    return (rand() << 15) | rand();
}

struct decartach {
    struct node {
        int x, y, sz;
        node *l, *r;

        node() {
            l = r = nullptr;
            sz = 0;
        }

        node(int what) {
            x = what;
            y = ran();
            l = r = nullptr;
            sz = 1;
        }
    };

    int getsz(node *r) {
        if (!r)return 0;
        return r->sz;
    }

    void upd(node *r) {
        if (!r) {
            return;
        }
        r->sz = 1 + getsz(r->l) + getsz(r->r);
    }

    void split(node *root, node *&l, node *&r, int x) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (root->x <= x) {
            split(root->r, root->r, r, x);
            upd(l);
            l = root;
        } else {
            split(root->l, l, root->l, x);
            upd(r);
            r = root;
        }
    }

    void getfirstk(node *root, node *&l, node *&r, int k) {
        if (!root) {
            l = r = nullptr;
            return;
        }
        if (k == 0) {
            l = nullptr;
            r = root;
            return;
        }
        if (getsz(root->l) >= k) {
            node *p1, *p2;
            getfirstk(root->l, p1, p2, k);
            root->l = p2;
            upd(root);
            l = p1;
            r = root;
        } else {
            node *p1, *p2;
            getfirstk(root->r, p1, p2, k - getsz(root->l) - 1);
            root->r = p1;
            upd(root);
            l = root;
            r = p2;
        }
    }

    void merge(node *&root, node *l, node *r) {
        if (!l) {
            root = r;
            return;
        }
        if (!r) {
            root = l;
            return;
        }
        if (l->y > r->y) {
            merge(l->r, l->r, r);
            upd(l);
            root = l;
        } else {
            merge(r->l, l, r->l);
            upd(r);
            root = r;
        }

    }

    void print(node *r) {
        if (!r)return;
        print(r->l);
        printf("%d ", r->x);
        print(r->r);
    }

};

int n,m;
struct edge{
    int a, b;
    int w;
    int id;
};
edge e[200005];
vector<pair<int,int> > t[200005];
int id[200005];
ll re[200005];
int p[200005][30];
int h[200005];
ll heavy[200005][30];

int get(int ind){
    if(id[ind] == ind)return ind;
    return id[ind] = get(id[ind]);
}

void con(int a,int b){
    a = get(a);
    b = get(b);
    id[b] = a;
}

ll getfat(int a, int b){
    ll re = LLONG_MIN;
    if(h[a] > h[b])swap(a,b);
    for(int j = 29; j>= 0; j--){
        if(((h[b] - h[a])&(1 << j)) > 0){
            ma(re, heavy[b][j]);
            b = p[b][j];
            if(h[b] - h[a]<0)printf("!%d %d %d %d!S", a, b, h[a], h[b]);

        }
    }

    if(a == b)return re;

    for(int j = 29; j >= 0; j--){
        if(p[a][j] != p[b][j]){
            ma(re, heavy[a][j]);
            ma(re, heavy[b][j]);
            a = p[a][j];
            b = p[b][j];
        }
    }

    ma(re, heavy[a][0]);
    ma(re, heavy[b][0]);
    return re;
}

void dfs0(int ind, int s){
    h[ind] = s;
    for(auto &i: t[ind]){
        if(h[i.first] == 0) {
            dfs0(i.first, s + 1);
            p[i.first][0] = ind;
            heavy[i.first][0] = i.second;
        }
    }
}

void prec(){
    for(int i = 0 ;i < 200005; i++)for(int j = 0 ;j <30; j++)p[i][j] = 0, heavy[i][j] = 0;
    dfs0(0, 1);
    for(int i = 1; i<n;i++){
        for(int j = 1; j< 30; j++){
            p[i][j] = p[p[i][j-1]][j-1];
            ma(heavy[i][j], max(heavy[i][j-1], heavy[p[i][j-1]][j-1]));
        }
    }
}

int main() {
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
    //freopen("chocolate.in", "r", stdin);
    //freopen("chocolate.out", "w", stdout);
#endif

    scanf("%d %d", &n, &m);
    for(int i = 0; i <n; i++)id[i] = i;
    for(int i= 0; i <m ;i++){
        int a, b, c;
        scanf("%d %d %d", &a, &b, &c);
        a--;
        b--;
        e[i].a=a;
        e[i].b=b;
        e[i].w=c;
        e[i].id=i;
    }
    sort(e, e + m, [&](edge &l, edge &r){return l.w < r.w;});
    ll yy = 0;
    for(int i = 0; i < m; i++){
        if(e[i].a == e[i].b)throw(1);
        if(get(e[i].a) == get(e[i].b))continue;
        con(e[i].a, e[i].b);
        yy += e[i].w;
        t[e[i].a].pb({e[i].b, e[i].w});
        t[e[i].b].pb({e[i].a, e[i].w});
    }
    for(auto &i: h)i = 0;
    prec();
    for(int i = 0; i <m; i++) {
        re[e[i].id] = yy - getfat(e[i].a, e[i].b) + e[i].w;
    }
    for(int i = 0 ;i < m;i++)printf("%lld\n", re[i]);
    return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF (1e9)
#define in cin >>
#define LINF (1e18)
#define out cout <<
#define ln  << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define sint short int
#define ull unsigned ll
#define uint unsigned int
#define B out "BreakPoint\n";
#define read(_num) scanf("%d",&_num)
#define ms(_data) memset(_data,0,sizeof(_data))
#define fin(_filename) freopen(_filename,"r",stdin)
#define fout(_filename) freopen(_filename,"w",stdout)
#define read2(_num1,_num2) scanf("%d %d",&_num1,&_num2)
#define msn(_data,_num) memset(_data,_num,sizeof(_data))
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define read3(_num1,_num2,_num3) scanf("%d %d %d",&_num1,&_num2,&_num3)
using namespace std;
#define MAXN 200100
#define MAXLOG 10

int n , m;
int fro[MAXN] , too[MAXN];
ll cot[MAXN];
struct Edge{
	int from , to , id;
	ll cost;
	Edge( int fromm , int too , ll costt , int iid ){
		from = fromm;
		to = too;
		cost = costt;
		id = iid;
	}
};
bool operator<( Edge e1 , Edge e2 ){
	return e1.cost < e2.cost;
}
vector<Edge> g;

int dsu[MAXN];
void init(){
	srand( time(0) );
	for( int i = 1 ; i <= n ; i++ )
		dsu[i] = i;
}
int getfa( int pos ){
	if( dsu[pos] == pos ) return pos;
	return dsu[pos] = getfa( dsu[pos] );
}
bool unite( int a , int b ){
	//cout << a << " " << b << "    ";
	a = getfa(a);
	b = getfa(b);
	//cout << a << " " << b << endl;
	if( a == b ) return 0;
	dsu[a] = b;
	return 1;
}

ll mintreecost = 0;
vector<Edge> e[MAXN];
bool intree[MAXN];

void mintree(){
	sort( g.begin() , g.end() );
	ms( intree );
	init();
	int cnt = 0 , pos = 0;
	while( cnt < n-1 ){
		//if( pos >= m ) return;
		Edge now = g[pos];
		if( unite( now.from , now.to) ){
			cout << "[mintree] Choose Edge " << now.id << endl;
			mintreecost += now.cost;
			cnt++;
			e[now.from].pb( Edge( 0 , now.to , now.cost , now.id ) );
			e[now.to].pb( Edge( 0 , now.from , now.cost , now.id ) );
			intree[now.id] = 1;
		}
		pos++;
	}
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << cycid[i] << " ";
	//cout << endl;
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << maxedge[i] << " ";
	//cout << endl;
	//cout << mintreecost << endl;
}

int par[MAXLOG][MAXN];
int maxedge[MAXLOG][MAXN];	//maxedge[i][v]=x   MAXEDGE in [v~parent[i][v]]
int deep[MAXN];

void dfs( int pos , int fa , int d , int lastcost ){
	deep[pos] = d;
	par[0][pos] = fa;
	maxedge[0][pos] = lastcost;
	for( uint i = 0 ; i < e[pos].size() ; i++ )
		if( e[pos][i].to != fa )
			dfs( e[pos][i].to , pos , d+1 , e[pos][i].cost );
}
void lca_init(){
	dfs( 1 , -1 , 0 , -1 );
	for( int i = 0 ; i+1 < MAXLOG ; i++ )
		for( int j = 1 ; j <= n ; j++ ){
			if( par[i][j] < 0 ){
				par[i+1][j] = -1;
				maxedge[i+1][j] = maxedge[i][j];
			}else{
				par[i+1][j] = par[i][par[i][j]];
				maxedge[i+1][j] = max(  maxedge[i][j] , maxedge[i][par[i][j]]  );
			}
		}
	//for( int i = 1;  i <= n ; i++ ){
	//	int x = rand() % 3;
	//	cout << i << "  go up  2^" << x << "  =>  " << par[x][i] << endl;
	//}
}
int getmaxdis( int a , int b ){
	int ret = -1;
	if( deep[a] < deep[b] ) swap( a , b );
	int delta = deep[a] - deep[b];
	//cout << bitset<3>(delta) << endl;
	for( int i = 0 ; i < MAXLOG ; i++ ){
		if( delta == 0 ) break;
		if( delta & (1<<i) ){
			ret = max( ret , maxedge[i][a] );
			a = par[i][a];
		}
	}
	//cout << a << " " << b << endl;
	for( int i = MAXLOG ; i >= 0 ; i-- )
		if( par[i][a] != par[i][b] ){
			ret = max( ret , maxedge[i][a] );
			ret = max( ret , maxedge[i][b] );
			a = par[i][a];
			b = par[i][b];
		}
	ret = max( ret , maxedge[0][a] );
	ret = max( ret , maxedge[0][b] );
	//cout << ret << endl;
	return ret;
}

void solve(){
	//cout << "[ANS]\n";
	for( int i = 1 ; i <= m ; i++ ){
		if( intree[i] ){
			printf("%I64d\n",mintreecost);
			continue;
		}
		printf("%I64d\n", mintreecost - getmaxdis( fro[i] , too[i] ) + cot[i] );
	}
}

int main(){
//	fin("minimum.txt");

	read2( n , m );
	for( int i = 1 ; i <= m ; i++ ){
		int ff , tt;
		ll cc;
		read2( ff , tt );
		scanf("%I64d",&cc);
		g.pb( Edge( ff , tt , cc , i ) );
		fro[i] = ff;
		too[i] = tt;
		cot[i] = cc;
	}

	mintree();
	lca_init();
	//cout << getmaxdis( 2 , 5 ) << endl;
	solve();

	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF (1e9)
#define in cin >>
#define LINF (1e18)
#define out cout <<
#define ln  << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define sint short int
#define ull unsigned ll
#define uint unsigned int
#define B out "BreakPoint\n";
#define read(_num) scanf("%d",&_num)
#define ms(_data) memset(_data,0,sizeof(_data))
#define fin(_filename) freopen(_filename,"r",stdin)
#define fout(_filename) freopen(_filename,"w",stdout)
#define read2(_num1,_num2) scanf("%d %d",&_num1,&_num2)
#define msn(_data,_num) memset(_data,_num,sizeof(_data))
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define read3(_num1,_num2,_num3) scanf("%d %d %d",&_num1,&_num2,&_num3)
using namespace std;
#define MAXN 200100

int n , m;
int fro[MAXN] , too[MAXN] , cot[MAXN];
struct Edge{
	int from , to;
	int cost , id;
	Edge( int fromm , int too , int costt , int iid ){
		from = fromm;
		to = too;
		cost = costt;
		id = iid;
	}
};
bool operator<( Edge e1 , Edge e2 ){
	return e1.cost < e2.cost;
}
vector<Edge> g;

int dsu[MAXN];
void init(){
	for( int i = 1 ; i <= n ; i++ )
		dsu[i] = i;
}
int getfa( int pos ){
	if( dsu[pos] == pos ) return pos;
	return dsu[pos] = getfa( dsu[pos] );
}
bool unite( int a , int b ){
	a = getfa(a);
	b = getfa(b);
	if( a == b ) return 0;
	dsu[a] = b;
	return 1;
}

int mintreecost = 0;
vector<Edge> e[MAXN];
bool intree[MAXN];
void mintree(){
	sort( g.begin() , g.end() );
	ms( intree );
	init();
	int cnt = 0 , pos = 0;
	while( cnt < n-1 ){
		//cout << cnt << endl;
		//if( pos >= m ) return;
		Edge now = g[pos];
		if( unite( now.from , now.to) ){
			//cout << "[mintree] Choose Edge " << now.id << endl;
			mintreecost += now.cost;
			cnt++;
			e[now.from].pb( Edge( 0 , now.to , now.cost , now.id ) );
			e[now.to].pb( Edge( 0 , now.from , now.cost , now.id ) );
			intree[now.id] = 1;
		}
		pos++;
	}
	//for( int i = 1 ; i <= m ; i++ )
	//	cout << intree[i] << " ";
	//cout << endl;
	//cout << mintreecost << endl;
}

int maxcost;
bool dfs( int pos , int fa , int goal ){
	//cout << pos << endl;
	if( pos == goal ) return 1;
	for( uint i = 0 ; i < e[pos].size() ; i++ )
		if( e[pos][i].to != fa ){
			if( dfs( e[pos][i].to , pos , goal ) ){
				maxcost = max( maxcost , e[pos][i].cost );
				return 1;
			}
		}
	return 0;
}
int getmaxdis( int a , int b ){
	maxcost = -1;
	dfs( a , -1 , b );
	return maxcost;
}

void solve(){
	for( int i = 1 ; i <= m ; i++ ){
		if( intree[i] ){
			printf("%d\n",mintreecost);
			continue;
		}
		printf("%d\n", mintreecost - getmaxdis( fro[i] , too[i] ) + cot[i] );
	}
}

int main(){
//	fin("minimum.txt");

	read2( n , m );
	for( int i = 1 ; i <= m ; i++ ){
		int ff , tt , cc;
		read3( ff , tt , cc );
		g.pb( Edge( ff , tt , cc , i ) );
		fro[i] = ff;
		too[i] = tt;
		cot[i] = cc;
	}

	mintree();

	solve();

	return 0;
}


//76A
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF (1e9)
#define in cin >>
#define LINF (1e18)
#define out cout <<
#define ln  << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define sint short int
#define ull unsigned ll
#define uint unsigned int
#define B out "BreakPoint\n";
#define read(_num) scanf("%d",&_num)
#define ms(_data) memset(_data,0,sizeof(_data))
#define fin(_filename) freopen(_filename,"r",stdin)
#define fout(_filename) freopen(_filename,"w",stdout)
#define read2(_num1,_num2) scanf("%d %d",&_num1,&_num2)
#define msn(_data,_num) memset(_data,_num,sizeof(_data))
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define read3(_num1,_num2,_num3) scanf("%d %d %d",&_num1,&_num2,&_num3)
using namespace std;
#define MAXN 200600
#define MAXLOG 12

int n , m;
int fro[MAXN] , too[MAXN];
ll cot[MAXN];
struct Edge{
	int from , to , id;
	ll cost;
	Edge( int fromm , int too , ll costt , int iid ){
		from = fromm;
		to = too;
		cost = costt;
		id = iid;
	}
};
bool operator<( Edge e1 , Edge e2 ){
	return e1.cost < e2.cost;
}
vector<Edge> g;

int dsu[MAXN];
void init(){
	for( int i = 0 ; i <= n+2 ; i++ )
		dsu[i] = i;
}
int getfa( int pos ){
	if( dsu[pos] == pos ) return pos;
	return dsu[pos] = getfa( dsu[pos] );
}
bool unite( int a , int b ){
	//cout << a << " " << b << "    ";
	a = getfa(a);
	b = getfa(b);
	//cout << a << " " << b << endl;
	if( a == b ) return 0;
	dsu[a] = b;
	return 1;
}

ll mintreecost = 0;
vector<Edge> e[MAXN];
bool intree[MAXN];

void mintree(){
	sort( g.begin() , g.end() );
	ms( intree );
	for( int i = 1 ; i <= n ; i++ ) e[i].clear();
	init();
	int cnt = 0 , pos = 0;
	while( cnt < n-1 ){
		//if( pos >= m ) return;
		Edge now = g[pos];
		if( unite( now.from , now.to) ){
			//cout << "[mintree] Choose Edge " << now.id << endl;
			mintreecost += now.cost;
			cnt++;
			e[now.from].pb( Edge( 0 , now.to , now.cost , now.id ) );
			e[now.to].pb( Edge( 0 , now.from , now.cost , now.id ) );
			intree[now.id] = 1;
		}
		pos++;
	}
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << cycid[i] << " ";
	//cout << endl;
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << maxedge[i] << " ";
	//cout << endl;
	//cout << mintreecost << endl;
}

int par[MAXLOG][MAXN];
ll maxedge[MAXLOG][MAXN];	//maxedge[i][v]=x   MAXEDGE in [v~parent[i][v]]
int deep[MAXN];

void dfs( int pos , int fa , int d , ll lastcost ){
	deep[pos] = d;
	par[0][pos] = fa;
	maxedge[0][pos] = lastcost;
	for( uint i = 0 ; i < e[pos].size() ; i++ )
		if( e[pos][i].to != fa )
			dfs( e[pos][i].to , pos , d+1 , e[pos][i].cost );
}
void lca_init(){
	msn( deep , -1 );
	msn( maxedge , -1 );
	msn( par , -1 );
	dfs( 1 , -1 , 0 , -1 );
	for( int i = 0 ; i+1 < MAXLOG ; i++ )
		for( int j = 1 ; j <= n ; j++ ){
			if( par[i][j] < 0 ){
				par[i+1][j] = -1;
				maxedge[i+1][j] = maxedge[i][j];
			}else{
				par[i+1][j] = par[i][par[i][j]];
				maxedge[i+1][j] = max(  maxedge[i][j] , maxedge[i][par[i][j]]  );
			}
		}
	//for( int i = 1;  i <= n ; i++ ){
	//	int x = rand() % 3;
	//	cout << i << "  go up  2^" << x << "  =>  " << par[x][i] << endl;
	//}
}
ll getmaxdis( int a , int b ){
	ll ret = -1;
	if( deep[a] < deep[b] ) swap( a , b );
	int delta = deep[a] - deep[b];
	//cout << bitset<3>(delta) << endl;
	for( int i = 0 ; i < MAXLOG ; i++ ){
		if( delta == 0 ) break;
		if( delta & (1<<i) ){
			ret = max( ret , maxedge[i][a] );
			a = par[i][a];
		}
	}
	//cout << a << " " << b << endl;
	for( int i = MAXLOG-1 ; i >= 0 ; i-- )
		if( par[i][a] != par[i][b] ){
			ret = max( ret , maxedge[i][a] );
			ret = max( ret , maxedge[i][b] );
			a = par[i][a];
			b = par[i][b];
		}
	ret = max( ret , maxedge[0][a] );
	ret = max( ret , maxedge[0][b] );
	//cout << ret << endl;
	return ret;
}

void solve(){
	//cout << "[ANS]\n";
	for( int i = 1 ; i <= m ; i++ ){
		if( intree[i] ){
			printf("%I64d\n",mintreecost);
			continue;
		}
		printf("%I64d\n", mintreecost - getmaxdis( fro[i] , too[i] ) + cot[i] );
	}
}

int main(){
//	fin("minimum.txt");
	g.clear();
	ms(fro); ms(too); ms(cot);

	read2( n , m );
	for( int i = 1 ; i <= m ; i++ ){
		int ff , tt;
		ll cc;
		read2( ff , tt );
		scanf("%I64d",&cc);
		g.pb( Edge( ff , tt , cc , i ) );
		fro[i] = ff;
		too[i] = tt;
		cot[i] = cc;
	}

	mintree();
	lca_init();
	//cout << getmaxdis( 2 , 5 ) << endl;
	solve();

	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF (1e9)
#define in cin >>
#define LINF (1e18)
#define out cout <<
#define ln  << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define sint short int
#define ull unsigned ll
#define uint unsigned int
#define B out "BreakPoint\n";
#define read(_num) scanf("%d",&_num)
#define ms(_data) memset(_data,0,sizeof(_data))
#define fin(_filename) freopen(_filename,"r",stdin)
#define fout(_filename) freopen(_filename,"w",stdout)
#define read2(_num1,_num2) scanf("%d %d",&_num1,&_num2)
#define msn(_data,_num) memset(_data,_num,sizeof(_data))
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define read3(_num1,_num2,_num3) scanf("%d %d %d",&_num1,&_num2,&_num3)
using namespace std;
#define MAXN 200100
#define MAXLOG 10

int n , m;
int fro[MAXN] , too[MAXN];
ll cot[MAXN];
struct Edge{
	int from , to , id;
	ll cost;
	Edge( int fromm , int too , ll costt , int iid ){
		from = fromm;
		to = too;
		cost = costt;
		id = iid;
	}
};
bool operator<( Edge e1 , Edge e2 ){
	return e1.cost < e2.cost;
}
vector<Edge> g;

int dsu[MAXN];
void init(){
	for( int i = 1 ; i <= n ; i++ )
		dsu[i] = i;
}
int getfa( int pos ){
	if( dsu[pos] == pos ) return pos;
	return dsu[pos] = getfa( dsu[pos] );
}
bool unite( int a , int b ){
	//cout << a << " " << b << "    ";
	a = getfa(a);
	b = getfa(b);
	//cout << a << " " << b << endl;
	if( a == b ) return 0;
	dsu[a] = b;
	return 1;
}

ll mintreecost = 0;
vector<Edge> e[MAXN];
bool intree[MAXN];

void mintree(){
	sort( g.begin() , g.end() );
	ms( intree );
	init();
	int cnt = 0 , pos = 0;
	while( cnt < n-1 ){
		//if( pos >= m ) return;
		Edge now = g[pos];
		if( unite( now.from , now.to) ){
			//cout << "[mintree] Choose Edge " << now.id << endl;
			mintreecost += now.cost;
			cnt++;
			e[now.from].pb( Edge( 0 , now.to , now.cost , now.id ) );
			e[now.to].pb( Edge( 0 , now.from , now.cost , now.id ) );
			intree[now.id] = 1;
		}
		pos++;
	}
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << cycid[i] << " ";
	//cout << endl;
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << maxedge[i] << " ";
	//cout << endl;
	//cout << mintreecost << endl;
}

int par[MAXLOG][MAXN];
int maxedge[MAXLOG][MAXN];	//maxedge[i][v]=x   MAXEDGE in [v~parent[i][v]]
int deep[MAXN];

void dfs( int pos , int fa , int d , int lastcost ){
	deep[pos] = d;
	par[0][pos] = fa;
	maxedge[0][pos] = lastcost;
	for( uint i = 0 ; i < e[pos].size() ; i++ )
		if( e[pos][i].to != fa )
			dfs( e[pos][i].to , pos , d+1 , e[pos][i].cost );
}
void lca_init(){
	msn( deep , -1 );
	msn( maxedge , -1 );
	msn( par , -1 );
	dfs( 1 , -1 , 0 , -1 );
	for( int i = 0 ; i+1 < MAXLOG ; i++ )
		for( int j = 1 ; j <= n ; j++ ){
			if( par[i][j] < 0 ){
				par[i+1][j] = -1;
				maxedge[i+1][j] = maxedge[i][j];
			}else{
				par[i+1][j] = par[i][par[i][j]];
				maxedge[i+1][j] = max(  maxedge[i][j] , maxedge[i][par[i][j]]  );
			}
		}
	//for( int i = 1;  i <= n ; i++ ){
	//	int x = rand() % 3;
	//	cout << i << "  go up  2^" << x << "  =>  " << par[x][i] << endl;
	//}
}
int getmaxdis( int a , int b ){
	int ret = -1;
	if( deep[a] < deep[b] ) swap( a , b );
	int delta = deep[a] - deep[b];
	//cout << bitset<3>(delta) << endl;
	for( int i = 0 ; i < MAXLOG ; i++ ){
		if( delta == 0 ) break;
		if( delta & (1<<i) ){
			ret = max( ret , maxedge[i][a] );
			a = par[i][a];
		}
	}
	//cout << a << " " << b << endl;
	for( int i = MAXLOG ; i >= 0 ; i-- )
		if( par[i][a] != par[i][b] ){
			ret = max( ret , maxedge[i][a] );
			ret = max( ret , maxedge[i][b] );
			a = par[i][a];
			b = par[i][b];
		}
	ret = max( ret , maxedge[0][a] );
	ret = max( ret , maxedge[0][b] );
	//cout << ret << endl;
	return ret;
}

void solve(){
	//cout << "[ANS]\n";
	for( int i = 1 ; i <= m ; i++ ){
		if( intree[i] ){
			printf("%I64d\n",mintreecost);
			continue;
		}
		printf("%I64d\n", mintreecost - getmaxdis( fro[i] , too[i] ) + cot[i] );
	}
}

int main(){
//	fin("minimum.txt");

	read2( n , m );
	for( int i = 1 ; i <= m ; i++ ){
		int ff , tt;
		ll cc;
		read2( ff , tt );
		scanf("%I64d",&cc);
		g.pb( Edge( ff , tt , cc , i ) );
		fro[i] = ff;
		too[i] = tt;
		cot[i] = cc;
	}

	mintree();
	lca_init();
	//cout << getmaxdis( 2 , 5 ) << endl;
	solve();

	return 0;
}


#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <vector>
#include <bitset>
#include <ctime>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF (1e9)
#define in cin >>
#define LINF (1e18)
#define out cout <<
#define ln  << endl
#define pb push_back
#define mp make_pair
#define ll long long
#define sint short int
#define ull unsigned ll
#define uint unsigned int
#define B out "BreakPoint\n";
#define read(_num) scanf("%d",&_num)
#define ms(_data) memset(_data,0,sizeof(_data))
#define fin(_filename) freopen(_filename,"r",stdin)
#define fout(_filename) freopen(_filename,"w",stdout)
#define read2(_num1,_num2) scanf("%d %d",&_num1,&_num2)
#define msn(_data,_num) memset(_data,_num,sizeof(_data))
#define fastio ios_base::sync_with_stdio(false);cin.tie(0);
#define read3(_num1,_num2,_num3) scanf("%d %d %d",&_num1,&_num2,&_num3)
using namespace std;
#define MAXN 200100

int n , m;
int fro[MAXN] , too[MAXN];
ll cot[MAXN];
struct Edge{
	int from , to , id;
	ll cost;
	Edge( int fromm , int too , ll costt , int iid ){
		from = fromm;
		to = too;
		cost = costt;
		id = iid;
	}
};
bool operator<( Edge e1 , Edge e2 ){
	return e1.cost < e2.cost;
}
vector<Edge> g;

int dsu[MAXN];
void init(){
	for( int i = 1 ; i <= n ; i++ )
		dsu[i] = i;
}
int getfa( int pos ){
	if( dsu[pos] == pos ) return pos;
	return dsu[pos] = getfa( dsu[pos] );
}
bool unite( int a , int b ){
	//cout << a << " " << b << "    ";
	a = getfa(a);
	b = getfa(b);
	//cout << a << " " << b << endl;
	if( a == b ) return 0;
	dsu[a] = b;
	return 1;
}

ll mintreecost = 0;
vector<Edge> e[MAXN];
bool intree[MAXN];

void mintree(){
	sort( g.begin() , g.end() );
	ms( intree );
	init();
	int cnt = 0 , pos = 0 , cyc = 0;
	while( cnt < n-1 ){
		//if( pos >= m ) return;
		Edge now = g[pos];
		if( unite( now.from , now.to) ){
			//cout << "[mintree] Choose Edge " << now.id << endl;
			mintreecost += now.cost;
			cnt++;
			e[now.from].pb( Edge( 0 , now.to , now.cost , now.id ) );
			e[now.to].pb( Edge( 0 , now.from , now.cost , now.id ) );
			intree[now.id] = 1;
		}
		pos++;
	}
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << cycid[i] << " ";
	//cout << endl;
	//for( int i = 1 ; i <= n ; i++ )
	//	cout << maxedge[i] << " ";
	//cout << endl;
	//cout << mintreecost << endl;
}

ll maxcost;
bool dfs( int pos , int fa , int goal ){
	//cout << pos << endl;
	if( pos == goal ) return 1;
	for( int i = e[pos].size()-1 ; i >= 0 ; i-- )
		if( e[pos][i].to != fa ){
			if( dfs( e[pos][i].to , pos , goal ) ){
				maxcost = max( maxcost , e[pos][i].cost );
				return 1;
			}
		}
	return 0;
}
ll getmaxdis( int a , int b ){
	maxcost = -1;
	dfs( a , -1 , b );
	return maxcost;
}

void solve(){
	for( int i = 1 ; i <= m ; i++ ){
		if( intree[i] ){
			printf("%I64d\n",mintreecost);
			continue;
		}
		printf("%I64d\n", mintreecost - getmaxdis( fro[i] , too[i] ) + cot[i] );
	}
}

int main(){
//	fin("minimum.txt");

	read2( n , m );
	for( int i = 1 ; i <= m ; i++ ){
		int ff , tt;
		ll cc;
		read2( ff , tt );
		scanf("%I64d",&cc);
		g.pb( Edge( ff , tt , cc , i ) );
		fro[i] = ff;
		too[i] = tt;
		cot[i] = cc;
	}

	mintree();

	solve();

	return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define ll long long
#define pll pair<ll , ll>
#define pii pair<int , int>
#define pb push_back
#define N 1001
#define mod 1000000007
#define Base 100001
#define ret(x) return cout << x << endl , 0
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

bool mark[N];
vector<int> pr;

void find_prime(){
    for(int i = 2 ; i < N ; i++){
        if(!mark[i]){
            pr.pb(i);
            for(int j = 2 ; j * i < N ; j++)
                mark[i * j] = true;
        }
    }
}

ll lim = 1e9;
ll INF = 1e18;

ll up(int x){
    ll ans = 1;
    while(ans * x < lim)
        ans *= x;
    return ans;
}

ll num(int gcd , int x){
    int cnt = 1;
    while(gcd % x == 0)
        cnt++ , gcd /= x;
    return cnt;
}

int main(){
    ios_base :: sync_with_stdio(false);
    find_prime();
    int t;
    cin >> t;
    while(t--){
        ll mul = 1;
        int p = 0;
        int cnt = 1;
        queue<int> q;
        ll x;
        int a , b;
        ll gcd;

        for(int i = 0 ; i < 22 ; i++){
            if(q.size() > 1){
                a = q.front();
                q.pop();
                b = q.front();
                q.pop();
                x = up(a) * up(b);

                cout << "? " << x << endl;
                cin >> gcd;
                cnt *= num(gcd , a);
                cnt *= num(gcd , b);
            }
            else if(i == 21 && !q.empty()){
                x = up(q.front());
                cout << "? " << x << endl;
                cin >> gcd;
                cnt *= num(gcd , q.front());
            }
            else{
                x = 1;
                int last = p;
                while(INF / x >= pr[p]){
                    x *= pr[p];
                    p++;
                }
                cout << "? " << x << endl;
                cin >> gcd;

                for(int i = last ; i < p ; i++){
                    if(gcd % pr[i] == 0)
                        q.push(pr[i]);
                }
            }
        }

        cout <<"! " << max(cnt + 7 , cnt * 2) << endl;
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define ll long long
#define pll pair<ll , ll>
#define pii pair<int , int>
#define pb push_back
#define N 1001
#define mod 1000000007
#define Base 100001
#define ret(x) return cout << x << endl , 0
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>

bool mark[N];
vector<int> pr;

void find_prime(){
    for(int i = 2 ; i < N ; i++){
        if(!mark[i]){
            pr.pb(i);
            for(int j = 2 ; j * i < N ; j++)
                mark[i * j] = true;
        }
    }
}

ll lim = 1e9;
ll INF = 1e18;

ll up(int x){
    ll ans = 1;
    while(ans * x < lim)
        ans *= x;
    return ans;
}

ll num(int gcd , int x){
    int cnt = 1;
    while(gcd % x == 0)
        cnt++ , gcd /= x;
    return cnt;
}

int main(){
    ios_base :: sync_with_stdio(false);
    find_prime();
    ll a , b;
    ll gcd , ans;
    int cnt;
    int t;
    cin >> t;
    while(t--){
        cnt = 1;
        ans = 1;

        for(int i = 0 ; i < 22 ; i += 2){
            a = up(pr[i]);
            b = up(pr[i + 1]);
            cout << "? " << a * b << endl;
            cin >> gcd;
            cnt *= num(gcd , pr[i]);
            cnt *= num(gcd , pr[i + 1]);
            ans *= gcd;
        }

        if(ans <= 1000)
            cout << "! " << cnt + 7 << endl;
        else
            cout << "! " << cnt * 2 << endl;
    }
    return 0;
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Cf182 implements Runnable
{
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		public InputReader(InputStream stream)
		{
			this.stream = stream;
		}

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

			int res = 0;
			do
			{
				if(c<'0'||c>'9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Cf182(),"Main",1<<27).start();
	}

	public static long gcd(long a, long b)
	{
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}
	//  array sorting by colm
public static void sortbyColumn(int arr[][], int col)
    {

        Arrays.sort(arr, new Comparator<int[]>() {

          @Override
          public int compare(final int[] entry1,
                             final int[] entry2) {

            if (entry1[col] > entry2[col])
                return 1;
            else
                return -1;
          }
        });
    }

	// gcd

	public static long findGCD(long arr[], int n)
	{
		long result = arr[0];
		for (int i = 1; i < n; i++)
			result = gcd(arr[i], result);
		return result;
	}
	// fibonaci
	static int fib(int n)
    {
        int a = 0, b = 1, c;
        if (n == 0)
            return a;
        for (int i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }

// sort a string
    public static String sortString(String inputString)
    {

        char tempArray[] = inputString.toCharArray();

        Arrays.sort(tempArray);

        return new String(tempArray);
    }
    // pair function

    // list.add(new Pair<>(sc.nextInt(), i + 1));
    //     Collections.sort(list, (a, b) -> Integer.compare(b.first, a.first));
    private static class Pair<F, S> {

        private F first;

        private S second;

        public Pair() {}

        public Pair(F first, S second) {
            this.first = first;
            this.second = second;
        }
    }

	public void run()
	{
		InputReader sc = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);
		int t = sc.nextInt();
		while(t--!=0)
		{
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    HashSet<Long> set=new HashSet();
		    long i = 1;
		    while(true)
		    {

		        long temp = n;
		        while(temp>0)
		        {
		            set.add(temp%10);
		            temp = temp/10;
		        }
		        long maxD = Collections.max(set);
		        long minD = Collections.min(set);
		        n = n + (maxD*minD);
		        i++;
		        set.clear();

		        if(minD==0 || maxD==0)
		        {
		            System.out.println(n);
		            break;
		        }
		        if(k==i)
		        {
		            System.out.println(n);
		            break;
		        }
		    }
		}
	}
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Cf182 implements Runnable
{
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		public InputReader(InputStream stream)
		{
			this.stream = stream;
		}

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

			int res = 0;
			do
			{
				if(c<'0'||c>'9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Cf182(),"Main",1<<27).start();
	}

	public static long gcd(long a, long b)
	{
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}
	//  array sorting by colm
public static void sortbyColumn(int arr[][], int col)
    {

        Arrays.sort(arr, new Comparator<int[]>() {

          @Override
          public int compare(final int[] entry1,
                             final int[] entry2) {

            if (entry1[col] > entry2[col])
                return 1;
            else
                return -1;
          }
        });
    }

	// gcd

	public static long findGCD(long arr[], int n)
	{
		long result = arr[0];
		for (int i = 1; i < n; i++)
			result = gcd(arr[i], result);
		return result;
	}
	// fibonaci
	static int fib(int n)
    {
        int a = 0, b = 1, c;
        if (n == 0)
            return a;
        for (int i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }

// sort a string
    public static String sortString(String inputString)
    {

        char tempArray[] = inputString.toCharArray();

        Arrays.sort(tempArray);

        return new String(tempArray);
    }
    // pair function

    // list.add(new Pair<>(sc.nextInt(), i + 1));
    //     Collections.sort(list, (a, b) -> Integer.compare(b.first, a.first));
    private static class Pair<F, S> {

        private F first;

        private S second;

        public Pair() {}

        public Pair(F first, S second) {
            this.first = first;
            this.second = second;
        }
    }

	public void run()
	{
		InputReader sc = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);
		int t = sc.nextInt();
		while(t--!=0)
		{
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    HashSet<Long> set=new HashSet();
		    long i = 1;
		    while(true)
		    {
		        if(k==i)
		        {
		            System.out.println(n);
		            break;
		        }
		        long temp = n;
		        while(temp>0)
		        {
		            set.add(temp%10);
		            temp = temp/10;
		        }
		        long maxD = Collections.max(set);
		        long minD = Collections.min(set);
		        n = n + (maxD*minD);
		        i++;
		        set.clear();
		    }
		}
	}
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Cf182 implements Runnable
{
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		public InputReader(InputStream stream)
		{
			this.stream = stream;
		}

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

			int res = 0;
			do
			{
				if(c<'0'||c>'9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Cf182(),"Main",1<<27).start();
	}

	public static long gcd(long a, long b)
	{
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}
	//  array sorting by colm
public static void sortbyColumn(int arr[][], int col)
    {

        Arrays.sort(arr, new Comparator<int[]>() {

          @Override
          public int compare(final int[] entry1,
                             final int[] entry2) {

            if (entry1[col] > entry2[col])
                return 1;
            else
                return -1;
          }
        });
    }

	// gcd

	public static long findGCD(long arr[], int n)
	{
		long result = arr[0];
		for (int i = 1; i < n; i++)
			result = gcd(arr[i], result);
		return result;
	}
	// fibonaci
	static int fib(int n)
    {
        int a = 0, b = 1, c;
        if (n == 0)
            return a;
        for (int i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }

// sort a string
    public static String sortString(String inputString)
    {

        char tempArray[] = inputString.toCharArray();

        Arrays.sort(tempArray);

        return new String(tempArray);
    }
    // pair function

    // list.add(new Pair<>(sc.nextInt(), i + 1));
    //     Collections.sort(list, (a, b) -> Integer.compare(b.first, a.first));
    private static class Pair<F, S> {

        private F first;

        private S second;

        public Pair() {}

        public Pair(F first, S second) {
            this.first = first;
            this.second = second;
        }
    }

	public void run()
	{
		InputReader sc = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);
		int t = sc.nextInt();
		while(t--!=0)
		{
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    HashSet<Long> set=new HashSet();
		    long i = 1;
		    while(true)
		    {
		        if(k==i)
		        {
		            System.out.println(n);
		            break;
		        }
		        long temp = n;
		        int kk = 0;
		        while(temp>0)
		        {
		            if(kk==3)
		                break;
		            set.add(temp%10);
		            temp = temp/10;
		            kk++;
		        }
		        long maxD = Collections.max(set);
		        long minD = Collections.min(set);
		        n = n + (maxD*minD);
		        i++;
		        set.clear();
		    }
		}
	}
}


import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;

import static java.lang.Math.*;

public class Cf182 implements Runnable
{
	static class InputReader
	{
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;
		private SpaceCharFilter filter;
		private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		public InputReader(InputStream stream)
		{
			this.stream = stream;
		}

		public int read()
		{
			if (numChars==-1)
				throw new InputMismatchException();

			if (curChar >= numChars)
			{
				curChar = 0;
				try
				{
					numChars = stream.read(buf);
				}
				catch (IOException e)
				{
					throw new InputMismatchException();
				}

				if(numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		public String nextLine()
		{
			String str = "";
			try
			{
				str = br.readLine();
			}
			catch (IOException e)
			{
				e.printStackTrace();
			}
			return str;
		}
		public int nextInt()
		{
			int c = read();

			while(isSpaceChar(c))
				c = read();

			int sgn = 1;

			if (c == '-')
			{
				sgn = -1;
				c = read();
			}

			int res = 0;
			do
			{
				if(c<'0'||c>'9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));

			return res * sgn;
		}

		public long nextLong()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			long res = 0;

			do
			{
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			while (!isSpaceChar(c));
				return res * sgn;
		}

		public double nextDouble()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			int sgn = 1;
			if (c == '-')
			{
				sgn = -1;
				c = read();
			}
			double res = 0;
			while (!isSpaceChar(c) && c != '.')
			{
				if (c == 'e' || c == 'E')
					return res * Math.pow(10, nextInt());
				if (c < '0' || c > '9')
					throw new InputMismatchException();
				res *= 10;
				res += c - '0';
				c = read();
			}
			if (c == '.')
			{
				c = read();
				double m = 1;
				while (!isSpaceChar(c))
				{
					if (c == 'e' || c == 'E')
						return res * Math.pow(10, nextInt());
					if (c < '0' || c > '9')
						throw new InputMismatchException();
					m /= 10;
					res += (c - '0') * m;
					c = read();
				}
			}
			return res * sgn;
		}

		public String readString()
		{
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do
			{
				res.appendCodePoint(c);
				c = read();
			}
			while (!isSpaceChar(c));

			return res.toString();
		}

		public boolean isSpaceChar(int c)
		{
			if (filter != null)
				return filter.isSpaceChar(c);
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		public String next()
		{
			return readString();
		}

		public interface SpaceCharFilter
		{
			public boolean isSpaceChar(int ch);
		}
	}
	public static void main(String args[]) throws Exception
	{
		new Thread(null, new Cf182(),"Main",1<<27).start();
	}

	public static long gcd(long a, long b)
	{
		if (a == 0)
			return b;
		return gcd(b % a, a);
	}
	//  array sorting by colm
public static void sortbyColumn(int arr[][], int col)
    {

        Arrays.sort(arr, new Comparator<int[]>() {

          @Override
          public int compare(final int[] entry1,
                             final int[] entry2) {

            if (entry1[col] > entry2[col])
                return 1;
            else
                return -1;
          }
        });
    }

	// gcd

	public static long findGCD(long arr[], int n)
	{
		long result = arr[0];
		for (int i = 1; i < n; i++)
			result = gcd(arr[i], result);
		return result;
	}
	// fibonaci
	static int fib(int n)
    {
        int a = 0, b = 1, c;
        if (n == 0)
            return a;
        for (int i = 2; i <= n; i++)
        {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }

// sort a string
    public static String sortString(String inputString)
    {

        char tempArray[] = inputString.toCharArray();

        Arrays.sort(tempArray);

        return new String(tempArray);
    }
    // pair function

    // list.add(new Pair<>(sc.nextInt(), i + 1));
    //     Collections.sort(list, (a, b) -> Integer.compare(b.first, a.first));
    private static class Pair<F, S> {

        private F first;

        private S second;

        public Pair() {}

        public Pair(F first, S second) {
            this.first = first;
            this.second = second;
        }
    }

	public void run()
	{
		InputReader sc = new InputReader(System.in);
		PrintWriter w = new PrintWriter(System.out);
		int t = sc.nextInt();
		while(t--!=0)
		{
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    HashSet<Long> set=new HashSet();
		    long i = 1;
		    while(true)
		    {
		        if(k==i)
		        {
		            System.out.println(n);
		            break;
		        }
		        long temp = n;
		        while(temp>0)
		        {
		            set.add(temp%10);
		            temp = temp/10;
		        }
		        long maxD = Collections.max(set);
		        long minD = Collections.min(set);
		        n = n + (maxD*minD);
		        i++;
		        set.clear();

		        if(minD==0 || maxD==0)
		        {
		            System.out.println(n);
		            break;
		        }
		    }
		}
	}
}


import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q,r,m,largest,smallest;

	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            m=p;
                largest = 0;
                smallest=9;

                while(m != 0)
                {
                    r = m % 10;
                    if(r>largest){largest=r;}
                    if(r<smallest){smallest=r;}
                    m = m / 10;
                }
                p=p+(largest*smallest);
            }
            System.out.println(p);
        }
    }
}


import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q;
	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            p=p+digit(p)[0]*digit(p)[1];
            }
            System.out.println(p);
        }
    }
    public static long[] digit(long n)
    {
        long largest = 0;
        long smallest=9;

        while(n != 0)
        {
            long r = n % 10;
            largest = Math.max(r, largest);
            smallest = Math.min(r, smallest);
            n = n / 10;
        }
        return new long[]{largest,smallest};
    }
}


import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q;
        long m;
        long largest;
        long smallest;
        long r;
	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            m=p;
                largest = 0;
                smallest=9;

                while(m != 0)
                {
                    r = m % 10;
                    largest = Math.max(r, largest);
                    smallest = Math.min(r, smallest);
                    m = m / 10;
                }
                p=p+(largest*smallest);
            }
            System.out.println(p);
        }
    }
}


import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q;
	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            p=p+maxdigit(p)*mindigit(p);
            }
            System.out.println(p);
        }
    }
    public static long maxdigit(long n)
    {
        long largest = 0;

        while(n != 0)
        {
            long r = n % 10;
            largest = Math.max(r, largest);
            n = n / 10;
        }
        return largest;
    }
    public static long mindigit(long n)
    {
        long smallest = 9;

        while(n != 0)
        {
            long r = n % 10;
            smallest = Math.min(r, smallest);
            n = n / 10;
        }
        return smallest;
    }
}


import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q,r,m,largest,smallest;

	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            m=p;
                largest = 0;
                smallest=9;

                for(;m>0;m/=10)
                {
                    r = m % 10;
                    if(r>largest){largest=r;}
                    if(r<smallest){smallest=r;}
                }
                p=p+(largest*smallest);
            }
            System.out.println(p);
        }
    }
}


import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q,r,m,largest,smallest;

	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            m=p;
                largest = 0;
                smallest=9;

                for(;m>0;m/=10)
                {
                    r = m % 10;
                    if(r>largest){largest=r;}
                    if(r<smallest){smallest=r;}
                    if(smallest==0){break;}
                }
                if(smallest==0){break;}
                p=p+(largest*smallest);
            }
            System.out.println(p);
        }
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    int p,q;
        List<Integer> list=new ArrayList<>();
	    for(int i=0;i<n;i++){
	        p=scanner.nextInt();
	        q=scanner.nextInt();
	        for(int j=1;j<q;j++){
	            p=p+maxdigit(p)*mindigit(p);
            }
	        list.add(p);
        }
	    for(int i:list){
            System.out.println(i);
        }
    }
    public static int maxdigit(int n)
    {
        int largest = 0;

        while(n != 0)
        {
            int r = n % 10;
            largest = Math.max(r, largest);
            n = n / 10;
        }
        return largest;
    }
    public static int mindigit(int n)
    {
        int smallest = 9;

        while(n != 0)
        {
            int r = n % 10;
            smallest = Math.min(r, smallest);
            n = n / 10;
        }
        return smallest;
    }
}


import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    public static Scanner scanner=new Scanner(System.in);
    public static void main(String[] args) {
	    int n=scanner.nextInt();
	    long p,q;
        List<Long> list=new ArrayList<>();
	    for(int i=0;i<n;i++){
	        p=scanner.nextLong();
	        q=scanner.nextLong();
	        for(int j=1;j<q;j++){
	            p=p+maxdigit(p)*mindigit(p);
            }
	        list.add(p);
        }
	    for(long i:list){
            System.out.println(i);
        }
    }
    public static long maxdigit(long n)
    {
        long largest = 0;

        while(n != 0)
        {
            long r = n % 10;
            largest = Math.max(r, largest);
            n = n / 10;
        }
        return largest;
    }
    public static long mindigit(long n)
    {
        long smallest = 9;

        while(n != 0)
        {
            long r = n % 10;
            smallest = Math.min(r, smallest);
            n = n / 10;
        }
        return smallest;
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sequence_with_Digits {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            String[] str = reader.readLine().split(" ");
            long n = Long.parseLong(str[0]);
            long k = Long.parseLong(str[1]);
            long temp;
            for (long i=1;i<k;i++)
            {
                temp=n;
                long max = 0, min = 9;
                while(n>0) {
                    long d = n % 10;
                    if (d > max) {
                        max = d;
                    }
                    if (d < min) {
                        min = d;
                    }
                    n = n / 10;
                }
                n = temp + (min*max);
                if (min == 0)
                {
                    break;
                }
            }
            System.out.println(n);
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sequence_with_Digits {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            String[] str = reader.readLine().split(" ");
            long n = Long.parseLong(str[0]);
            long k = Long.parseLong(str[1]);
            k=k-1;
            long temp;
            if (k%3==0) {
                long div=k/3;
                for (long i = 0; i < div; i++) {
                    temp = n;
                    long max = 0, min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                    temp = n;
                    max = 0;
                    min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                    temp = n;
                    max = 0;
                    min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                }
            }
            else if(k % 3 == 1)
            {
                long div = k/3;
                for (long i = 0; i < div; i++) {
                    temp = n;
                    long max = 0, min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                    temp = n;
                    max = 0;
                    min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                    temp = n;
                    max = 0;
                    min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                }
                temp = n;
                long max = 0, min = 9;
                while (n > 0) {
                    long d = n % 10;
                    if (d > max) {
                        max = d;
                    }
                    if (d < min) {
                        min = d;
                    }
                    n = n / 10;
                }
                n = temp + (min * max);
            }
            else {
                long div = k / 3;
                for (long i = 0; i < div; i++) {
                    temp = n;
                    long max = 0, min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                    temp = n;
                    max = 0;
                    min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                    temp = n;
                    max = 0;
                    min = 9;
                    while (n > 0) {
                        long d = n % 10;
                        if (d > max) {
                            max = d;
                        }
                        if (d < min) {
                            min = d;
                        }
                        n = n / 10;
                    }
                    n = temp + (min * max);
                }
                temp = n;
                long max = 0, min = 9;
                while (n > 0) {
                    long d = n % 10;
                    if (d > max) {
                        max = d;
                    }
                    if (d < min) {
                        min = d;
                    }
                    n = n / 10;
                }
                n = temp + (min * max);
                temp = n;
                max = 0;
                min = 9;
                while (n > 0) {
                    long d = n % 10;
                    if (d > max) {
                        max = d;
                    }
                    if (d < min) {
                        min = d;
                    }
                    n = n / 10;
                }
                n = temp + (min * max);
            }
            System.out.println(n);
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sequence_with_Digits {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            String[] str = reader.readLine().split(" ");
            long n = Long.parseLong(str[0]);
            long k = Long.parseLong(str[1]);
            long temp;
            if (k%2!=0)
            {
                for (long i=1;i<k;i+=2)
                {
                    temp=n;
                    long max = 0, min = 9;
                    while(n>0)
                    {
                        long d = n%10;
                        if (d > max)
                        {
                            max = d;
                        }
                        if (d < min)
                        {
                            min = d;
                        }
                        n=n/10;
                    }
                    n = temp + (min*max);
                    temp=n;
                    max = 0;
                    min = 9;
                    while(n>0)
                    {
                        long d = n%10;
                        if (d > max)
                        {
                            max = d;
                        }
                        if (d < min)
                        {
                            min = d;
                        }
                        n=n/10;
                    }
                    n = temp + (min*max);
                }
            }
            else
            {
                for (long i=2;i<k;i+=2)
                {
                    temp=n;
                    long max = 0, min = 9;
                    while(n>0)
                    {
                        long d = n%10;
                        if (d > max)
                        {
                            max = d;
                        }
                        if (d < min)
                        {
                            min = d;
                        }
                        n=n/10;
                    }
                    n = temp + (min*max);
                    temp=n;
                    max = 0;
                    min = 9;
                    while(n>0)
                    {
                        long d = n%10;
                        if (d > max)
                        {
                            max = d;
                        }
                        if (d < min)
                        {
                            min = d;
                        }
                        n=n/10;
                    }
                    n = temp + (min*max);
                }
                temp=n;
                long max = 0, min = 9;
                while(n>0)
                {
                    long d = n%10;
                    if (d > max)
                    {
                        max = d;
                    }
                    if (d < min)
                    {
                        min = d;
                    }
                    n=n/10;
                }
                n = temp + (min*max);
            }

            System.out.println(n);
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sequence_with_Digits {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            String[] str = reader.readLine().split(" ");
            long n = Long.parseLong(str[0]);
            long k = Long.parseLong(str[1]);
            long temp;
            for (long i=1;i<k;i++)
            {
                temp=n;
                long max = 0, min = 9;
                while(n>0)
                {
                    long d = n%10;
                    if (d > max)
                    {
                        max = d;
                    }
                    if (d < min)
                    {
                        min = d;
                    }
                    n=n/10;
                }
                n = temp + (min*max);
            }
            System.out.println(n);
        }
    }
}


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Sequence_with_Digits {
    public static void main(String[] args) throws java.lang.Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(reader.readLine());
        while (t-- > 0) {
            String[] str = reader.readLine().split(" ");
            long n = Long.parseLong(str[0]);
            long k = Long.parseLong(str[1]);
            long temp;
            for (long i=1;i<k;i++)
            {
                temp=n;
                long max = 0, min = 9;
                while(n>0) {
                    long d = n % 10;
                    if (d > max) {
                        max = d;
                    }
                    if (d < min) {
                        min = d;
                    }
                    n = n / 10;
                }
                n = temp + (min*max);
                if (min == 0 && max == 0)
                {
                    break;
                }
            }
            System.out.println(n);
        }
    }
}


import java.io.*;
import java.util.*;
public class CFG
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        while(t-->0)
        {
            long a=sc.nextLong();
            long b=sc.nextLong();
            long sum=0;
            if(b==1)
            System.out.println(a);
            else
            { sum=a;
                while(b>1)
                {
                    sum+=dig(sum);
                    b--;
                }

                System.out.println(sum);
            }
        }}

    static long dig(long num)
    {
        int min=9;
        int max=0;
        String o=String.valueOf(num);

        for(int i=0;i<o.length();i++)
        {if((int)o.charAt(i)-48<min)
        {
        min=(int)o.charAt(i)-48;
        }
        if((int)o.charAt(i)-48>max)
        max=(int)o.charAt(i)-48;}
        return max*min;

    }
}


        import java.io.*;
        import java.util.*;
        public class CFG
        {
             Set<Integer> set=new HashSet<>();
            public static void main(String args[])
            {
                Scanner sc=new Scanner(System.in);
                int t=sc.nextInt();
                while(t-->0)
                {
                    long a=sc.nextLong();
                    long b=sc.nextLong();
                    long sum=0;
                    if(b==1)
                    System.out.println(a);
                    else
                    { sum=a;
                        while(b>1)
                        {
                            sum+=dig(sum);
                            b--;
                        }

                        System.out.println(sum);
                    }
                }}

            static long dig(long n)
    {
        long largest = 0;
        long smallest = 9;

        while(n != 0)
        {
           long r = n % 10;

            // Find the largest digit
            largest = Math.max(r, largest);

            // Find the smallest digit
            smallest = Math.min(r, smallest);

            n = n / 10;
            if(smallest==0)
            break;
        }
       return largest*smallest;

        }}


        import java.io.*;
        import java.util.*;
        public class CFG
        {
             Set<Integer> set=new HashSet<>();
            public static void main(String args[])
            {
                Scanner sc=new Scanner(System.in);
                int t=sc.nextInt();
                while(t-->0)
                {
                    long a=sc.nextLong();
                    long b=sc.nextLong();
                    long sum=0;
                    if(b==1)
                    System.out.println(a);
                    else
                    { sum=a;
                        while(b>1)
                        {
                            long f=dig(sum);
                            if(f==0)
                            break;
                            sum+=f;
                            b--;
                        }

                        System.out.println(sum);
                    }
                }}

            static long dig(long n)
    {
        long largest = 0;
        long smallest = 9;

        while(n != 0)
        {
           long r = n % 10;

            // Find the largest digit
            largest = Math.max(r, largest);

            // Find the smallest digit
            smallest = Math.min(r, smallest);

            n = n / 10;
            if(smallest==0)
            break;
        }
       return largest*smallest;

        }}


    import java.io.*;
    import java.util.*;
    public class CFG
    {
         Set<Integer> set=new HashSet<>();
        public static void main(String args[])
        {
            Scanner sc=new Scanner(System.in);
            int t=sc.nextInt();
            while(t-->0)
            {
                long a=sc.nextLong();
                long b=sc.nextLong();
                long sum=0;
                if(b==1)
                System.out.println(a);
                else
                { sum=a;
                    while(b>1)
                    {
                        sum+=dig(sum);
                        b--;
                    }

                    System.out.println(sum);
                }
            }}

        static long dig(long n)
{
    long largest = 0;
    long smallest = 9;

    while(n != 0)
    {
       long r = n % 10;

        // Find the largest digit
        largest = Math.max(r, largest);

        // Find the smallest digit
        smallest = Math.min(r, smallest);

        n = n / 10;
    }
   return largest*smallest;

    }}


import java.util.*;
import java.lang.*;
public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-->0){
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    long min = 9;
		    while(k-->1){
		        long a = n;
		        min = 9;
		        long max =0;
		        while(a!=0){
		            min=(long)Math.min(a%10,min);
		            max=(long)Math.max(a%10,max);
		            if(min==0) break;
		            a=a/10;
		        }
		        n = n + min*max;
		        if(min==0) break;
		    }
		    System.out.println(n);
		}
	}
}


import java.util.*;
import java.lang.*;
public class Main
{
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-->0){
		    long n = sc.nextLong();
		    long k = sc.nextLong();
		    while(k-->1){
		        long a = n;
		        long min = 9;
		        long max =0;
		        while(a!=0){
		            min=(long)Math.min(a%10,min);
		            max=(long)Math.max(a%10,max);
		            a=a/10;
		        }
		        n = n + min*max;
		    }
		    System.out.println(n);
		}
	}
}


import java.util.Scanner;

public class MyClass {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0) {
			long a = sc.nextLong(), k=sc.nextLong();
			while(--k>0) {
				long b = solve(a);
				if(b==0) break;
				a+=b;
			}
			System.out.println(a);
		}
	}
	public static long solve(long x) {
		long mn=10, mx=-1;
		while(x>0) {
			long y=x%10;
			mn=Math.min(mn, y);
			mx=Math.max(mx, y);
			x/=10;
		}
		return mn*mx;
	}
}


import java.util.Scanner;

public class MyClass {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0) {
			long a = sc.nextLong(), k=sc.nextLong();
			while(--k>0) {
				int mn=10, mx=-1, b;
				long t=1;
				while(a/t>=10) {
					b=(int) (a/t)%10;
					mn=mn>b?b:mn;
					mx=mx<b?b:mx;
					t*=10;
				}
				b=(int)(a/t);
				mn=mn>b?b:mn;
				mx=mx<b?b:mx;
				if(mn==0) break;
				a+=mn*mx;
			}
			System.out.println(a);
		}
	}
}


import java.util.Scanner;

public class MyClass {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0) {
			long a = sc.nextInt(), k=sc.nextInt();
			while(--k>0) {
				int mn=10, mx=-1,t=1;
				while(a/t>0) {
					int b=(int) (a/t)%10;
					mn=mn>b?b:mn;
					mx=mx<b?b:mx;
					t*=10;
				}
				if(mn==0) break;
				a+=mn*mx;
			}
			System.out.println(a);
		}
	}
}


import java.util.Scanner;

public class MyClass {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int T = sc.nextInt();
		while(T-->0) {
			long a = sc.nextInt(), k=sc.nextInt();
			while(--k>0) {
				int mn=10, mx=-1, b;
				long t=1;
				while(a/t>=10) {
					b=(int) (a/t)%10;
					mn=mn>b?b:mn;
					mx=mx<b?b:mx;
					t*=10;
				}
				b=(int)(a/t);
				mn=mn>b?b:mn;
				mx=mx<b?b:mx;
				if(mn==0) break;
				a+=mn*mx;
			}
			System.out.println(a);
		}
	}
}


import java.util.Scanner;
import java.util.*;
public class problemAdiv2 {
	public static void main(String[] args){
			Scanner in = new Scanner(System.in);
			////////////////////////////////////
			int t = Integer.parseInt(in.nextLine());
			while(t-->0){
				String[] nk = (in.nextLine().split(" "));
				long res = 0;
				res = Long.parseLong(nk[0]);
				Long k = Long.parseLong(nk[1]);
				while(k-->1){
					String s = Long.toString(res);
					if(s.contains("0")) break;
					SortedSet<Character> digits = new TreeSet();
					for(int i=0; i<s.length(); i++)
						digits.add(s.charAt(i));
					int smallest = Character.getNumericValue(digits.first());
					int largest = Character.getNumericValue(digits.last());
					//System.out.println(digits.last()+"---"+digits.first());
					res+=smallest*largest;
				}
				System.out.println(res);
			}
		}
}


import java.util.Scanner;
import java.util.*;
public class problemAdiv2 {
	public static void main(String[] args){
			Scanner in = new Scanner(System.in);
			////////////////////////////////////
			int t = Integer.parseInt(in.nextLine());
			while(t-->0){
				String[] nk = (in.nextLine().split(" "));
				long res = 0;
				res = Long.parseLong(nk[0]);
				Long k = Long.parseLong(nk[1]);
				while(k-->1){
					String s = Long.toString(res);
					SortedSet<Character> digits = new TreeSet();
					for(int i=0; i<s.length(); i++)
						digits.add(s.charAt(i));
					int smallest = Character.getNumericValue(digits.first());
					int largest = Character.getNumericValue(digits.last());
					//System.out.println(digits.last()+"---"+digits.first());
					res+=smallest*largest;
				}
				System.out.println(res);
			}
		}
}


import java.util.Scanner;

public class Problem1355a {

	private static Scanner sc;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		sc = new Scanner(System.in);

		int t = sc.nextInt();

		while(t-- > 0)
		{
			long n = sc.nextLong();
			long k = sc.nextLong();

			long ans = solve(n, k);

			System.out.println(ans);
		}
	}

	private static long solve(long n, long k)
	{
		while(--k > 0)
		{
			int y = multi(n);
			if(y == 0)	break;

			n += y;
		}

		return n;
	}

	private static int multi(long x)
	{
		int max = 10, min = 0;

		while(x > 0)
		{
			int y = (int) x % 10;
			x = x / 10;
			max = Math.min(max, y);
			min = Math.max(min, y);
		}

		return (max * min);
	}
}


import java.util.Scanner;

public class Problem1355a {

	private static Scanner sc;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		sc = new Scanner(System.in);

		int t = sc.nextInt();

		while(t-- > 0)
		{
			long n = sc.nextLong();
			long k = sc.nextLong();

			long ans = solvee(n, k);

			System.out.println(ans);
		}
	}

	private static long solve(long n, long k)
	{
		while(--k > 0)
		{
			long y = multi(n);
			if(y == 0)	break;

			n += y;
		}

		return n;
	}

	private static long multi(long x)
	{
		long max = 9, min = 0;

		while(x > 0)
		{
			long y =  x % 10;
			x = x / 10;
			max = Math.min(max, y);
			min = Math.max(min, y);
		}

		return (max * min);
	}

	private static long solvee(long n, long k)
	{
		if(k == 1)
			return n;

			int max = 0;
			int min = 0;
			long x = n;

			while(x > 0)
			{
				int c = (int) x % 10;

				if(c > max)
					max = c;
				else
					min = c;

				x = x / 10;
			}

			long cal = n + max * min;

			long ans = solve(cal, --k);

			return ans;
	}
}


import java.util.Scanner;

public class Problem1355a {

	private static Scanner sc;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		sc = new Scanner(System.in);

		int t = sc.nextInt();

		while(t-- > 0)
		{
			long n = sc.nextLong();
			long k = sc.nextLong();

			long ans = solve(n, k);

			System.out.println(ans);
		}
	}

	private static long solve(long n, long k)
	{
		while(--k > 0)
		{
			long y = multi(n);
			if(y == 0)	break;

			n += y;
		}

		return n;
	}

	private static long multi(long x)
	{
		long max = 9, min = 0;

		while(x > 0)
		{
			long y =  x % 10;
			x = x / 10;
			max = Math.min(max, y);
			min = Math.max(min, y);
		}

		return (max * min);
	}
}


import java.util.Scanner;

public class Problem1355a {

	private static Scanner sc;

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		sc = new Scanner(System.in);

		int t = sc.nextInt();

		while(t-- > 0)
		{
			long n = sc.nextLong();
			long k = sc.nextLong();

			long ans = solvee(n, k);

			System.out.println(ans);
		}
	}

	private static long solve(long n, long k)
	{
		while(--k > 0)
		{
			long y = multi(n);
			if(y == 0)	break;

			n += y;
		}

		return n;
	}

	private static long multi(long x)
	{
		long max = 9, min = 0;

		while(x > 0)
		{
			long y =  x % 10;
			x = x / 10;
			max = Math.min(max, y);
			min = Math.max(min, y);
		}

		return (max * min);
	}

	private static long solvee(long n, long k)
	{
		if(k == 1)
			return n;

			int max = 0;
			int min = 0;
			long x = n;

			while(x > 0)
			{
				int c = (int) x % 10;

				if(c > max)
					max = c;
				else
					min = c;

				x = x / 10;
			}

			long cal = n + max * min;

			long ans = solve(cal, k--);

			return ans;
	}
}