#include <bits/stdc++.h>
using namespace std;
int n, m, par[100005], h[100005], pa[100005][17], cnt;
long long ans, d[100005], da[105], db[105];
vector<pair<int, int>> v[100005];
vector<int> hv;
vector<vector<long long>> dd;
vector<pair<pair<int, int>, int>> vv;
int ta, tb, tc;
int find(int a) { return par[a] == a ? a : par[a] = find(par[a]); }
void dfs(int a) {
  for (int i = 1; i < 17; i++) pa[a][i] = pa[pa[a][i - 1]][i - 1];
  for (auto& p : v[a])
    if (p.first != pa[a][0]) {
      h[p.first] = h[a] + 1;
      d[p.first] = d[a] + p.second;
      pa[p.first][0] = a;
      dfs(p.first);
    }
}
inline int lca(int a, int b) {
  if (h[a] > h[b]) swap(a, b);
  tc = h[b] - h[a];
  for (int i = 0; i < 17; i++)
    if (tc & (1 << i)) b = pa[b][i];
  if (a == b) return a;
  for (int i = 16; i >= 0; i--)
    if (pa[a][i] != pa[b][i]) {
      a = pa[a][i];
      b = pa[b][i];
    }
  return pa[a][0];
}
inline long long dis(int a, int b) { return d[a] + d[b] - d[lca(a, b)] * 2; }
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) par[i] = i;
  while (m--) {
    scanf("%d%d%d", &ta, &tb, &tc);
    if (find(ta) == find(tb)) {
      hv.push_back(ta);
      hv.push_back(tb);
      vv.push_back({{ta, tb}, tc});
    } else {
      par[par[ta]] = par[tb];
      v[ta].push_back({tb, tc});
      v[tb].push_back({ta, tc});
    }
  }
  dfs(1);
  sort(hv.begin(), hv.end());
  auto hve = unique(hv.begin(), hv.end());
  cnt = hve - hv.begin();
  dd = vector<vector<long long>>(cnt, vector<long long>(cnt));
  for (int i = 0; i < cnt; i++)
    for (int j = 0; j < cnt; j++) dd[i][j] = dis(hv[i], hv[j]);
  for (auto& p : vv) {
    ta = lower_bound(hv.begin(), hve, p.first.first) - hv.begin();
    tb = lower_bound(hv.begin(), hve, p.first.second) - hv.begin();
    dd[ta][tb] = min(dd[ta][tb], (long long)p.second);
    dd[tb][ta] = min(dd[tb][ta], (long long)p.second);
  }
  for (int i = 0; i < cnt; i++)
    for (int j = 0; j < cnt; j++)
      for (int k = 0; k < cnt; k++)
        dd[j][k] = min(dd[j][k], dd[j][i] + dd[i][k]);
  scanf("%d", &m);
  while (m--) {
    scanf("%d%d", &ta, &tb);
    ans = dis(ta, tb);
    for (int i = 0; i < cnt; i++) {
      da[i] = dis(ta, hv[i]);
      db[i] = dis(tb, hv[i]);
    }
    for (int i = 0; i < cnt; i++)
      for (int j = 0; j < cnt; j++)
        if (i != j) ans = min(ans, da[i] + dd[i][j] + db[j]);
    printf("%lld\n", ans);
  }
  return 0;
}
