#include <bits/stdc++.h>
using namespace std;
int read() {
  int r = 0, t = 1, c = getchar();
  while (c < '0' || c > '9') {
    t = c == '-' ? -1 : 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    r = (r << 3) + (r << 1) + (c ^ 48);
    c = getchar();
  }
  return r * t;
}
namespace run {
int begin[200010], next[200010 << 1], to[200010 << 1], f[200010][100 + 10],
    d[200010], cnt, n, m, e, k[200010], vis[200010];
long long w[200010 << 1], h[200010], o[100][200010];
void add(int u, int v, int ww) {
  e++;
  next[e] = begin[u];
  begin[u] = e;
  to[e] = v;
  w[e] = ww;
}
void dfs(int u) {
  int v;
  vis[u] = 1;
  for (int i = begin[u]; i; i = next[i])
    if (!vis[v = to[i]]) {
      f[v][0] = u;
      d[v] = d[u] + 1;
      h[v] = h[u] + w[i];
      dfs(v);
    } else if (f[u][0] != v) {
      k[++cnt] = u;
      k[++cnt] = v;
    }
}
struct node {
  int p;
  long long d;
  node(int P = 0, long long D = 0) {
    p = P;
    d = D;
  }
};
bool operator<(node a, node b) { return a.d > b.d; }
priority_queue<node> q;
void dij(int x, long long* o) {
  for (int i = (1), _end_ = (n); i <= _end_; i++) o[i] = (1ll << 60);
  o[x] = 0;
  for (int i = (1), _end_ = (n); i <= _end_; i++) vis[i] = 0;
  while (!q.empty()) q.pop();
  q.push({x, 0});
  while (!q.empty()) {
    int u = q.top().p, v;
    q.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = begin[u]; i; i = next[i])
      if (o[to[i]] > o[u] + w[i]) {
        o[to[i]] = o[u] + w[i];
        q.push({to[i], o[to[i]]});
      }
  }
}
int lca(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  for (int i = (100), _end_ = (0); i >= _end_; i--)
    if (d[f[u][i]] >= d[v]) u = f[u][i];
  for (int i = (100), _end_ = (0); i >= _end_; i--)
    if (f[u][i] != f[v][i]) u = f[u][i], v = f[v][i];
  return u == v ? u : f[u][0];
}
int main() {
  n = read();
  m = read();
  for (int i = (1), _end_ = (m); i <= _end_; i++) {
    int u = read(), v = read(), w = read();
    add(u, v, w);
    add(v, u, w);
  }
  d[1] = 1;
  dfs(1);
  for (int i = (1), _end_ = (100); i <= _end_; i++)
    for (int j = (1), _end_ = (n); j <= _end_; j++)
      f[j][i] = f[f[j][i - 1]][i - 1];
  for (int i = (1), _end_ = (cnt); i <= _end_; i++) dij(k[i], o[i]);
  int q = read();
  while (q--) {
    int u = read(), v = read();
    long long ans = h[u] + h[v] - 2 * h[lca(u, v)];
    for (int i = (1), _end_ = (cnt); i <= _end_; i++)
      ans = min(ans, o[i][u] + o[i][v]);
    printf("%lld\n", ans);
  }
  return 0;
}
}  // namespace run
int main() { return run::main(); }
