#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};
vector<int> t_parent;
vi t_ord;

template<typename T>
void wtree_getorder(const vector<vector<pair<int, T> > > &gw, int root, vector<T> &t_weight) {
	int n = (int)gw.size();
	t_parent.assign(n, -1);
	t_ord.clear();
	t_weight.assign(n, T());

	vector<int> stk; stk.push_back(root);
	while(!stk.empty()) {
		int i = stk.back(); stk.pop_back();
		t_ord.push_back(i);
		for(int j = (int)gw[i].size() - 1; j >= 0; j --) {
			int c = gw[i][j].first;
			if(t_parent[c] == -1 && c != root) {
				t_parent[c] = i;
				t_weight[c] = gw[i][j].second;
				stk.push_back(c);
			}
		}
	}
}


struct Edge {
	int a, b, c;
	bool operator<(const Edge &that) const {
		return c < that.c;
	}
};

struct Sum {
	int maxC;
	Sum() : maxC(-INF) {}
	explicit Sum(int x) : maxC(x) {}
	Sum &operator+=(const Sum &that) {
		amax(maxC, that.maxC);
		return *this;
	}
};
struct PathCompressionTree {
private:
	vector<int> parent;
	vector<Sum> val, sum;
public:
	void init(int n, const vector<Sum> &initval) {
		parent.assign(n, -1);
		val = initval;
		sum.assign(n, Sum());
	}
	void link(int c, int p) {
		assert(parent[c] == -1 && p != -1);
		parent[c] = p;
		sum[c] = val[c];
	}
	int find(int u) {
		int p = parent[u], g;
		if(p == -1) return u;
		g = parent[u] = find(p);
		sum[u] += sum[p];
		return g;
	}
	Sum get(int u) {
		find(u);
		return sum[u];
	}
};

struct Query {
	int u, v;
	int i;
};

vector<vi> g;
vector<bool> visited;
PathCompressionTree pct;
vector<vector<Query>> queries, lca;
vector<Sum> vals, ans;

void dfs(int i, int p) {
	for(int j : g[i]) if(j != p)
		dfs(j, i);
	visited[i] = true;
	for(const Query &q : queries[i]) {
		if(visited[q.v])
			lca[pct.find(q.v)].push_back(q);
	}
	for(const Query &q : lca[i]) {
		ans[q.i] += pct.get(q.u);
		ans[q.i] += pct.get(q.v);
	}
	if(p != -1)
		pct.link(i, p);
}

int main() {
	int N; int M;
	while(~scanf("%d%d", &N, &M)) {
		vector<Edge> edges(M);
		rep(i, M) {
			int a; int b; int c;
			scanf("%d%d%d", &a, &b, &c), -- a, -- b;
			edges[i] = { a, b, c };
		}
		sort(edges.begin(), edges.end());
		UnionFind uf; uf.init(N);
		g.assign(N, vi());
		vector<vector<pair<int, Sum>>> gw(N);
		ll cost = 0;
		for(auto &&e : edges) {
			if(uf.unionSet(e.a, e.b)) {
				g[e.a].push_back(e.b);
				g[e.b].push_back(e.a);
				gw[e.a].emplace_back(e.b, Sum(e.c));
				gw[e.b].emplace_back(e.a, Sum(e.c));
				cost += e.c;
			}
		}
		wtree_getorder(gw, 0, vals);
		queries.assign(N, vector<Query>());
		int Q;
		scanf("%d", &Q);
		rep(i, Q) {
			int u; int v;
			scanf("%d%d", &u, &v), -- u, -- v;
			queries[u].push_back(Query{ u, v, i });
			queries[v].push_back(Query{ v, u, i });
		}
		lca.assign(N, vector<Query>());
		ans.assign(Q, Sum());
		visited.assign(N, false);
		pct.init(N, vals);
		dfs(0, -1);
		rep(i, Q) {
			ll x = cost - max(0, ans[i].maxC);
			printf("%lld\n", x);
		}
	}
	return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

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 rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }

struct UnionFind {
	vector<int> data;
	void init(int n) { data.assign(n, -1); }
	bool unionSet(int x, int y) {
		x = root(x); y = root(y);
		if(x != y) {
			if(data[y] < data[x]) swap(x, y);
			data[x] += data[y]; data[y] = x;
		}
		return x != y;
	}
	bool findSet(int x, int y) { return root(x) == root(y); }
	int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
	int size(int x) { return -data[root(x)]; }
};
vector<int> t_parent;
vi t_ord;

template<typename T>
void wtree_getorder(const vector<vector<pair<int, T> > > &gw, int root, vector<T> &t_weight) {
	int n = (int)gw.size();
	t_parent.assign(n, -1);
	t_ord.clear();
	t_weight.assign(n, T());

	vector<int> stk; stk.push_back(root);
	while(!stk.empty()) {
		int i = stk.back(); stk.pop_back();
		t_ord.push_back(i);
		for(int j = (int)gw[i].size() - 1; j >= 0; j --) {
			int c = gw[i][j].first;
			if(t_parent[c] == -1 && c != root) {
				t_parent[c] = i;
				t_weight[c] = gw[i][j].second;
				stk.push_back(c);
			}
		}
	}
}


struct Edge {
	int a, b, c;
	bool operator<(const Edge &that) const {
		return c < that.c;
	}
};

struct Sum {
	int maxC;
	Sum() : maxC(-INF) {}
	explicit Sum(int x) : maxC(x) {}
	Sum &operator+=(const Sum &that) {
		amax(maxC, that.maxC);
		return *this;
	}
};
struct PathCompressionTree {
private:
	vector<int> parent;
	vector<Sum> val, sum;
public:
	void init(int n, const vector<Sum> &initval) {
		parent.assign(n, -1);
		val = initval;
		sum.assign(n, Sum());
	}
	void link(int c, int p) {
		assert(parent[c] == -1 && p != -1);
		parent[c] = p;
		sum[c] = val[c];
	}
	int find(int u) {
		int p = parent[u], g;
		if(p == -1) return u;
		g = parent[u] = find(p);
		sum[u] += sum[p];
		return g;
	}
	Sum get(int u) {
		find(u);
		return sum[u];
	}
};

struct Query {
	int u, v;
	int i;
};

vector<vi> g;
vector<bool> visited;
PathCompressionTree pct;
vector<vector<Query>> queries, lca;
vector<Sum> vals, ans;

void dfs(int i, int p) {
	for(int j : g[i]) if(j != p)
		dfs(j, i);
	visited[i] = true;
	for(const Query &q : queries[i]) {
		if(visited[q.v])
			lca[pct.find(q.v)].push_back(q);
	}
	for(const Query &q : lca[i]) {
		ans[q.i] += pct.get(q.u);
		ans[q.i] += pct.get(q.v);
	}
	if(p != -1)
		pct.link(i, p);
}

int main() {
	int N; int M;
	while(~scanf("%d%d", &N, &M)) {
		vector<Edge> edges(M);
		rep(i, M) {
			int a; int b; int c;
			scanf("%d%d%d", &a, &b, &c), -- a, -- b;
			edges[i] = { a, b, c };
		}
		sort(edges.begin(), edges.end());
		UnionFind uf; uf.init(N);
		g.assign(N, vi());
		vector<vector<pair<int, Sum>>> gw(N);
		ll cost = 0;
		for(auto &&e : edges) {
			if(uf.unionSet(e.a, e.b)) {
				g[e.a].push_back(e.b);
				g[e.b].push_back(e.a);
				gw[e.a].emplace_back(e.b, Sum(e.c));
				gw[e.b].emplace_back(e.a, Sum(e.c));
				cost += e.c;
			}
		}
		wtree_getorder(gw, 0, vals);
		queries.assign(N, vector<Query>());
		int Q;
		scanf("%d", &Q);
		rep(i, Q) {
			int u; int v;
			scanf("%d%d", &u, &v), -- u, -- v;
			queries[u].push_back(Query{ u, v, i });
			queries[v].push_back(Query{ v, u, i });
		}
		lca.assign(N, vector<Query>());
		ans.assign(Q, Sum());
		visited.assign(N, false);
		pct.init(N, vals);
		dfs(0, -1);
		rep(i, Q) {
			ll x = cost - max(0, ans[i].maxC);
			printf("%lld\n", x);
		}
	}
	return 0;
}

```