#include <bits/stdc++.h>
using namespace std;
int sz[100000], p[100000];
int lv[100000], cnt;
int fa[100000][17];
int dp[100000][17];
long long sum;
pair<int, pair<int, int> > a[200000];
vector<pair<int, int> > e[100000];
int root(int x) { return p[x] != x ? p[x] = root(p[x]) : x; }
long long gao(int x, int y) {
  if (cnt >= 3) return -1;
  if (cnt == 2) return root(x) == root(y) ? -1 : sum;
  int ret = 0, c = lv[x] - lv[y];
  if (c > 0)
    for (int i = 16; ~i; i--)
      if (+c & 1 << i) ret = max(ret, dp[x][i]), x = fa[x][i];
  if (c < 0)
    for (int i = 16; ~i; i--)
      if (-c & 1 << i) ret = max(ret, dp[y][i]), y = fa[y][i];
  if (x == y) return sum - ret;
  for (int i = 16; ~i; i--)
    if (fa[x][i] != fa[y][i]) {
      ret = max(ret, dp[x][i]), x = fa[x][i];
      ret = max(ret, dp[y][i]), y = fa[y][i];
    }
  ret = max(ret, dp[x][0]);
  ret = max(ret, dp[y][0]);
  return sum - ret;
}
void make(int x) {
  for (int i = 1; i < 17; i++) {
    fa[x][i] = fa[fa[x][i - 1]][i - 1];
    dp[x][i] = dp[fa[x][i - 1]][i - 1];
    if (dp[x][i] < dp[x][i - 1]) dp[x][i] = dp[x][i - 1];
  }
  for (auto& r : e[x]) {
    if (r.first == fa[x][0]) continue;
    lv[r.first] = lv[x] + 1;
    fa[r.first][0] = x;
    dp[r.first][0] = r.second;
    make(r.first);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < n; i++) cnt += sz[p[i] = i] = 1;
  for (int i = 0; i < m; i++) {
    int x, y, c;
    scanf("%d%d%d", &x, &y, &c);
    a[i] = {c, {x - 1, y - 1}};
  }
  sort(a, a + m);
  for (int i = 0; i < m; i++) {
    int u, v;
    tie(u, v) = a[i].second;
    int x = root(u), y = root(v);
    if (x == y) continue;
    if (sz[x] <= sz[y])
      sz[p[x] = y] += sz[x];
    else
      sz[p[y] = x] += sz[y];
    cnt--, sum += a[i].first;
    e[u].push_back({v, a[i].first});
    e[v].push_back({u, a[i].first});
  }
  if (cnt == 1) make(0);
  scanf("%d", &m);
  while (m--) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%I64d\n", gao(--x, --y));
  }
}
