#include <bits/stdc++.h>
template <class T>
T read(T &a) {
  a = 0;
  char x = getchar();
  bool f = 0;
  for (; x < '0' || x > '9'; x = getchar()) f |= x == '-';
  for (; x >= '0' && x <= '9'; x = getchar()) a = (a << 3) + (a << 1) + x - '0';
  if (f) a = -a;
  return a;
}
using namespace std;
const int N = 1e5 + 5;
int n, m, f[N], nnext[N << 1], v[N << 1], cnt, p[N], w[N << 1];
bool chk[N << 1];
int jmp[N][18], dep[N];
long long val[N], dis[25][N];
vector<int> pre;
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    q;
int find(int a) { return a == p[a] ? a : p[a] = find(p[a]); }
void add(int a, int b, int c) {
  nnext[++cnt] = f[a];
  f[a] = cnt;
  v[cnt] = b;
  w[cnt] = c;
}
void dfs(int u, int fa) {
  for (int i = 0, j = fa; i <= 17; ++i) {
    jmp[u][i] = j;
    j = jmp[j][i];
  }
  for (int i = f[u]; i; i = nnext[i]) {
    int to = v[i];
    if (chk[i] || to == fa) continue;
    dep[to] = dep[u] + 1;
    val[to] = val[u] + w[i];
    dfs(to, u);
  }
}
int lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  while (a != b) {
    if (dep[a] == dep[b]) {
      for (int i = 1; i <= 17; ++i) {
        if (jmp[a][i] == jmp[b][i]) {
          a = jmp[a][i - 1];
          b = jmp[b][i - 1];
          break;
        }
      }
    } else {
      int ned = dep[a] - dep[b];
      for (int i = 1, cnt = 0; cnt <= 17; cnt++) {
        if (ned & i) {
          a = jmp[a][cnt];
        }
        i <<= 1;
      }
    }
  }
  return a;
}
void dijkstra(int u, int num) {
  memset(dis[num], 0x3f3f3f3f3f3f3f3f, sizeof dis[num]);
  dis[num][u] = 0;
  q.push(pair<long long, int>(0, u));
  while (!q.empty()) {
    pair<long long, int> k = q.top();
    q.pop();
    if (k.first > dis[num][k.second]) continue;
    for (int i = f[k.second]; i; i = nnext[i]) {
      int to = v[i];
      if (dis[num][to] > k.first + w[i]) {
        dis[num][to] = k.first + w[i];
        q.push(pair<long long, int>(k.first + w[i], to));
      }
    }
  }
}
int main() {
  read(n), read(m);
  for (int i = 1; i <= n; ++i) {
    p[i] = i;
  }
  for (int i = 1; i <= m; ++i) {
    int a, b, c;
    read(a), read(b), read(c);
    add(a, b, c);
    add(b, a, c);
    if (find(a) != find(b)) {
      p[find(a)] = find(b);
    } else {
      chk[cnt] = chk[cnt - 1] = 1;
      pre.push_back(a);
    }
  }
  for (int i = 0; i <= 17; ++i) {
    jmp[1][i] = 1;
  }
  dep[1] = val[1] = 0;
  dfs(1, 0);
  for (int i = 0; i < pre.size(); ++i) {
    dijkstra(pre[i], i + 1);
  }
  int q;
  read(q);
  while (q--) {
    int a, b;
    read(a), read(b);
    long long ans = val[a] + val[b] - 2 * val[lca(a, b)];
    for (int i = 1; i <= pre.size(); ++i) {
      ans = min(ans, dis[i][a] + dis[i][b]);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
