#include <bits/stdc++.h>
using namespace std;
const long long N = 1e5 + 5;
const long long inf = 1ll << 60;
long long read() {
  long long s = 0;
  char c = getchar(), lc = '+';
  while (c < '0' || '9' < c) lc = c, c = getchar();
  while ('0' <= c && c <= '9') s = s * 10 + c - '0', c = getchar();
  return lc == '-' ? -s : s;
}
void write(long long x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x < 10)
    putchar(x + '0');
  else {
    write(x / 10);
    putchar(x % 10 + '0');
  }
}
void print(long long x, char c = '\n') {
  write(x);
  putchar(c);
}
struct edge {
  long long to, val, nxt;
} e[N * 2];
long long head[N], cnt = 0;
void add(long long u, long long v, long long w) {
  e[++cnt].to = v;
  e[cnt].val = w;
  e[cnt].nxt = head[u];
  head[u] = cnt;
}
long long fa[N][23], dep[N], diss[N];
long long f[N], dis[N][25][2], ed[25], cntt = 0;
long long ff(long long x) { return f[x] == x ? x : f[x] = ff(f[x]); }
struct node {
  long long val, pos;
  bool operator<(const node &a) const { return val > a.val; }
} qwq;
bool vis[N];
priority_queue<node> h;
void dijkstra(long long s, long long n, long long xx, long long yy) {
  memset(vis, 0, sizeof(vis));
  for (long long i = 1; i <= n; i++) dis[i][xx][yy] = inf;
  while (!h.empty()) h.pop();
  dis[s][xx][yy] = 0;
  h.push((node){0, s});
  while (!h.empty()) {
    qwq = h.top();
    h.pop();
    long long now = qwq.pos;
    if (vis[now]) continue;
    vis[now] = 1;
    for (long long i = head[now]; i; i = e[i].nxt) {
      long long to = e[i].to, val = e[i].val;
      if (dis[to][xx][yy] > dis[now][xx][yy] + val) {
        dis[to][xx][yy] = dis[now][xx][yy] + val;
        h.push((node){dis[to][xx][yy], to});
      }
    }
  }
}
void dfs(long long now, long long f, long long d, long long di) {
  dep[now] = d;
  diss[now] = di;
  fa[now][0] = f;
  for (long long i = head[now]; i; i = e[i].nxt)
    if (e[i].to != f) dfs(e[i].to, now, d + 1, di + e[i].val);
}
long long find(long long u, long long v) {
  long long now = 20;
  do {
    if (dep[u] < dep[v]) swap(u, v);
    if (dep[u] - dep[v] >= (1 << now)) u = fa[u][now];
    now--;
  } while (dep[u] != dep[v]);
  if (u == v) return u;
  for (now = 20; now >= 0; now--)
    if (fa[u][now] != fa[v][now]) {
      u = fa[u][now];
      v = fa[v][now];
    }
  return fa[u][0];
}
long long u[N], v[N], w[N], cnttt = 0;
int main() {
  long long n = read(), m = read();
  for (long long i = 1; i <= n; i++) f[i] = i;
  for (long long i = 1; i <= n; i++)
    for (long long j = 0; j <= 23; j++) dis[i][j][0] = dis[i][j][1] = inf;
  for (long long i = 1; i <= m; i++) {
    u[i] = read();
    v[i] = read();
    w[i] = read();
    long long x = ff(u[i]), y = ff(v[i]);
    if (x != y) {
      add(u[i], v[i], w[i]);
      add(v[i], u[i], w[i]);
      f[x] = y;
      w[i] = 0;
    }
  }
  dfs(1, 0, 1, 0);
  for (int j = 1; j <= 20; j++)
    for (int i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  for (long long i = 1; i <= m; i++)
    if (w[i]) {
      add(u[i], v[i], w[i]);
      add(v[i], u[i], w[i]);
    }
  for (long long i = 1; i <= m; i++)
    if (w[i]) {
      ed[++cntt] = i;
      dijkstra(u[i], n, cntt, 0);
      dijkstra(v[i], n, cntt, 1);
    }
  long long q = read();
  while (q--) {
    long long x = read(), y = read(), fath = find(x, y);
    long long ans = diss[x] + diss[y] - diss[fath] * 2;
    for (long long i = 1; i <= cntt; i++) {
      ans = min(ans, dis[x][i][0] + dis[y][i][1] + w[ed[i]]);
      ans = min(ans, dis[x][i][1] + dis[y][i][0] + w[ed[i]]);
    }
    print(ans);
  }
  return 0;
}
