#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
const int logN = 20;
int p[N];
int tin[N], tout[N];
int T = 0;
int fup[N][logN];
int dist[N];
vector<pair<int, int>> g[N];
int dsu_get (int v) {
return (v == p[v]) ? v : (p[v] = dsu_get (p[v]));
}
void dsu_unite (int a, int b) {
a = dsu_get (a);
b = dsu_get (b);
if (rand() & 1)
swap (a, b);
if (a != b)
p[a] = b;
}
void dfs(int v, int u, int d)
{
dist[v] = d;
tin[v] = T++;
fup[v][0] = u;
for(int i = 1; i < logN; i++)
fup[v][i] = fup[fup[v][i - 1]][i - 1];
for(auto [y, w] : g[v])
if(y != u)
dfs(y, v, d + w);
tout[v] = T++;
}
bool is_ancestor(int u, int v)
{
return tin[u] <= tin[v] && tout[u] >= tout[v];
}
int LCA(int x, int y)
{
if(is_ancestor(x, y)) return x;
for(int i = logN - 1; i >= 0; i--)
if(!is_ancestor(fup[x][i], y))
x = fup[x][i];
return fup[x][0];
}
int main() {
int n,m;
cin >> n >> m;
vector<pair<int,pair<int,int>>> e;
for (int i = 0; i < m; ++i) {
int u,v,w;
cin >>u>>v>>w;
e.push_back({w,{u,v}});
}
int cost = 0, cnt = 0;
sort(e.begin(), e.end());
for (int i = 1; i <= n; ++i) p[i] = i;
for (int i = 0; i < m; ++i) {
int u = e[i].second.first, v = e[i].second.second, w = e[i].first;
if (dsu_get(u) != dsu_get(v)) {
cost += w;
++cnt;
g[u].push_back ({v,w});
g[v].push_back ({u,w});
dsu_unite (u, v);
}
}
if (cnt != n - 1) {
cout << "Impossible" << endl;
return 0;
}
cout << cost << endl;
int q;
cin >> q;
dfs(1, 1, 0);
while (q--) {
int u,v;
cin >>u>>v;
int lca = LCA(u, v);
int ans = dist[u] - 2*dist[lca] + dist[v];
cout <<ans<<endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDEwMDEwMDsKY29uc3QgaW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AgICAgeCA9IGZ1cFt4XVtpXTsKICAgIHJldHVybiBmdXBbeF1bMF07Cn0KCmludCBtYWluKCkgewogICAgaW50IG4sbTsKICAgIGNpbiA+PiBuID4+IG07CgogICAgdmVjdG9yPHBhaXI8aW50LHBhaXI8aW50LGludD4+PiBlOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyArK2kpIHsKICAgICAgICBpbnQgdSx2LHc7CiAgICAgICAgY2luID4+dT4+dj4+dzsKICAgICAgICBlLnB1c2hfYmFjayh7dyx7dSx2fX0pOwogICAgfQoKICAgIGludCBjb3N0ID0gMCwgY250ID0gMDsKICAgIHNvcnQoZS5iZWdpbigpLCBlLmVuZCgpKTsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkgcFtpXSA9IGk7CiAgICBmb3IgKGludCBpID0gMDsgaSA8IG07ICsraSkgewogICAgICAgIGludCB1ID0gZVtpXS5zZWNvbmQuZmlyc3QsICB2ID0gZVtpXS5zZWNvbmQuc2Vjb25kLCAgdyA9IGVbaV0uZmlyc3Q7CiAgICAgICAgaWYgKGRzdV9nZXQodSkgIT0gZHN1X2dldCh2KSkgewogICAgICAgICAgICBjb3N0ICs9IHc7CiAgICAgICAgICAgICsrY250OwogICAgICAgICAgICBnW3VdLnB1c2hfYmFjayAoe3Ysd30pOwogICAgICAgICAgICBnW3ZdLnB1c2hfYmFjayAoe3Usd30pOwogICAgICAgICAgICBkc3VfdW5pdGUgKHUsIHYpOwogICAgICAgIH0KICAgIH0KCiAgICBpZiAoY250ICE9IG4gLSAxKSB7CiAgICAgICAgY291dCA8PCAiSW1wb3NzaWJsZSIgPDwgZW5kbDsKICAgICAgICByZXR1cm4gMDsKICAgIH0KICAgIGNvdXQgPDwgY29zdCA8PCBlbmRsOwoKICAgIGludCBxOwogICAgY2luID4+IHE7CiAgICBkZnMoMSwgMSwgMCk7CiAgICB3aGlsZSAocS0tKSB7CiAgICAgICAgaW50IHUsdjsKICAgICAgICBjaW4gPj51Pj52OwoKICAgICAgICBpbnQgbGNhID0gTENBKHUsIHYpOwogICAgICAgIGludCBhbnMgPSBkaXN0W3VdIC0gMipkaXN0W2xjYV0gKyBkaXN0W3ZdOwogICAgICAgIGNvdXQgPDxhbnM8PGVuZGw7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0=