#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
vector<pair<int, long long> > G[maxn];
int n, m;
set<pair<int, int> > st;
int vis[maxn];
int depth[maxn];
struct Edge {
  int u, v;
  long long w;
} edge[maxn];
int fa[maxn][21];
long long d[maxn];
void dfs(int u, int pre, long long res) {
  depth[u] = depth[pre] + 1;
  vis[u] = 1;
  d[u] = res;
  fa[u][0] = pre;
  for (int i = 1; i <= 20; ++i) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  int len = (int)G[u].size();
  for (int i = 0; i <= len - 1; ++i) {
    int v = G[u][i].first;
    long long w = G[u][i].second;
    if (vis[v]) continue;
    st.insert(make_pair(u, v));
    st.insert(make_pair(v, u));
    dfs(v, u, res + w);
  }
}
int LCA(int x, int y) {
  if (depth[x] < depth[y]) swap(x, y);
  for (int i = 20; i >= 0; --i) {
    if (depth[fa[x][i]] >= depth[y]) {
      x = fa[x][i];
    }
  }
  if (x == y) return x;
  for (int i = 20; i >= 0; --i) {
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  }
  return fa[x][0];
}
long long get_dis(int x, int y) {
  int rt = LCA(x, y);
  return d[x] + d[y] - d[rt] * 2;
}
int id[45];
long long dis[45][45];
long long fuck[45];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    int u, v;
    long long w;
    scanf("%d%d%lld", &u, &v, &w);
    G[u].push_back(make_pair(v, w));
    G[v].push_back(make_pair(u, w));
    if (u > v) swap(u, v);
    edge[i] = {u, v, w};
  }
  depth[0] = 0;
  dfs(1, 0, 0);
  memset(vis, 0, sizeof(vis));
  memset(dis, 0x3f, sizeof(dis));
  int cnt = 0;
  for (int i = 1; i <= m; ++i) {
    pair<int, int> p = make_pair(edge[i].u, edge[i].v);
    if (st.find(p) != st.end()) continue;
    if (!vis[edge[i].u]) id[vis[edge[i].u] = ++cnt] = edge[i].u;
    if (!vis[edge[i].v]) id[vis[edge[i].v] = ++cnt] = edge[i].v;
    dis[vis[edge[i].u]][vis[edge[i].v]] = edge[i].w;
    dis[vis[edge[i].v]][vis[edge[i].u]] = edge[i].w;
  }
  for (int i = 1; i <= cnt; ++i) dis[i][i] = 0;
  for (int i = 1; i <= cnt; ++i)
    for (int j = 1; j <= cnt; ++j) {
      dis[i][j] = min(dis[i][j], get_dis(id[i], id[j]));
    }
  for (int k = 1; k <= cnt; ++k)
    for (int i = 1; i <= cnt; ++i)
      for (int j = 1; j <= cnt; ++j) {
        dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
      }
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v;
    scanf("%d%d", &u, &v);
    long long ans = get_dis(u, v);
    for (int i = 1; i <= cnt; ++i) fuck[i] = get_dis(id[i], v);
    for (int i = 1; i <= cnt; ++i) {
      long long temp = get_dis(u, id[i]);
      for (int j = 1; j <= cnt; ++j) {
        ans = min(ans, temp + dis[i][j] + fuck[j]);
      }
    }
    printf("%lld\n", ans);
  }
  return 0;
}
