#include <bits/stdc++.h>
using namespace std;
const int N = 100000;
int n, e;
pair<pair<int, int>, int> E[N];
int dp[17][N];
long long d[42][42];
vector<int> who;
vector<pair<pair<int, int>, int> > sp;
vector<vector<pair<int, int> > > g;
int id[N], I;
long long dist[N][42];
int p[N];
int find(int u) {
  if (p[u] == u) return u;
  return p[u] = find(p[u]);
}
void add(int u) {
  if (id[u] != -1) return;
  id[u] = I++;
  who.push_back(u);
}
long long w[N];
int D[N];
void DFS(int u, int p) {
  dp[0][u] = p;
  for (auto v : g[u])
    if (v.first != p) {
      w[v.first] = w[u] + v.second;
      D[v.first] = D[u] + 1;
      DFS(v.first, u);
    }
}
int LCA(int u, int v) {
  if (D[u] < D[v]) swap(u, v);
  int l = 0;
  while ((1 << l) <= D[u]) ++l;
  --l;
  for (int i = l; i >= 0; --i)
    if (D[u] - (1 << i) >= D[v]) u = dp[i][u];
  if (u == v) return u;
  for (int i = l; i >= 0; --i)
    if (dp[i][u] != dp[i][v]) {
      u = dp[i][u];
      v = dp[i][v];
    }
  return dp[0][u];
}
int main() {
  scanf("%d%d", &n, &e);
  g.resize(n);
  memset(id, -1, sizeof(id));
  for (int i = 0; i < n; ++i) p[i] = i;
  for (int i = 0; i < e; ++i) {
    scanf("%d%d%d", &E[i].first.first, &E[i].first.second, &E[i].second);
    --E[i].first.first;
    --E[i].first.second;
    int u = find(E[i].first.first);
    int v = find(E[i].first.second);
    if (u != v) {
      p[u] = v;
      g[E[i].first.first].push_back({E[i].first.second, E[i].second});
      g[E[i].first.second].push_back({E[i].first.first, E[i].second});
    } else {
      add(E[i].first.first);
      add(E[i].first.second);
      sp.push_back(E[i]);
    }
  }
  memset(dp, -1, sizeof(dp));
  DFS(0, -1);
  for (int k = 1; k < 17; ++k)
    for (int u = 0; u < n; ++u)
      if (dp[k - 1][u] != -1) dp[k][u] = dp[k - 1][dp[k - 1][u]];
  for (int i = 0; i < I; ++i)
    for (int j = i + 1; j < I; ++j) {
      int u = who[i];
      int v = who[j];
      int c = LCA(u, v);
      d[i][j] = d[j][i] = w[u] + w[v] - 2 * w[c];
    }
  for (auto e : sp) {
    int u = id[e.first.first];
    int v = id[e.first.second];
    int c = e.second;
    d[u][v] = d[v][u] = min(d[u][v], (long long)c);
  }
  for (int k = 0; k < I; ++k)
    for (int i = 0; i < I; ++i)
      for (int j = 0; j < I; ++j) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < I; ++j) {
      int u = i;
      int v = who[j];
      int c = LCA(u, v);
      dist[i][j] = w[u] + w[v] - 2 * w[c];
    }
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < I; ++j)
      for (int k = 0; k < I; ++k)
        dist[i][j] = min(dist[i][j], dist[i][k] + d[j][k]);
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v;
    scanf("%d%d", &u, &v);
    --u;
    --v;
    int c = LCA(u, v);
    long long res = w[u] + w[v] - 2 * w[c];
    if (id[u] != -1) res = min(res, dist[v][id[u]]);
    if (id[v] != -1) res = min(res, dist[u][id[v]]);
    for (int i = 0; i < I; ++i)
      for (int j = 0; j < I; ++j)
        res = min(res, dist[u][i] + dist[v][j] + d[i][j]);
    printf("%lld\n", res);
  }
  return 0;
}
