#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 100010, M = 200010;
struct edge {int a, b, c;} a[M]; bool cmp(edge a, edge b) {return a.c < b.c;} vector<pair<int, int> > e[N]; int n, m, dp[N][20], rt[N], dep[N], F[N][20]; bool vis[N];
int Get(int x) {return rt[x] == x ? x : rt[x] = Get(rt[x]);}
void kru() {
for(int i = 1; i <= n; i++) rt[i] = i; sort(a + 1, a + 1 + m, cmp);
for(int i = 1; i <= m; i++) {
int x = a[i].a, y = a[i].b, w = a[i].c; int fx = Get(x), fy = Get(y);
if(fx != fy) {rt[fy] = fx; e[fx].push_back(make_pair(fy, w)); e[fy].push_back(make_pair(fx, w));}
}
}
void dfs(int u, int fa) {vis[u] = 1; F[u][0] = fa; dep[u] = dep[fa] + 1; for(auto it: e[u]) {int v = it.first, w = it.second; if(v == fa) continue; dp[v][0] = w; dfs(v, u);}}
void RMQ() {for(int i = 1; i < 20; i++) for(int j = 1; j <= n; j++) F[j][i] = F[F[j][i - 1]][i - 1], dp[j][i] = max(dp[j][i - 1], dp[F[j][i - 1]][i - 1]);}
int ask(int x, int y) {
if(Get(x) != Get(y)) return -1; int ans = -2e9; if(dep[x] < dep[y]) swap(x, y); int delta = dep[x] - dep[y]; for(int i = 0; i < 19; i++) if(delta & (1 << i)) ans = max(ans, dp[x][i]), x = F[x][i];
if(x == y) return ans; for(int i = 19; i >= 0; i--) if(F[x][i] != F[y][i]) ans = max(ans, max(dp[x][i], dp[y][i])), x = F[x][i], y = F[y][i]; return max(ans, max(dp[x][0], dp[y][0]));
}
signed main() {
ios::sync_with_stdio(0); while(cin >> n >> m) {
for(int i = 1; i <= m; i++) cin >> a[i].a >> a[i].b >> a[i].c; kru(); for(int i = 1; i <= n; i++) vis[i] = 0;
for(int i = 1; i <= n; i++) if(!vis[i]) {dp[i][0] = -2e9; F[i][0] = i; dfs(i, 0);}
RMQ(); int q; cin >> q; while(q--) {int x, y; cin >> x >> y; int ans = ask(x, y);
if(ans == -1) cout << "impossible\n"; else cout << ans << '\n';}
}
return 0;
}