#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 10007;
const long long inf = 1e17;
struct node {
  int u, v, w, ne, f;
} e[maxn * 2];
vector<int> p;
int dep[maxn], head[maxn], f[maxn][18], n, tot;
long long h[maxn], dis[42][maxn];
bool vis[maxn];
void add(int u, int v, int w) {
  e[tot].u = u;
  e[tot].v = v;
  e[tot].w = w;
  e[tot].ne = head[u];
  e[tot].f = 0;
  head[u] = tot++;
}
void spfa(int x, int po) {
  for (int i = 1; i <= n; i++) {
    dis[po][i] = inf;
    vis[i] = 0;
  }
  vis[x] = 1;
  dis[po][x] = 0;
  queue<int> q;
  q.push(x);
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (int i = head[u]; i != -1; i = e[i].ne) {
      int v = e[i].v;
      int w = e[i].w;
      if (dis[po][v] > dis[po][u] + w) {
        dis[po][v] = dis[po][u] + w;
        if (vis[v] == 0) {
          q.push(v);
          vis[v] = 1;
        }
      }
    }
  }
}
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  f[u][0] = fa;
  vis[u] = 1;
  for (int i = head[u]; i != -1; i = e[i].ne) {
    if (vis[e[i].v] == 1) continue;
    e[i].f = e[i ^ 1].f = 1;
    h[e[i].v] = h[u] + e[i].w;
    dfs(e[i].v, u);
  }
}
void cal() {
  for (int i = 1; (1 << i) <= n; i++)
    for (int u = 1; u <= n; u++) f[u][i] = f[f[u][i - 1]][i - 1];
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int d = dep[u] - dep[v];
  for (int i = 0; (1 << i) <= d; i++)
    if ((1 << i) & d) u = f[u][i];
  if (u != v) {
    for (int i = (int)log(n); i >= 0; i--)
      if (f[u][i] != f[v][i]) {
        u = f[u][i];
        v = f[v][i];
      }
    return f[u][0];
  } else
    return u;
}
int main() {
  int m;
  cin >> n >> m;
  tot = 0;
  for (int i = 1; i <= n; i++) head[i] = -1;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    add(u, v, w);
    add(v, u, w);
  }
  h[1] = 0;
  dep[0] = 0;
  dfs(1, 0);
  cal();
  for (int i = 0; i < tot; i++)
    if (e[i].f == 0) {
      p.push_back(e[i].u);
      p.push_back(e[i].v);
    }
  sort(p.begin(), p.end());
  p.erase(unique(p.begin(), p.end()), p.end());
  for (int i = 0; i < p.size(); i++) spfa(p[i], i);
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int ff = lca(u, v);
    long long ans = h[u] + h[v] - 2 * h[ff];
    for (int i = 0; i < p.size(); i++) ans = min(ans, dis[i][u] + dis[i][v]);
    printf("%lld\n", ans);
  }
  return 0;
}
