#include <bits/stdc++.h>
using namespace std;
#define int long long

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef pair<ll,pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1<<30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod

int n, m;
int cost[4005][4005];

class UnionFind {
    public:
        vector<int> parent, rank;
        vector<vector<int>> group;
        UnionFind(int size) {
            group.resize(size);
            for (int i = 0; i < size; i++) {
                parent.push_back(i);
                rank.push_back(0);
                group[i].push_back(i);
            }
        }

        int findset(int x) {
            return x == parent[x] ? x : parent[x] = findset(parent[x]);
        }

        void unite(int x, int y, int c) {
            x = findset(x); y = findset(y);
            if (x == y) return;
            if (rank[x] > rank[y]) swap(x, y);
            parent[x] = y;
            for (auto nodey : group[y]) {
                for (auto nodex : group[x]) {
                    cost[nodey][nodex] = c;
                    cost[nodex][nodey] = c;
                }
            }
            for (auto ch : group[x]) {
                group[y].push_back(ch);
            }
            if (rank[x] == rank[y]) rank[y] += 1;
        }

        bool same(int x, int y) {
            return findset(x) == findset(y);
        }
};

struct edge {ll cost, u, v;};
bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

vector<edge> es;

ll kruskal(int v, int e, vector<edge> es) {//vは頂点数,eは辺数
    sort(all(es), comp);   //esは[辺の重み,辺の端点1,辺の端点2]
    UnionFind node(v);
    ll ret = 0;
    for (int i = 0; i < e; i++) {
        edge e = es[i];
        if (!node.same(e.u, e.v)) {
            ret += e.cost;
            node.unite(e.u, e.v, e.cost);
        }
    }
    return ret;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        es.push_back(edge{c, a, b});
    }
    int total = kruskal(n, m, es);
    int q;
    cin >> q;
    rep(i, q) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        cout << total - cost[s][t] << endl;
    }
}

### Prompt

Please formulate a cpp solution to the following problem:

Takahashi found an undirected connected graph with N vertices and M edges. The vertices are numbered 1 through N. The i-th edge connects vertices a_i and b_i, and has a weight of c_i.

He will play Q rounds of a game using this graph. In the i-th round, two vertices S_i and T_i are specified, and he will choose a subset of the edges such that any vertex can be reached from at least one of the vertices S_i or T_i by traversing chosen edges.

For each round, find the minimum possible total weight of the edges chosen by Takahashi.

Constraints

* 1 ≦ N ≦ 4,000
* 1 ≦ M ≦ 400,000
* 1 ≦ Q ≦ 100,000
* 1 ≦ a_i,b_i,S_i,T_i ≦ N
* 1 ≦ c_i ≦ 10^{9}
* a_i \neq b_i
* S_i \neq T_i
* The given graph is connected.

Input

The input is given from Standard Input in the following format:


N M
a_1 b_1 c_1
a_2 b_2 c_2
:
a_M b_M c_M
Q
S_1 T_1
S_2 T_2
:
S_Q T_Q


Output

Print Q lines. The i-th line should contain the minimum possible total weight of the edges chosen by Takahashi.

Examples

Input

4 3
1 2 3
2 3 4
3 4 5
2
2 3
1 4


Output

8
7


Input

4 6
1 3 5
4 1 10
2 4 6
3 2 2
3 4 5
2 1 3
1
2 3


Output

8

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
#define int long long

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned __int128 HASH;
typedef pair<int,int> pii;
typedef pair<ll, ll> pll;
typedef pair<ull, ull> pullull;
typedef pair<ll,int> plli;
typedef pair<double, int> pdbi;
typedef pair<int,pii> pipii;
typedef pair<ll,pll> plpll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef vector<pii> vpii;
typedef vector<vector<int>> mat;

#define rep(i,n) for (int i=0;i<(n);i++)
#define rep2(i,a,b) for (int i=(a);i<(b);i++)
#define rrep(i,n) for (int i=(n);i>0;i--)
#define rrep2(i,a,b) for (int i=(a);i>b;i--)
#define pb push_back
#define fi first
#define se second
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()

const ll hmod1 = 999999937;
const ll hmod2 = 1000000000 + 9;
const int INF = 1<<30;
const ll mod = 1000000000 + 7;
const int dx4[4] = {1, 0, -1, 0};
const int dy4[4] = {0, 1, 0, -1};
const int dx8[8] = {1, 1, 1, 0, 0, -1, -1, -1};
const int dy8[8] = {0, 1, -1, 1, -1, 0, 1, -1};
const double pi = 3.141592653589793;

#define addm(X, Y) (X) = ((X) + ((Y) % mod) + mod) % mod

int n, m;
int cost[4005][4005];

class UnionFind {
    public:
        vector<int> parent, rank;
        vector<vector<int>> group;
        UnionFind(int size) {
            group.resize(size);
            for (int i = 0; i < size; i++) {
                parent.push_back(i);
                rank.push_back(0);
                group[i].push_back(i);
            }
        }

        int findset(int x) {
            return x == parent[x] ? x : parent[x] = findset(parent[x]);
        }

        void unite(int x, int y, int c) {
            x = findset(x); y = findset(y);
            if (x == y) return;
            if (rank[x] > rank[y]) swap(x, y);
            parent[x] = y;
            for (auto nodey : group[y]) {
                for (auto nodex : group[x]) {
                    cost[nodey][nodex] = c;
                    cost[nodex][nodey] = c;
                }
            }
            for (auto ch : group[x]) {
                group[y].push_back(ch);
            }
            if (rank[x] == rank[y]) rank[y] += 1;
        }

        bool same(int x, int y) {
            return findset(x) == findset(y);
        }
};

struct edge {ll cost, u, v;};
bool comp(const edge& e1, const edge& e2) {
    return e1.cost < e2.cost;
}

vector<edge> es;

ll kruskal(int v, int e, vector<edge> es) {//vは頂点数,eは辺数
    sort(all(es), comp);   //esは[辺の重み,辺の端点1,辺の端点2]
    UnionFind node(v);
    ll ret = 0;
    for (int i = 0; i < e; i++) {
        edge e = es[i];
        if (!node.same(e.u, e.v)) {
            ret += e.cost;
            node.unite(e.u, e.v, e.cost);
        }
    }
    return ret;
}

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> n >> m;
    rep(i, m) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;
        es.push_back(edge{c, a, b});
    }
    int total = kruskal(n, m, es);
    int q;
    cin >> q;
    rep(i, q) {
        int s, t;
        cin >> s >> t;
        s--; t--;
        cout << total - cost[s][t] << endl;
    }
}

```