1 #include<bits/stdc++.h>
2 using namespace std;
3 #define maxn 10010
4 #define maxm 50010
5 struct Edge{
6 int from,to,value,next;
7 bool operator < (const Edge& a)const {
8 return value>a.value;}
9 }e[maxm],ee[maxm];
10 int n,m,fa[maxn],f[maxn][21],deep[maxn],temp;
11 int head[maxn],tot,Q,num,dis[maxm][21];
12 void Built_Map(){
13 for(int i=1,x,y,w;i<=m;i++){
14 scanf("%d%d%d",&x,&y,&w);
15 e[i].from=x;e[i].to=y;e[i].value=w;
16 }
17 }
18
19 int find(int x){
20 if(x==fa[x]) return x;
21 else return fa[x]=find(fa[x]);
22 }
23 void Add_Eedge(int u,int v,int w){
24 ee[++num].from=u;ee[num].value=w;ee[num].to=v;
25 ee[num].next=head[u];head[u]=num;
26 }
27 void Kursual(){
28 sort(e+1,e+m+1);int cur=0;
29 for(int i=1;i<=n;i++) fa[i]=i;
30 for(int i=1;i<=m;i++){
31 int u=e[i].from,v=e[i].to;
32 int rx=find(u),ry=find(v);
33 if(rx != ry){
34 cur++;fa[rx]=ry;
35 Add_Eedge(u,v,e[i].value);
36 Add_Eedge(v,u,e[i].value);
37 }
38 if(cur == n-1)break;
39 }
40 }
41 void DFS(int now,int from,int deepth,int ktwo){
42 f[now][0]=from;deep[now]=deepth;
43 dis[now][0]=ktwo;
44 for(int i=head[now];i;i=ee[i].next)
45 if(ee[i].to!=from)
46 DFS(ee[i].to,now,deepth+1,ee[i].value);
47 }
48 void Get_Fa(){
49 for(int i=1;i<=17;i++)
50 for(int j=1;j<=n;j++){
51 f[j][i]=f[ f[j][i-1] ][i-1];
52 dis[j][i]=min(dis[j][i],dis[f[j][i-1]][i-1]);
53 dis[j][i]=min(dis[j][i],dis[j][i-1]);
54 }
55 }
56 int get_same(int a,int t){
57 for(int i=0;i<17;i++)
58 if(t&(1<<i)){
59 temp=min(temp,dis[a][i]);
60 a=f[a][i];
61 }
62 return a;
63 }
64 int LCA_Judge(int u,int v){
65 if(u!=1&&deep[u]==0) return -1;
66 if(v!=1&&deep[v]==0) return -1;
67 temp=0x3f3f3f3f;
68 if(deep[u]<deep[v]) swap(u,v);
69 u=get_same(u,deep[u]-deep[v]);
70 if(u==v) return temp;
71 for(int i=17;i>=0;i--){
72 if(f[u][i]!=f[v][i]){
73 temp=min(temp,dis[u][i]);
74 temp=min(temp,dis[v][i]);
75 u=f[u][i];v=f[v][i];
76 }
77
78 }
79 temp=min(temp,dis[u][0]);
80 temp=min(temp,dis[v][0]);
81 return temp;
82 }
83 int main()
84 {
85 scanf("%d%d",&n,&m);
86 memset(dis,0x3f,sizeof dis);
87 Built_Map();
88 Kursual();// 生成一张最大生成树 保存在ee 边表中
89 DFS(1,1,0,0);dis[1][0]=0x3f3f3f3f;
90 Get_Fa();
91 scanf("%d",&Q);
92 for(int i=1,x,y;i<=Q;i++){
93 scanf("%d%d",&x,&y);
94 printf("%d\n",LCA_Judge(x,y));
95 }
96 return 0;
97 }