#include <bits/stdc++.h>
namespace chtholly {
inline int read() {
  int x = 0, f = 1;
  char c = getchar();
  for (; !isdigit(c); c = getchar()) f ^= c == '-';
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ '0');
  return f ? x : -x;
}
template <typename mitsuha>
inline bool read(mitsuha &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  for (; !isdigit(c) && ~c; c = getchar()) f ^= c == '-';
  if (!~c) return 0;
  for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + (c ^ '0');
  return x = f ? x : -x, 1;
}
template <typename mitsuha>
inline int write(mitsuha x) {
  if (!x) return 0 & putchar(48);
  if (x < 0) x = -x, putchar('-');
  int bit[20], i, p = 0;
  for (; x; x /= 10) bit[++p] = x % 10;
  for (i = p; i; --i) putchar(bit[i] + 48);
  return 0;
}
inline char fuhao() {
  char c = getchar();
  for (; isspace(c); c = getchar())
    ;
  return c;
}
}  // namespace chtholly
using namespace chtholly;
using namespace std;
const int yuzu = 2e5;
typedef long long fuko[yuzu | 10];
struct edge {
  int u, v, c;
} e[yuzu | 10];
typedef vector<edge> yudachi[yuzu | 10];
yudachi lj, tlj;
namespace dsu {
fuko fa;
void init(int n) {
  for (int i = 1; i <= n; ++i) fa[i] = i;
}
int find(int x) { return fa[x] ^ x ? fa[x] = find(fa[x]) : x; }
int mg(int u, int v) {
  int fu = find(u), fv = find(v);
  return fu ^ fv ? fa[fu] = fv, 1 : 0;
}
}  // namespace dsu
using dsu::find;
namespace {
fuko fa, son, sz, dep, dfn, ord, top;
void dfs1(int u, int f) {
  sz[u] = 1, fa[u] = f;
  for (auto i : lj[u])
    if (i.v ^ f) {
      dep[i.v] = dep[u] + i.c;
      dfs1(i.v, u), sz[u] += sz[i.v];
      if (sz[i.v] > sz[son[u]]) son[u] = i.v;
    }
}
void dfs2(int u, int _top) {
  top[u] = _top, ord[dfn[u] = ++top[0]] = u;
  if (son[u]) dfs2(son[u], _top);
  for (auto i : lj[u]) {
    int v = i.v;
    if (v ^ fa[u] && v ^ son[u]) dfs2(v, v);
  }
}
int lca(int u, int v) {
  for (; top[u] ^ top[v];)
    dep[top[u]] > dep[top[v]] ? u = fa[top[u]] : v = fa[top[v]];
  return dep[u] > dep[v] ? v : u;
}
}  // namespace
fuko dist[50], vis;
void spfa(int s, int id) {
  queue<int> q;
  for (int i = 1; i <= yuzu; ++i) dist[id][i] = 1e18;
  dist[id][s] = 0, q.push(s);
  for (; !q.empty();) {
    int u = q.front();
    q.pop();
    vis[u] = 0;
    for (auto i : tlj[u]) {
      int v = i.v;
      long long c = i.c;
      if (dist[id][v] > dist[id][u] + c) {
        dist[id][v] = dist[id][u] + c;
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
    }
  }
}
fuko visb;
int main() {
  int i, n = read(), m = read();
  dsu::init(n);
  for (i = 1; i <= m; ++i) {
    int u = read(), v = read(), c = read();
    e[i] = edge{u, v, c};
    if (dsu::mg(u, v)) {
      lj[u].push_back(edge{u, v, c});
      lj[v].push_back(edge{v, u, c});
      visb[i] = 1;
    }
    tlj[u].push_back(edge{u, v, c});
    tlj[v].push_back(edge{v, u, c});
  }
  dfs1(1, 0), dfs2(1, 1);
  int q, id = 0, j;
  for (i = 1; i <= m; ++i)
    if (!visb[i]) spfa(e[i].u, ++id), spfa(e[i].v, ++id);
  for (q = read(); q--;) {
    int u = read(), v = read();
    long long ans = dep[u] + dep[v] - (dep[lca(u, v)] << 1);
    for (i = 1; i <= id; ++i) ans = min(ans, dist[i][u] + dist[i][v]);
    write(ans), puts("");
  }
}
