#include <bits/stdc++.h>
using namespace std;
struct Edge {
  int to;
  long long cost;
  bool operator<(const Edge& o) const {
    return tie(cost, to) > tie(o.cost, o.to);
  }
  bool operator!=(const Edge& o) const {
    return tie(cost, to) != tie(o.cost, o.to);
  }
};
vector<Edge> v[100005], tv[100005];
int cnt = 1;
int ac[100005][20], level[100005];
map<int, int> mp;
long long dp[50][100005];
long long dist[100005][20];
long long INF = 0x3232323232323232;
priority_queue<Edge> pq;
void dijkstra(int S) {
  int i = mp[S];
  if (dp[i][S] != INF) return;
  dp[i][S] = 0;
  pq.push({S, 0});
  while (!pq.empty()) {
    Edge cur = pq.top();
    pq.pop();
    if (dp[i][cur.to] < cur.cost) continue;
    for (Edge nxt : v[cur.to]) {
      if (dp[i][nxt.to] > cur.cost + nxt.cost) {
        dp[i][nxt.to] = cur.cost + nxt.cost;
        pq.push({nxt.to, dp[i][nxt.to]});
      }
    }
  }
}
void dfs(int cur, int lev) {
  level[cur] = lev;
  for (Edge nxt : v[cur]) {
    if (!level[nxt.to]) {
      tv[cur].push_back(nxt);
      tv[nxt.to].push_back({cur, nxt.cost});
      dfs(nxt.to, lev + 1);
      ac[nxt.to][0] = cur;
      dist[nxt.to][0] = nxt.cost;
    }
  }
}
long long lca(int a, int b) {
  if (level[a] < level[b]) swap(a, b);
  long long ret = 0, gap = level[a] - level[b];
  for (int i = 0; gap; i++, gap >>= 1) {
    if (gap & 1) {
      ret += dist[a][i];
      a = ac[a][i];
    }
  }
  if (a == b) return ret;
  for (int i = 18; i >= 0; i--) {
    if (ac[a][i] != ac[b][i]) {
      ret += dist[a][i] + dist[b][i];
      a = ac[a][i];
      b = ac[b][i];
    }
  }
  return ret += dist[a][0] + dist[b][0];
}
int main() {
  memset(dp, 50, sizeof dp);
  int N, M, Q, a, b, c;
  scanf("%d %d", &N, &M);
  while (M--) {
    scanf("%d %d %d", &a, &b, &c);
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  dfs(1, 1);
  for (int i = 1; i <= N; i++) sort(v[i].begin(), v[i].end());
  for (int i = 1; i <= N; i++) sort(tv[i].begin(), tv[i].end());
  for (int i = 1; i < 19; i++) {
    for (int j = 1; j <= N; j++) {
      ac[j][i] = ac[ac[j][i - 1]][i - 1];
      dist[j][i] = dist[j][i - 1] + dist[ac[j][i - 1]][i - 1];
    }
  }
  for (int i = 1; i <= N; i++) {
    if (v[i].size() != tv[i].size()) {
      for (int j = 0, k = 0; j < v[i].size(); j++, k++) {
        if (v[i][j] != tv[i][k]) {
          Edge edge = v[i][j];
          if (!mp[i]) mp[i] = cnt++;
          if (!mp[edge.to]) mp[edge.to] = cnt++;
          dijkstra(i);
          dijkstra(edge.to);
          k--;
        }
      }
    }
  }
  scanf("%d", &Q);
  while (Q--) {
    scanf("%d %d", &a, &b);
    long long ans = lca(a, b);
    for (auto it : mp) {
      ans = min(ans, dp[it.second][a] + dp[it.second][b]);
    }
    printf("%lld\n", ans);
  }
}
