#include <bits/stdc++.h>
using namespace std;
#define usd unsigned
#define int long long
#define endl '\n'
#define el putchar('\n')
#define AC return
#define AK return 0
#define lowbit(x) (x&(-x))
#define ls(i) 2*i
#define rs(i) 2*i+1
#define pii pair<int,int>
int read() {int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;
}
void main_();
signed main() {
int t=1;
while(t--) main_();
AK;
}
const int maxn=5*1e4+18,mxx=1e4+114,infi=1e9;
int n,m,q,fa[maxn],tot,f[mxx][33],pos[mxx][33],dep[maxn];
map<long,map<long,long> > mp;
int father(int x) {
if(fa[x]==x) return x;
else {
fa[x]=father(fa[x]);
return fa[x];
}
}
void merge(int a,int b) {
fa[father(a)]=fa[father(b)];
}
struct edge{
int u,v,w;
}e[maxn];
bool operator<(edge a,edge b) {
return a.w<b.w;
}
priority_queue<edge> pq;
vector<pii> vec[maxn];
void dfs(int i,int from) {
dep[i]=dep[from]+1;
f[i][0]=from;
for(auto tmp:vec[i]) {
int to=tmp.first,mx=tmp.second;
if(to==from) continue;
pos[to][0]=mx;
dfs(to,i);
}
}
int lca(int x,int y) {
if(dep[x]>dep[y]) swap(x,y);
int ret=infi;
for(int i=30;i>=0;i--) if(dep[x]<=dep[y]-(1<<i)) ret=min(ret,pos[y][i]),y=f[y][i];
if(x==y) return ret;
for(int i=30;i>=0;i--) if(f[x][i]!=f[y][i]) ret=min(ret,min(pos[x][i],pos[y][i])),x=f[x][i],y=f[y][i];
ret=min(ret,min(pos[x][0],pos[y][0]));
return ret;
}
void main_() {
memset(pos,0x3f3f3f3f,sizeof(pos));
n=read(),m=read();
for(int i=1;i<=m;i++) {
e[i].u=read(),e[i].v=read(),e[i].w=read();
pq.push(e[i]);
}
for(int i=1;i<=n;i++) fa[i]=i;
int flag=0;
while(!pq.empty()) {
auto top=pq.top();pq.pop();
if(father(top.u)==father(top.v)) continue;
merge(top.u,top.v);
vec[top.u].push_back({top.v,top.w});
vec[top.v].push_back({top.u,top.w});
flag++;
if(flag==n-1) break;
}
for(int i=1;i<=n;i++) if(dep[i]==0) dfs(i,i);
for(int i=1;i<=n;i++) {
for(int j=1;j<=30;j++) {
f[i][j]=f[f[i][j-1]][j-1];
pos[i][j]=min(pos[i][j-1],pos[f[i][j-1]][j-1]);
}
}
q=read();
while(q--) {
int x=read(),y=read();
if(father(x)!=father(y)) {
cout<<-1;el;continue;
}
else cout<<lca(x,y);el;
}
}