#include <bits/stdc++.h>
using namespace std;
inline int Read() {
  int x(0), f(1);
  char c(getchar());
  while (c < '0' || '9' < c) {
    if (c == '-') {
      f = -1;
    }
    c = getchar();
  }
  while ('0' <= c && c <= '9') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return f * x;
}
void Write(int x) {
  if (x < 0) {
    printf("-");
    x = -x;
  }
  if (9 < x) {
    Write(x / 10);
  }
  putchar(x % 10 + '0');
}
inline void Writeln(int x) {
  Write(x);
  puts("");
}
const int MAXN = 1e5 + 5;
const int MAXM = (MAXN + 21) << 1;
const long long INF = 1e16;
int n, m, q;
struct Edge {
  int to, next, val;
} edge[MAXM];
int edge_head[MAXN];
int edge_cnt = 0;
void AddEdge(int form, int to, int val) {
  edge[++edge_cnt].to = to;
  edge[edge_cnt].val = val;
  edge[edge_cnt].next = edge_head[form];
  edge_head[form] = edge_cnt;
}
bool visit[MAXN];
void Dijkstra(int root, long long *dis) {
  priority_queue<pair<long long, int> > Queue;
  for (int i = 1; i <= n; ++i) {
    dis[i] = INF;
    visit[i] = 0;
  }
  dis[root] = 0;
  Queue.push(make_pair(1, root));
  int now;
  while (!Queue.empty()) {
    now = Queue.top().second;
    Queue.pop();
    if (!visit[now]) {
      visit[now] = 1;
      for (int edge_i = edge_head[now]; edge_i; edge_i = edge[edge_i].next) {
        if (dis[edge[edge_i].to] > dis[now] + edge[edge_i].val) {
          dis[edge[edge_i].to] = dis[now] + edge[edge_i].val;
          Queue.push(make_pair(INF - dis[edge[edge_i].to], edge[edge_i].to));
        }
      }
    }
  }
}
long long dis[44][MAXN];
int father[MAXN];
int Find(int now) {
  if (father[now] == now) {
    return now;
  }
  return father[now] = Find(father[now]);
}
struct Edge2 {
  int f, t, v;
} e[MAXM * 2];
bool Cmp(Edge2 a, Edge2 b) { return a.v < b.v; }
bool boo[MAXM];
long long sum[MAXN];
int point_deep[MAXN];
int point_father[MAXN];
int point_son[MAXN];
int point_size[MAXN];
int chain_cnt = 0;
int point_id[MAXN];
int chain_top[MAXN];
void DFS_1(int now = 1) {
  int max_size = -1;
  point_size[now] = 1;
  for (int edge_i = edge_head[now]; edge_i; edge_i = edge[edge_i].next) {
    if (point_father[now] != edge[edge_i].to) {
      sum[edge[edge_i].to] = sum[now] + edge[edge_i].val;
      point_father[edge[edge_i].to] = now;
      point_deep[edge[edge_i].to] = point_deep[now] + 1;
      DFS_1(edge[edge_i].to);
      point_size[now] += point_size[edge[edge_i].to];
      if (point_size[edge[edge_i].to] > max_size) {
        max_size = point_size[edge[edge_i].to];
        point_son[now] = edge[edge_i].to;
      }
    }
  }
}
void DFS_2(int now = 1, int top = 1) {
  point_id[now] = ++chain_cnt;
  chain_top[now] = top;
  if (!point_son[now]) {
    return;
  }
  DFS_2(point_son[now], top);
  for (int edge_i = edge_head[now]; edge_i; edge_i = edge[edge_i].next) {
    if (edge[edge_i].to != point_father[now] &&
        edge[edge_i].to != point_son[now]) {
      DFS_2(edge[edge_i].to, edge[edge_i].to);
    }
  }
}
int LCA(int u, int v) {
  while (chain_top[u] != chain_top[v]) {
    if (point_deep[chain_top[u]] < point_deep[chain_top[v]]) {
      swap(u, v);
    }
    u = point_father[chain_top[u]];
  }
  if (point_deep[u] > point_deep[v]) {
    swap(u, v);
  }
  return u;
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%d", &e[i].f, &e[i].t, &e[i].v);
  }
  sort(e + 1, e + 1 + m, Cmp);
  for (int i = 1; i <= n; ++i) {
    father[i] = i;
  }
  for (int i = 1; i <= m; ++i) {
    if (Find(e[i].f) ^ Find(e[i].t)) {
      father[Find(e[i].f)] = Find(e[i].t);
      AddEdge(e[i].f, e[i].t, e[i].v);
      AddEdge(e[i].t, e[i].f, e[i].v);
    } else {
      boo[i] = 1;
    }
  }
  DFS_1();
  DFS_2();
  for (int i = 1; i <= m; ++i) {
    if (boo[i]) {
      AddEdge(e[i].f, e[i].t, e[i].v);
      AddEdge(e[i].t, e[i].f, e[i].v);
    }
  }
  int cnt = 0;
  for (int i = 1; i <= m; ++i) {
    if (boo[i]) {
      Dijkstra(e[i].f, dis[++cnt]);
      Dijkstra(e[i].t, dis[++cnt]);
    }
  }
  int q;
  scanf("%d", &q);
  int a, b;
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &a, &b);
    long long answer = sum[a] + sum[b] - 2ll * sum[LCA(a, b)];
    for (int j = 1; j <= cnt; ++j) {
      answer = min(answer, dis[j][a] + dis[j][b]);
    }
    printf("%lld\n", answer);
  }
  return 0;
}
