#include <bits/stdc++.h>
using namespace std;
#define DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(...) debug_func_mult(split_names(#__VA_ARGS__), __VA_ARGS__)
#define DEBUG_ENDL endl << flush
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(...) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "") << flush;

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

string to_string(const string &s) { return s; }

template <typename T, typename U>
string to_string(const P<T, U> &p) {
    string ret = "(";
    ret += to_string(p.first);
    ret += ", ";
    ret += to_string(p.second);
    ret += ")";
    return move(ret);
}

template <typename T>
string to_string(const V<T> &v) {
    string ret = "{";
    for(const T &t : v) {
        ret += to_string(t);
        ret += ", ";
    }
    ret += "}";
    return move(ret);
}

template <typename T>
void debug_func(const T &t, const string &s = "") {
    if(s.size()) cout << s << " = ";
    cout << to_string(t) << DEBUG_ENDL_S(s);
}

template <typename T>
void debug_func_mult(int idx, const V<string> &names, const T &t) {
    debug_func(t, names[idx]);
}

template <typename T>
void debug_func_mult(const V<string> &names, const T &t) {
    debug_func(t, names[0]);
}

template <typename Head, typename... Tail>
void debug_func_mult(int idx, const V<string> &names, const Head &head, const Tail&... args) {
    debug_func(head, names[idx]);
    debug_func_mult(idx + 1, names, args...);
}

template <typename Head, typename... Tail>
void debug_func_mult(const V<string> &names, const Head &head, const Tail&... args) {
    debug_func(head, names[0]);
    debug_func_mult(1, names, args...);
}

V<string> split_names(string &&s) {
    replace(ALL(s), ' ', ',');
    V<string> ret;
    istringstream ss(s);
    string t;
    while(getline(ss, t, ',')) if(t.size()) ret.push_back(move(t));
    return move(ret);
}

void init_io() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
}

class UnionFind{
    using vec = vector<ll>;
    public:
        vec rank;
        vec parent;

        UnionFind(ll N){
            rank = vec(N, 0);
            parent = vec(N);
            iota(parent.begin(), parent.end(), 0ll);
        }

        ~UnionFind(){
        }

        ll find(ll child){
            return (child == parent[child] ? child : parent[child] = find(parent[child]));
        }

        void unit(ll x, ll y){
            ll px = find(x);
            ll py = find(y);
            if(px == py){
                return;
            }
            if(rank[px] < rank[py]){
                swap(px, py);
            }
            parent[py] = px;
            rank[px] += (rank[px] == rank[py]);
        }

        bool same(ll x, ll y){
            return (find(x) == find(y));
        }

};

using TLL = tuple<ll, ll, ll>;

int main() {
    init_io();
    ll N, M;
    cin >> N >> M;
    VV<PLL> edges(N);
    V<TLL> elis;
    for(ll i = 0; i < M; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        edges[a].emplace_back(b, c);
        edges[b].emplace_back(a, c);
        elis.emplace_back(a, b, c);
    }

    ll cost = 0;
    V<TLL> used_edges;

    {
        UnionFind uf(N);
        sort(ALL(elis), [](TLL a, TLL b) { return get<2>(a) < get<2>(b); });
        for(const auto &tup : elis) {
            ll a, b, c;
            tie(a, b, c) = tup;
            if(uf.same(a, b)) continue;
            uf.unit(a, b);
            cost += c;
            used_edges.push_back(tup);
        }
    }

    ll Q;
    cin >> Q;
    V<PLL> st;
    for(ll i = 0; i < Q; i++) {
        ll s, t;
        cin >> s >> t;
        st.emplace_back(s - 1, t - 1);
    }
    V<ll> rcosts(Q, 0);

    {
        for(ll i = 0; i < N - 1; i++) {
            UnionFind uf(N);
            for(ll j = 0; j < N - 1; j++) {
                if(i == j) continue;
                ll a, b, c;
                tie(a, b, c) = used_edges[j];
                uf.unit(a, b);
            }

            ll unusedc = get<2>(used_edges[i]);

            for(ll j = 0; j < Q; j++) {
                ll s, t;
                tie(s, t) = st[j];
                if(uf.same(s, t)) continue;
                chmax(rcosts[j], unusedc);
            }
        }
    }

    for(ll e : rcosts) cout << cost - e << endl;
    return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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 DEBUG_MODE
#define endl '\n'
#ifdef DEBUG_MODE
#define DEBUG(...) debug_func_mult(split_names(#__VA_ARGS__), __VA_ARGS__)
#define DEBUG_ENDL endl << flush
#define DEBUG_SEPARATOR_LINE cout<<"=================\n"
#else
#define DEBUG(...) 0
#define DEBUG_ENDL 0
#define DEBUG_SEPARATOR_LINE 0
#endif
#define ALL(V) (V).begin(), (V).end()
#define ALLR(V) (V).rbegin(), (V).rend()
#define DEBUG_ENDL_S(S) ((S).size() ? "\n" : "") << flush;

template <typename T> using V = vector<T>;
template <typename T> using VV = V<V<T>>;
template <typename T, typename U> using P = pair<T, U>;
using ll = int64_t;
using PLL = P<ll, ll>;

template <typename T> const T& var_min(const T &t) { return t; }
template <typename T> const T& var_max(const T &t) { return t; }
template <typename Head, typename... Tail> const Head& var_min(const Head &head, const Tail&... tail) { return min(head, var_min(tail...)); }
template <typename Head, typename... Tail> const Head& var_max(const Head &head, const Tail&... tail) { return max(head, var_max(tail...)); }
template <typename T, typename... Tail> void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); }
template <typename T, typename... Tail> void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); }

string to_string(const string &s) { return s; }

template <typename T, typename U>
string to_string(const P<T, U> &p) {
    string ret = "(";
    ret += to_string(p.first);
    ret += ", ";
    ret += to_string(p.second);
    ret += ")";
    return move(ret);
}

template <typename T>
string to_string(const V<T> &v) {
    string ret = "{";
    for(const T &t : v) {
        ret += to_string(t);
        ret += ", ";
    }
    ret += "}";
    return move(ret);
}

template <typename T>
void debug_func(const T &t, const string &s = "") {
    if(s.size()) cout << s << " = ";
    cout << to_string(t) << DEBUG_ENDL_S(s);
}

template <typename T>
void debug_func_mult(int idx, const V<string> &names, const T &t) {
    debug_func(t, names[idx]);
}

template <typename T>
void debug_func_mult(const V<string> &names, const T &t) {
    debug_func(t, names[0]);
}

template <typename Head, typename... Tail>
void debug_func_mult(int idx, const V<string> &names, const Head &head, const Tail&... args) {
    debug_func(head, names[idx]);
    debug_func_mult(idx + 1, names, args...);
}

template <typename Head, typename... Tail>
void debug_func_mult(const V<string> &names, const Head &head, const Tail&... args) {
    debug_func(head, names[0]);
    debug_func_mult(1, names, args...);
}

V<string> split_names(string &&s) {
    replace(ALL(s), ' ', ',');
    V<string> ret;
    istringstream ss(s);
    string t;
    while(getline(ss, t, ',')) if(t.size()) ret.push_back(move(t));
    return move(ret);
}

void init_io() {
    cin.tie(0);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(30);
}

class UnionFind{
    using vec = vector<ll>;
    public:
        vec rank;
        vec parent;

        UnionFind(ll N){
            rank = vec(N, 0);
            parent = vec(N);
            iota(parent.begin(), parent.end(), 0ll);
        }

        ~UnionFind(){
        }

        ll find(ll child){
            return (child == parent[child] ? child : parent[child] = find(parent[child]));
        }

        void unit(ll x, ll y){
            ll px = find(x);
            ll py = find(y);
            if(px == py){
                return;
            }
            if(rank[px] < rank[py]){
                swap(px, py);
            }
            parent[py] = px;
            rank[px] += (rank[px] == rank[py]);
        }

        bool same(ll x, ll y){
            return (find(x) == find(y));
        }

};

using TLL = tuple<ll, ll, ll>;

int main() {
    init_io();
    ll N, M;
    cin >> N >> M;
    VV<PLL> edges(N);
    V<TLL> elis;
    for(ll i = 0; i < M; i++) {
        ll a, b, c;
        cin >> a >> b >> c;
        a--;
        b--;
        edges[a].emplace_back(b, c);
        edges[b].emplace_back(a, c);
        elis.emplace_back(a, b, c);
    }

    ll cost = 0;
    V<TLL> used_edges;

    {
        UnionFind uf(N);
        sort(ALL(elis), [](TLL a, TLL b) { return get<2>(a) < get<2>(b); });
        for(const auto &tup : elis) {
            ll a, b, c;
            tie(a, b, c) = tup;
            if(uf.same(a, b)) continue;
            uf.unit(a, b);
            cost += c;
            used_edges.push_back(tup);
        }
    }

    ll Q;
    cin >> Q;
    V<PLL> st;
    for(ll i = 0; i < Q; i++) {
        ll s, t;
        cin >> s >> t;
        st.emplace_back(s - 1, t - 1);
    }
    V<ll> rcosts(Q, 0);

    {
        for(ll i = 0; i < N - 1; i++) {
            UnionFind uf(N);
            for(ll j = 0; j < N - 1; j++) {
                if(i == j) continue;
                ll a, b, c;
                tie(a, b, c) = used_edges[j];
                uf.unit(a, b);
            }

            ll unusedc = get<2>(used_edges[i]);

            for(ll j = 0; j < Q; j++) {
                ll s, t;
                tie(s, t) = st[j];
                if(uf.same(s, t)) continue;
                chmax(rcosts[j], unusedc);
            }
        }
    }

    for(ll e : rcosts) cout << cost - e << endl;
    return 0;
}

```