#include <bits/stdc++.h>
using namespace std;
int n;
int m;
struct edge {
  int x, y, cost;
} e[200008];
bool operator<(edge a, edge b) { return a.cost < b.cost; }
int pa[200008][18 + 5];
int maxi[200008][18 + 5];
vector<int> v[200008], c[200008];
int depth[200008];
long long ans;
int bel[200008];
int trees[200008];
int fin(int x) {
  if (bel[x] == x) return x;
  return bel[x] = fin(bel[x]);
}
int lca(int x, int y) {
  int res = 0;
  if (depth[x] < depth[y]) swap(x, y);
  for (int i = 18; i >= 0; i--) {
    if (depth[pa[x][i]] >= depth[y]) {
      res = max(res, maxi[x][i]);
      x = pa[x][i];
    }
  }
  if (x == y) return res;
  for (int i = 18; i >= 0; i--) {
    if (pa[x][i] != pa[y][i]) {
      res = max(res, maxi[x][i]);
      res = max(res, maxi[y][i]);
      x = pa[x][i], y = pa[y][i];
    }
  }
  res = max(res, maxi[x][0]);
  res = max(res, maxi[y][0]);
  return res;
}
void dfs(int x, int p) {
  pa[x][0] = p;
  depth[x] = depth[p] + 1;
  for (int i = 0; i < v[x].size(); i++) {
    int t = v[x][i];
    if (t == p) continue;
    maxi[t][0] = c[x][i];
    dfs(t, x);
  }
}
int main() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].cost);
  sort(e + 1, e + m + 1);
  for (int i = 1; i <= n; i++) bel[i] = i;
  for (int i = 1; i <= m; i++) {
    int x = e[i].x, y = e[i].y, cost = e[i].cost;
    if (fin(x) == fin(y)) continue;
    v[x].push_back(y);
    v[y].push_back(x);
    c[x].push_back(cost);
    c[y].push_back(cost);
    bel[fin(x)] = fin(y);
    ans += cost;
  }
  for (int i = 1; i <= n; i++) trees[fin(i)] = 1;
  int tots = 0;
  for (int i = 1; i <= n; i++) tots += trees[i];
  int T;
  cin >> T;
  if (tots == 1) {
    dfs(1, 0);
    for (int j = 1; j <= 18; j++) {
      for (int i = 1; i <= n; i++) {
        int t = pa[i][j - 1];
        pa[i][j] = pa[t][j - 1];
        maxi[i][j] = max(maxi[i][j - 1], maxi[t][j - 1]);
      }
    }
  }
  while (T--) {
    int x, y;
    scanf("%d%d", &x, &y);
    if (tots == 1) {
      printf("%lld\n", ans - (lca(x, y)));
    } else {
      if (fin(x) != fin(y) and tots == 2)
        printf("%lld\n", ans);
      else
        printf("-1\n");
    }
  }
  return 0;
}
