intmain() { int n,m; while (scanf("%d%d",&n,&m)!=EOF) { for (int i=1;i<=m;i++) scanf("%d%d%d",&a[i].a,&a[i].b,&a[i].c); sort(&a[1],&a[m+1],op); int q; scanf("%d",&q); for (int i=1;i<=q;i++) { int t1,t2; scanf("%d%d",&t1,&t2); int minn,maxn,ans=2147483647; for (int j=1;j<=m;j++) { minn=2147483647; maxn=0; clean_father(n); for (int k=j;k<=m;k++) if (getfather(a[k].a)!=getfather(a[k].b)) { link(a[k].a,a[k].b); if (minn>a[k].c) minn=a[k].c; if (maxn<a[k].c) maxn=a[k].c; if (maxn-minn>ans) break; if (getfather(t1)==getfather(t2)) { if (ans>maxn-minn) { ans=maxn-minn; break; } } } } if (ans!=2147483647) printf("%d\n",ans); elseprintf("-1\n"); } } return0; }