#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <string>
#include <cstring>
#define MID(x,y) ((x+y)/2)
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int MAXV = 50005;
const int MAXE = 100005;
struct Gragh{
struct Gragh_Node{
int u, v, w;
int opp;
int next;
int res; //存Query的结果
}arc[MAXE];
int cnt, head[MAXV];
void init(){
cnt = 0;
memset(head, -1, sizeof(head));
}
void g_insert(int u, int v, int w){
arc[cnt].u = u;
arc[cnt].v = v;
arc[cnt].w = w;
arc[cnt].res = 0;
arc[cnt].next = head[u];
head[u] = cnt ++;
arc[cnt].u = v;
arc[cnt].v = u;
arc[cnt].w = w;
arc[cnt].res = 0;
arc[cnt].next = head[v];
head[v] = cnt ++;
}
void q_insert(int u, int v){
arc[cnt].u = u;
arc[cnt].v = v;
arc[cnt].res = 0;
arc[cnt].next = head[u];
arc[cnt].opp = cnt + 1;
head[u] = cnt ++;
arc[cnt].u = v;
arc[cnt].v = u;
arc[cnt].res = 0;
arc[cnt].next = head[v];
arc[cnt].opp = cnt - 1;
head[v] = cnt ++;
}
};
struct Disjoint_Sets{
//略
};
struct LCA{
Gragh G, Q; //G存图(树); Q存查询,可以把每个询问当成一个无向边存储
Disjoint_Sets DS;
int ancestor[MAXV];
int indegree[MAXV];
int dist[MAXV];
bool vis[MAXV];
bool flag[MAXV];
void init(int n){
memset(ancestor, 0, sizeof(ancestor));
memset(vis, 0, sizeof(vis));
memset(flag, 0, sizeof(flag));
memset(dist, 0, sizeof(dist));
G.init();
Q.init();
DS.Init(n);
}
void insert_gragh(int u, int v, int w){
G.g_insert(u, v, w);
}
void insert_query(int u, int v){
Q.q_insert(u, v);
}
void lca(int u, int dis){
ancestor[u] = u;
dist[u] = dis;
flag[u] = 1;
for (int i = G.head[u]; i != -1; i = G.arc[i].next){
int v = G.arc[i].v;
if (!flag[v]){
lca(v, dis+G.arc[i].w);
DS.Union(u, v);
ancestor[DS.Father(u)] = u;
}
}
vis[u] = 1;
for (int i = Q.head[u]; i != -1; i = Q.arc[i].next){
int v = Q.arc[i].v;
if (vis[v]){
Q.arc[i].res = dist[u] + dist[v] - 2*dist[ancestor[DS.Father(v)]];
Q.arc[Q.arc[i].opp].res = dist[u] + dist[v] - 2*dist[ancestor[DS.Father(v)]];
}
}
}
void solve(int n){
// memset(indegree, 0, sizeof(indegree));
// for (int i = 0; i < G.cnt; i ++){
// indegree[G.arc[i].v] ++;
// }
// for (int i = 1; i <= n; i ++){
// if (indegree[i] == 0)
// lca(i, 0);
// }
lca(1, 0);
}
}L;
int main(){
int n, m;
scanf("%d %d", &n, &m);
L.init(n);
for (int i = 0; i < m; i ++){
int u, v, w;
scanf("%d %d %d %*c", &u, &v, &w);
L.insert_gragh(u, v, w);
}
int q;
scanf("%d", &q);
for (int i = 0; i < q; i ++){
int u, v;
scanf("%d %d", &u, &v);
L.insert_query(u, v);
}
L.solve(n);
for (int i = 1; i <= q; i ++){
printf("%d\n", L.Q.arc[2*i-1].res);
}
return 0;
}
I2luY2x1ZGUgPGNzdGRpbz4KI2luY2x1ZGUgPGNzdGRsaWI+CiNpbmNsdWRlIDxjbWF0aD4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPHN0YWNrPgojaW5jbHVkZSA8cXVldWU+CiNpbmNsdWRlIDxzZXQ+CiNpbmNsdWRlIDxtYXA+CiNpbmNsdWRlIDxhbGdvcml0aG0+CiNpbmNsdWRlIDxzdHJpbmc+CiNpbmNsdWRlIDxjc3RyaW5nPgojZGVmaW5lIE1JRCh4LHkpICgoeCt5KS8yKQojZGVmaW5lIG1lbShhLGIpIG1lbXNldChhLGIsc2l6ZW9mKGEpKQp1c2luZyBuYW1lc3BhY2Ugc3RkOwpjb25zdCBpbnQgTUFYViA9IDUwMDA1Owpjb25zdCBpbnQgTUFYRSA9IDEwMDAwNTsKc3RydWN0IEdyYWdoewogICAgc3RydWN0IEdyYWdoX05vZGV7CiAgICAgICAgaW50IHUsIHYsIHc7CiAgICAgICAgaW50IG9wcDsKICAgICAgICBpbnQgbmV4dDsKICAgICAgICBpbnQgcmVzOyAgICAgICAgLy/lrZhRdWVyeeeahOe7k+aenAogICAgfWFyY1tNQVhFXTsKICAgIGludCBjbnQsIGhlYWRbTUFYVl07CiAgICB2b2lkIGluaXQoKXsKICAgICAgICBjbnQgPSAwOwogICAgICAgIG1lbXNldChoZWFkLCAtMSwgc2l6ZW9mKGhlYWQpKTsKICAgIH0KICAgIHZvaWQgZ19pbnNlcnQoaW50IHUsIGludCB2LCBpbnQgdyl7CiAgICAgICAgYXJjW2NudF0udSA9IHU7CiAgICAgICAgYXJjW2NudF0udiA9IHY7CiAgICAgICAgYXJjW2NudF0udyA9IHc7CiAgICAgICAgYXJjW2NudF0ucmVzID0gMDsKICAgICAgICBhcmNbY250XS5uZXh0ID0gaGVhZFt1XTsKICAgICAgICBoZWFkW3VdID0gY250ICsrOwoKICAgICAgICBhcmNbY250XS51ID0gdjsKICAgICAgICBhcmNbY250XS52ID0gdTsKICAgICAgICBhcmNbY250XS53ID0gdzsKICAgICAgICBhcmNbY250XS5yZXMgPSAwOwogICAgICAgIGFyY1tjbnRdLm5leHQgPSBoZWFkW3ZdOwogICAgICAgIGhlYWRbdl0gPSBjbnQgKys7CiAgICB9CiAgICB2b2lkIHFfaW5zZXJ0KGludCB1LCBpbnQgdil7CiAgICAgICAgYXJjW2NudF0udSA9IHU7CiAgICAgICAgYXJjW2NudF0udiA9IHY7CiAgICAgICAgYXJjW2NudF0ucmVzID0gMDsKICAgICAgICBhcmNbY250XS5uZXh0ID0gaGVhZFt1XTsKICAgICAgICBhcmNbY250XS5vcHAgPSBjbnQgKyAxOwogICAgICAgIGhlYWRbdV0gPSBjbnQgKys7CiAgICAgICAgYXJjW2NudF0udSA9IHY7CiAgICAgICAgYXJjW2NudF0udiA9IHU7CiAgICAgICAgYXJjW2NudF0ucmVzID0gMDsKICAgICAgICBhcmNbY250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AgZmxhZ1t1XSA9IDE7CiAgICAgICAgZm9yIChpbnQgaSA9IEcuaGVhZFt1XTsgaSAhPSAtMTsgaSA9IEcuYXJjW2ldLm5leHQpewogICAgICAgICAgICBpbnQgdiA9IEcuYXJjW2ldLnY7CiAgICAgICAgICAgIGlmICghZmxhZ1t2XSl7CiAgICAgICAgICAgICAgICBsY2EodiwgZGlzK0cuYXJjW2ldLncpOwogICAgICAgICAgICAgICAgRFMuVW5pb24odSwgdik7CiAgICAgICAgICAgICAgICBhbmNlc3RvcltEUy5GYXRoZXIodSldID0gdTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICB2aXNbdV0gPSAxOwogICAgICAgIGZvciAoaW50IGkgPSBRLmhlYWRbdV07IGkgIT0gLTE7IGkgPSBRLmFyY1tpXS5uZXh0KXsKICAgICAgICAgICAgaW50IHYgPSBRLmFyY1tpXS52OwogICAgICAgICAgICBpZiAodmlzW3ZdKXsKICAgICAgICAgICAgICAgIFEuYXJjW2ldLnJlcyA9IGRpc3RbdV0gKyBkaXN0W3ZdIC0gMipkaXN0W2FuY2VzdG9yW0RTLkZhdGhlcih2KV1dOwogICAgICAgICAgICAgICAgUS5hcmNbUS5hcmNbaV0ub3BwXS5yZXMgPSBkaXN0W3VdICsgZGlzdFt2XSAtIDIqZGlzdFthbmNlc3RvcltEUy5GYXRoZXIodildXTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIHZvaWQgc29sdmUoaW50IG4pewovLyAgICAgICAgbWVtc2V0KGluZGVncmVlLCAwLCBzaXplb2YoaW5kZWdyZWUpKTsKLy8gICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgRy5jbnQ7IGkgKyspewovLyAgICAgICAgICAgIGluZGVncmVlW0cuYXJjW2ldLnZdICsrOwovLyAgICAgICAgfQovLyAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSArKyl7Ci8vICAgICAgICAgICAgaWYgKGluZGVncmVlW2ldID09IDApCi8vICAgICAgICAgICAgICAgIGxjYShpLCAwKTsKLy8gICAgICAgIH0KICAgICAgICBsY2EoMSwgMCk7CiAgICB9Cn1MOwppbnQgbWFpbigpewogICAgaW50IG4sIG07CiAgICBzY2FuZigiJWQgJWQiLCAmbiwgJm0pOwogICAgTC5pbml0KG4pOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBtOyBpICsrKXsKICAgICAgICBpbnQgdSwgdiwgdzsKICAgICAgICBzY2FuZigiJWQgJWQgJWQgJSpjIiwgJnUsICZ2LCAmdyk7CiAgICAgICAgTC5pbnNlcnRfZ3JhZ2godSwgdiwgdyk7CiAgICB9CiAgICBpbnQgcTsKICAgIHNjYW5mKCIlZCIsICZxKTsKICAgIGZvciAoaW50IGkgPSAwOyBpIDwgcTsgaSArKyl7CiAgICAgICAgaW50IHUsIHY7CiAgICAgICAgc2NhbmYoIiVkICVkIiwgJnUsICZ2KTsKICAgICAgICBMLmluc2VydF9xdWVyeSh1LCB2KTsKICAgIH0KICAgIEwuc29sdmUobik7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBxOyBpICsrKXsKICAgICAgICBwcmludGYoIiVkXG4iLCBMLlEuYXJjWzIqaS0xXS5yZXMpOwogICAgfQoJcmV0dXJuIDA7Cn0K