#include <bits/stdc++.h>
using namespace std;
const int N = 101000, MN = 45;
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - 48;
    ch = getchar();
  }
  return x * f;
}
struct EDGE {
  int nxt, to, w;
} e[2 * N];
int n, m, Q, low[N], vis[N], fa[N][20], head[N], dpt[N], nd[MN], used[N],
    cnt = 0, cnt2 = 0;
long long dist[N][MN], dist2[N];
inline void add(int u, int v, int w) {
  e[++cnt].nxt = head[u];
  e[cnt].to = v;
  e[cnt].w = w;
  head[u] = cnt;
}
void dfs(int u, int fath) {
  used[u] = 1;
  dpt[u] = dpt[fath] + 1;
  fa[u][0] = fath;
  for (int i = 1; i <= low[dpt[u]]; ++i) {
    fa[u][i] = fa[fa[u][i - 1]][i - 1];
  }
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to, w = e[i].w;
    if (v == fath) continue;
    if (!used[v]) {
      dist2[v] = dist2[u] + w;
      dfs(v, u);
    } else {
      if (used[u] == 1) nd[++cnt2] = u, used[u]++;
      if (used[v] == 1) nd[++cnt2] = v, used[v]++;
    }
  }
}
struct NODE {
  long long d;
  int u;
  bool operator<(const NODE &a) const { return d > a.d; }
};
void dij(int stt, int now) {
  for (int i = 1; i <= n; ++i) dist[i][now] = 1e18 + 7;
  priority_queue<NODE> q;
  q.push((NODE){0, stt});
  dist[stt][now] = 0;
  memset(vis, 0, sizeof(vis));
  while (!q.empty()) {
    NODE ntp = q.top();
    q.pop();
    int u = ntp.u;
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to, w = e[i].w;
      if (dist[v][now] > dist[u][now] + w) {
        dist[v][now] = dist[u][now] + w;
        q.push((NODE){dist[v][now], v});
      }
    }
  }
}
inline int lca(int x, int y) {
  if (dpt[x] < dpt[y]) swap(x, y);
  while (dpt[x] > dpt[y]) {
    x = fa[x][low[dpt[x] - dpt[y]]];
  }
  if (x == y) return x;
  for (int i = low[dpt[x]]; i >= 0; --i)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
int main() {
  int i, j;
  n = read();
  m = read();
  memset(head, 0, sizeof(head));
  memset(used, 0, sizeof(used));
  memset(dist2, 0, sizeof(dist2));
  for (i = 1; i <= m; ++i) {
    int u = read(), v = read(), w = read();
    add(u, v, w);
    add(v, u, w);
  }
  low[1] = 0;
  for (i = 2; i <= n; ++i) low[i] = low[i >> 1] + 1;
  dpt[0] = 0;
  dfs(1, 0);
  for (i = 1; i <= cnt2; ++i) {
    int stt = nd[i];
    dij(stt, i);
  }
  Q = read();
  for (i = 1; i <= Q; ++i) {
    int u = read(), v = read();
    int ast = lca(u, v);
    long long DIST = dist2[u] + dist2[v] - dist2[ast] * 2;
    for (j = 1; j <= cnt2; ++j)
      DIST =
          ((DIST < dist[u][j] + dist[v][j]) ? DIST : dist[u][j] + dist[v][j]);
    printf("%I64d\n", DIST);
  }
  return 0;
}
