#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 10, MN = 1e5 + 10, LG = 18;
int par[LG][MN];
long long e[44][44], dist[44][MN];
int h[MN], es[MN], ee[MN], el[MN];
bool mk[MN], is[MN];
long long d[MN];
vector<int> V;
vector<pair<int, int> > adj[MN];
int n, m;
int lca(int u, int v) {
  if (h[u] > h[v]) swap(u, v);
  int t = h[v] - h[u];
  for (int i = 0; i < LG; i++)
    if (t & 1 << i) v = par[i][v];
  if (u == v) return u;
  for (int i = LG - 1; ~i; --i)
    if (par[i][u] ^ par[i][v]) u = par[i][u], v = par[i][v];
  return par[0][v];
}
void dfs(int s) {
  mk[s] = true;
  for (auto x : adj[s]) {
    int e = x.second, v = x.first;
    if (mk[v]) continue;
    is[e] = true;
    par[0][v] = s;
    h[v] = h[s] + 1;
    d[v] = d[s] + el[e];
    for (int i = 1; i < LG; i++) par[i][v] = par[i - 1][par[i - 1][v]];
    dfs(v);
  }
}
long long getDist(int u, int v) { return d[u] + d[v] - 2 * d[lca(u, v)]; }
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    cin >> es[i] >> ee[i] >> el[i];
    --es[i], --ee[i];
    adj[es[i]].push_back({ee[i], i});
    adj[ee[i]].push_back({es[i], i});
  }
  dfs(0);
  for (int i = 0; i < m; ++i)
    if (!is[i]) {
      V.push_back(es[i]);
      V.push_back(ee[i]);
    }
  sort(V.begin(), V.end());
  V.resize(unique(V.begin(), V.end()) - V.begin());
  for (int i = 0; i < ((int)(V).size()); i++)
    for (int j = 0; j < n; j++) dist[i][j] = getDist(V[i], j);
  for (int i = 0; i < ((int)(V).size()); i++)
    for (int j = 0; j < ((int)(V).size()); j++) e[i][j] = dist[i][V[j]];
  for (int i = 0; i < m; ++i)
    if (!is[i]) {
      for (int a = 0; a < ((int)(V).size()); a++)
        for (int b = 0; b < ((int)(V).size()); b++) {
          if (V[a] == es[i] && V[b] == ee[i]) {
            e[a][b] = min(e[a][b], 1ll * el[i]);
            e[b][a] = min(e[b][a], 1ll * el[i]);
          }
          if (V[a] == ee[i] && V[b] == es[i]) {
            e[a][b] = min(e[a][b], 1ll * el[i]);
            e[b][a] = min(e[b][a], 1ll * el[i]);
          }
        }
    }
  for (int k = 0; k < ((int)(V).size()); k++)
    for (int i = 0; i < ((int)(V).size()); i++)
      for (int j = 0; j < ((int)(V).size()); j++)
        e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    long long ans = getDist(u, v);
    for (int a = 0; a < ((int)(V).size()); a++)
      for (int b = 0; b < ((int)(V).size()); b++)
        ans = min(ans, dist[a][u] + e[a][b] + dist[b][v]);
    cout << ans << '\n';
  }
  return 0;
}
