#include<cstdio>
#include<algorithm>
using namespace std;
struct node {
int from,to,dis,next;
};
struct node usee[60001];
struct node edge[20001];
int n,m,head[10001],num=0;
int f[10001],bnum=0,tail=0;
int cur=0,dfn[10001],tarjan_dfn=0;
int max(int a,int b){return a>b?a:b;}
int min(int a,int b){return a<b?a:b;}
void tarjan(int scre,int before)
{
tarjan_dfn++;
dfn[scre]=tarjan_dfn;
for(int i=head[scre];i!=0;i=edge[i].next)
{
if(edge[i].to!=before)
{
tarjan(edge[i].to,scre);
}
}
}
int tarjan_lca(int minn,int maxn)
{
int kop=minn,kol=maxn,kcc=1e18;
while(dfn[kol]>dfn[minn])
{
for(int i=head[kol];i!=0;i=edge[i].next)
{
if(dfn[edge[i].to]<dfn[kol])
{
kol=edge[i].to;
kcc=min(kcc,edge[i].dis);
break;
}
}
}
while(dfn[kop]>dfn[kol])
{
for(int i=head[kop];i!=0;i=edge[i].next)
{
if(dfn[edge[i].to]<dfn[kop])
{
kop=edge[i].to;
kcc=min(kcc,edge[i].dis);
break;
}
}
}
return kcc;
}
int cmp(struct node a,struct node b){return a.dis>b.dis;}
void edge_add(int from,int to,int dis)
{
num++;
edge[num].to=to;
edge[num].dis=dis;
edge[num].from=from;
edge[num].next=head[from];
head[from]=num;
}
int find(int x)
{
if(x==f[x]) return f[x];
else return f[x]=find(f[x]);
}
int qread()
{
int x=0;char ch=getchar();
while(ch>‘9‘||ch<‘0‘) ch=getchar();
while(ch<=‘9‘&&ch>=‘0‘){x=x*10+(int)(ch-‘0‘);ch=getchar();}
return x;
}
int main()
{
n=qread(),m=qread();
for(int i=1;i<n;i++)
{
f[i]=i;
bnum++;
usee[bnum].from=i;
usee[bnum].to=i+1;
usee[bnum].dis=-1;
}
f[n]=n;
int from,to,dis;
for(int i=1;i<=m;i++)
{
from=qread(),to=qread(),dis=qread();
bnum++;
usee[bnum].to=to;
usee[bnum].dis=dis;
usee[bnum].from=from;
}
sort(usee+1,usee+bnum+1,cmp);
int x,y;
while(tail<n-1)
{
cur++;
x=find(usee[cur].from),y=find(usee[cur].to);
if(x!=y)
{
tail++;
edge_add(usee[cur].from,usee[cur].to,usee[cur].dis);
edge_add(usee[cur].to,usee[cur].from,usee[cur].dis);
f[x]=y;
}
}
tarjan(1,0);
int q=qread();
while(q--)
{
from=qread(),to=qread();
dfn[from]<dfn[to]?printf("%d\n",tarjan_lca(from,to)):printf("%d\n",tarjan_lca(to,from));
}
return 0;
}