#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
int n, m, cnt = 1, ccnt;
int head[maxn], deep[maxn], c[maxn];
int vis[maxn], flagd[maxn], visdfs[maxn], fa[maxn][20];
long long dis[maxn][60], lca[maxn][20];
vector<int> dj;
vector<pair<pair<int, int>, int>> v;
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    q;
struct Node {
  int a, b, val, id, next;
} edge[maxn << 1];
void addedge(int a, int b, int val, int id) {
  edge[cnt].a = a;
  edge[cnt].b = b;
  edge[cnt].id = id;
  edge[cnt].val = val;
  edge[cnt].next = head[a];
  head[a] = cnt++;
}
void init(int pos, int pre, long long val) {
  if (pos == 1) return;
  fa[pos][0] = pre;
  lca[pos][0] = val;
  for (int i = 1; i < 20; i++) {
    int t = fa[pos][i - 1];
    if (t && fa[t][i - 1]) {
      fa[pos][i] = fa[t][i - 1];
      lca[pos][i] = lca[pos][i - 1] + lca[t][i - 1];
    }
  }
}
long long getAns(int a, int b) {
  long long Ans = 0;
  if (deep[a] > deep[b]) swap(a, b);
  int f = deep[b] - deep[a];
  for (int i = 0; f; i++) {
    if (f % 2) {
      Ans += lca[b][i];
      b = fa[b][i];
    }
    f /= 2;
  }
  if (a != b) {
    for (int i = 19; i >= 0; i--) {
      if (fa[a][i] != fa[b][i]) {
        Ans += lca[a][i] + lca[b][i];
        a = fa[a][i];
        b = fa[b][i];
      }
    }
    Ans += lca[a][0] + lca[b][0];
  }
  return Ans;
}
void dfs(int pos, int pre, int dep, long long length) {
  vis[pos] = 1;
  deep[pos] = dep;
  init(pos, pre, length);
  for (int i = head[pos]; i; i = edge[i].next) {
    int v = edge[i].b;
    if (vis[v]) continue;
    visdfs[edge[i].id] = 1;
    dfs(v, pos, dep + 1, 1LL * edge[i].val);
  }
}
void Dijkstra(int start, int ccnt) {
  memset(vis, 0, sizeof(vis));
  vis[start] = 1;
  dis[start][ccnt] = 0;
  for (int i = head[start]; i; i = edge[i].next)
    q.push(make_pair(1LL * edge[i].val, edge[i].b));
  while (!q.empty()) {
    pair<long long, int> top = q.top();
    q.pop();
    int v = top.second;
    long long tpval = top.first;
    if (vis[v]) continue;
    vis[v] = 1;
    dis[v][ccnt] = tpval;
    for (int i = head[v]; i; i = edge[i].next) {
      int t = edge[i].b;
      if (!vis[t]) q.push(make_pair(dis[v][ccnt] + 1LL * edge[i].val, t));
    }
  }
}
int main() {
  memset(dis, 0x3f, sizeof(dis));
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++) {
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    addedge(a, b, c, i);
    addedge(b, a, c, i);
    v.push_back(make_pair(make_pair(a, b), c));
  }
  dfs(1, 0, 1, 0);
  for (int i = 0; i < m; i++)
    if (!visdfs[i + 1]) dj.push_back(i);
  for (int i = 0; i < dj.size(); i++) {
    int a = v[dj[i]].first.first, b = v[dj[i]].first.second;
    if (!flagd[a]) {
      flagd[a] = 1;
      Dijkstra(a, ++ccnt);
      c[a] = ccnt;
    }
    if (!flagd[b]) {
      flagd[b] = 1;
      Dijkstra(b, ++ccnt);
      c[b] = ccnt;
    }
  }
  int Q;
  scanf("%d", &Q);
  for (int i = 0; i < Q; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    long long Ans = getAns(a, b);
    for (int i = 0; i < dj.size(); i++) {
      int A = v[dj[i]].first.first, B = v[dj[i]].first.second;
      long long val = v[dj[i]].second;
      long long tmp =
          min(dis[a][c[A]] + dis[b][c[B]], dis[a][c[B]] + dis[b][c[A]]) + val;
      Ans = min(Ans, tmp);
    }
    printf("%lld\n", Ans);
  }
  return 0;
}
