#include <bits/stdc++.h>
using namespace std;
int n, m, h[100001], chk, sv[100001], mp = 16, LV[100001], q;
pair<int, int> dp[100001][17];
vector<pair<int, int> > e[100001];
pair<int, pair<int, int> > P[200001];
long long sum;
int H(int x) {
  if (h[x] != x) h[x] = H(h[x]);
  return h[x];
}
void dfs(int now, int head, int lv) {
  dp[now][0].first = head;
  LV[now] = lv;
  for (int i = 0; i < e[now].size(); i++)
    if (e[now][i].first != head) {
      dp[e[now][i].first][0].second = e[now][i].second;
      dfs(e[now][i].first, now, lv + 1);
    }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 0, u, v, w; i < m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    P[i] = make_pair(w, pair<int, int>(u, v));
  }
  sort(P, P + m);
  for (int i = 1; i <= n; i++) h[i] = i;
  for (int i = 0; i < m; i++) {
    pair<int, int> node = P[i].second;
    h[node.first] = H(node.first);
    h[node.second] = H(node.second);
    if (h[node.first] != h[node.second]) {
      sum += P[i].first;
      h[h[node.first]] = h[node.second];
      e[node.first].push_back(pair<int, int>(node.second, P[i].first));
      e[node.second].push_back(pair<int, int>(node.first, P[i].first));
    }
  }
  for (int i = 1; i <= n; i++) {
    h[i] = H(i);
    if (sv[h[i]] == 0) {
      chk++;
      sv[h[i]] = 1;
    }
  }
  if (chk == 1) {
    dfs(1, 0, 0);
    for (int i = 1; i <= mp; i++)
      for (int j = 1; j <= n; j++) {
        dp[j][i].first = dp[dp[j][i - 1].first][i - 1].first;
        dp[j][i].second =
            max(dp[dp[j][i - 1].first][i - 1].second, dp[j][i - 1].second);
      }
  }
  scanf("%d", &q);
  LV[0] = -1;
  for (int i = 0, u, v; i < q; i++) {
    scanf("%d%d", &u, &v);
    if (chk > 2)
      printf("-1\n");
    else if (chk == 2)
      printf("%I64d\n", (H(u) == H(v) ? -1ll : sum));
    else {
      int maxl = 0;
      if (LV[u] < LV[v]) swap(u, v);
      for (int r = mp; r >= 0; r--) {
        if (LV[dp[u][r].first] >= LV[v]) {
          maxl = max(maxl, dp[u][r].second);
          u = dp[u][r].first;
        }
      }
      for (int r = mp; r >= 0 && u != v; r--) {
        if (dp[u][r].first != dp[v][r].first) {
          maxl = max(maxl, dp[u][r].second);
          maxl = max(maxl, dp[v][r].second);
          u = dp[u][r].first;
          v = dp[v][r].first;
        }
      }
      if (u != v) {
        maxl = max(maxl, dp[u][0].second);
        maxl = max(maxl, dp[v][0].second);
        u = dp[u][0].first;
        v = dp[v][0].first;
      }
      printf("%I64d\n", sum - maxl);
    }
  }
  return 0;
}
