#include <bits/stdc++.h>
using namespace std;
struct ppp {
  int x, y, k;
} e[220000];
int cnt, n, m, i, j, k, bo[220000], bo2[220000], x, y, size[220000], fa[220000],
    d[220000], p, qn;
long long ans;
bool cmp(const ppp& a, const ppp& b) { return a.k < b.k; }
int get(int x) {
  while (fa[x]) x = fa[x];
  return x;
}
int main() {
  scanf("%d%d", &n, &m);
  for (i = 1; i <= m; ++i) scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].k);
  sort(e + 1, e + m + 1, cmp);
  for (i = 1; i <= n; ++i) size[i] = 1;
  p = n;
  for (i = 1; i <= m; ++i)
    if (get(x = e[i].x) != get(y = e[i].y)) {
      x = get(x);
      y = get(y);
      if (size[x] > size[y]) swap(x, y);
      fa[x] = y;
      size[y] += size[x];
      d[x] = e[i].k;
      ans += e[i].k;
      p--;
    }
  scanf("%d", &qn);
  for (i = 1; i <= qn; ++i) {
    scanf("%d%d", &x, &y);
    ++cnt;
    k = -1;
    for (j = x; j; k = max(k, d[j]), j = fa[j]) bo[j] = cnt, bo2[j] = k;
    k = -1;
    for (j = y; j; k = max(k, d[j]), j = fa[j])
      if (bo[j] == cnt) break;
    if (p > 2 || p == 2 && j)
      puts("-1");
    else if (p == 2)
      printf("%I64d\n", ans);
    else
      printf("%I64d\n", ans - max(k, bo2[j]));
  }
}
