#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7;
int n, m, q, vis[N], dp[N][18], dep[N], ID[N];
long long dis[N], dis2[50][50], dis3[N][50];
vector<pair<int, int>> v[N];
vector<int> ExtraNodes;
vector<pair<int, pair<int, int>>> ExtraEdges;
void dfs(int u, int p, int d) {
  dp[u][0] = p;
  dep[u] = d;
  vis[u] = 1;
  for (int i = 0; i < v[u].size(); i++) {
    int x = v[u][i].first, c = v[u][i].second;
    if (x == p) continue;
    if (vis[x]) {
      ExtraEdges.push_back({u, {x, c}});
      ExtraNodes.push_back(u);
      ExtraNodes.push_back(x);
      continue;
    }
    dis[x] = dis[u] + c;
    dfs(x, u, d + 1);
  }
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  for (int i = 17; i >= 0; i--)
    if (dep[u] - (1 << i) >= dep[v]) u = dp[u][i];
  if (u == v) return u;
  for (int i = 17; i >= 0; i--) {
    if (dp[u][i] != dp[v][i]) u = dp[u][i], v = dp[v][i];
  }
  return dp[u][0];
}
long long getdis(int u, int v) { return dis[u] + dis[v] - 2 * dis[lca(u, v)]; }
int main() {
  scanf("%d%d", &n, &m);
  for (int a, b, c, i = 0; i < m; i++) {
    scanf("%d%d%d", &a, &b, &c);
    v[a].push_back({b, c});
    v[b].push_back({a, c});
  }
  dfs(1, -1, 0);
  for (int k = 1; k < 18; k++)
    for (int i = 1; i <= n; i++)
      if (dp[i][k - 1] == -1)
        dp[i][k] = -1;
      else
        dp[i][k] = dp[dp[i][k - 1]][k - 1];
  sort(ExtraNodes.begin(), ExtraNodes.end());
  ExtraNodes.resize(unique(ExtraNodes.begin(), ExtraNodes.end()) -
                    ExtraNodes.begin());
  for (int i = 0; i < ExtraNodes.size(); i++) {
    ID[ExtraNodes[i]] = i;
  }
  for (int i = 0; i < ExtraNodes.size(); i++) {
    for (int j = 0; j < ExtraNodes.size(); j++) {
      dis2[i][j] = getdis(ExtraNodes[i], ExtraNodes[j]);
    }
  }
  for (int i = 0; i < ExtraEdges.size(); i++) {
    int u = ExtraEdges[i].first, v = ExtraEdges[i].second.first,
        c = ExtraEdges[i].second.second;
    dis2[ID[u]][ID[v]] = min(dis2[ID[u]][ID[v]], c * 1LL);
    dis2[ID[v]][ID[u]] = dis2[ID[u]][ID[v]];
  }
  for (int k = 0; k < ExtraNodes.size(); k++)
    for (int i = 0; i < ExtraNodes.size(); i++)
      for (int j = 0; j < ExtraNodes.size(); j++)
        dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 0; j < ExtraNodes.size(); j++)
      dis3[i][j] = getdis(i, ExtraNodes[j]);
  int q;
  scanf("%d", &q);
  while (q--) {
    int a, b;
    scanf("%d%d", &a, &b);
    long long ans = getdis(a, b);
    for (int i = 0; i < ExtraNodes.size(); i++)
      for (int j = 0; j < ExtraNodes.size(); j++) {
        ans = min(ans, dis3[a][i] + dis2[i][j] + dis3[b][j]);
      }
    cout << ans << endl;
  }
}
