#include <bits/stdc++.h>
using namespace std;
inline int read() {
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9') if (c == '-') flag = true;
int res = c - '0';
while ((c = getchar()) >= '0' && c <= '9') res = (res << 3) + (res << 1) + c - '0';
return flag ? -res : res;
}
const int maxn = 5e5 + 10;
int n, m, fa[maxn][21], dep[maxn], a[maxn], ans, f[maxn], pa[maxn], minn[maxn][21], vis[maxn], root[maxn];
struct Edge {
int to, w, next, from;
} edge[maxn << 1], e[maxn << 1];
int head[maxn << 1], cnt;
bool cmp(Edge e1, Edge e2) { return e1.w > e2.w; }
void init() {
for (int i = 0; i <= n; i++) head[i] = -1, pa[i] = i;
cnt = 0;
}
void add_edge(int u, int v, int w) {
edge[cnt].to = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
}
void dfs(int x) {
for (int j = 1; j <= 20; ++j) {
fa[x][j] = fa[fa[x][j - 1]][j - 1];
minn[x][j] = min(minn[x][j - 1], minn[fa[x][j - 1]][j - 1]);
}
for (int i = head[x]; ~i; i = edge[i].next) {
int y = edge[i].to;
if (vis[y]) continue;
minn[y][0] = edge[i].w;
dep[y] = dep[x] + 1;
fa[y][0] = x;
vis[y] = 1;
dfs(y);
}
}
int lca(int x, int y) {
if (dep[y] > dep[x]) swap(x, y);
int res = 0x3f3f3f3f;
for (int i = 20; i >= 0; --i) {
if (dep[x] - dep[y] >= (1 << i)) res = min(res, minn[x][i]), x = fa[x][i];
}
for (int i = 20; i >= 0; --i) {
if (fa[x][i] != fa[y][i]) {
res = min(res, min(minn[x][i], minn[y][i]));
x = fa[x][i], y = fa[y][i];
}
}
return min(res, min(minn[x][0], minn[y][0]));
}
int find(int x) {
return pa[x] == x ? x : pa[x] = find(pa[x]);
}
int main() {
n = read();
m = read();
init();
for (int i = 1; i <= m; ++i) {
e[i].from = read();
e[i].to = read();
e[i].w = read();
}
sort(e + 1, e + m + 1, cmp);
for (int i = 1; i <= m; ++i) {
if (find(e[i].from) == find(e[i].to)) continue;
add_edge(e[i].from, e[i].to, e[i].w);
add_edge(e[i].to, e[i].from, e[i].w);
pa[find(e[i].from)] = find(e[i].to);
}
/*for (int i = 1; i <= m; ++i) {
if(find(e[i].to)!= find(e[i].from)) pa[find(e[i].to)]=find(e[i].from);
}*/
for (int i = 1; i <= n; ++i) {
if (!vis[i]) {
vis[i] = 1;
dep[i] = 1;
root[i] = 1;
minn[i][0] = 0x3f3f3f3f;
dfs(i);
}
}
int q = read();
while (q--) {
int x = read(), y = read();
if (find(x) != find(y)) puts("-1");
else printf("%d\n", lca(x, y));
}
return 0;
}