#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 1005
#define INF 0x1f1f1f1f
struct tt{
int x,y,sp;
}p[N];
int pre[N];
bool cmp(tt a, tt b)
{
return a.sp<b.sp;
}
int find(int x)
{
int r = x;
while(x!=pre[x]) x = pre[x];
while(r!=x)
{
int j = pre[r];
pre[r] = x;
r = j;
}
return x;
}
int main()
{
int n,q,i,j,s,t,m;
while(~scanf("%d %d",&n,&m))
{
for(i = 0 ; i < m ; i++)
scanf("%d %d %d",&p[i].x,&p[i].y,&p[i].sp);
sort(p,p+m,cmp);
scanf("%d",&q);
while(q--)
{ int minx = INF;
scanf("%d %d",&s,&t);
for(i = 0 ; i < m ; i++)
{
for(j = 0 ; j <= n ; j++) pre[j] = j;
for(j = i ; j < m ; j++)
{
int fx = find(p[j].x);
int fy = find(p[j].y);
if(fx!=fy) pre[fx] = fy;
if(find(s) == find(t)) minx = min(minx,p[j].sp - p[i].sp);
}
}
if(minx!=INF) printf("%d\n",minx);
else printf("-1\n");
}
}
return 0;
}