#include <bits/stdc++.h>
using namespace std;
int n, m, q;
struct Edge {
  int u, v;
  long long w;
  Edge() {}
  Edge(int u, int v, long long w) : u(u), v(v), w(w) {}
};
vector<Edge> remind;
vector<pair<int, long long> > G[100010];
int pre[100010];
int find(int x) { return pre[x] == 0 ? x : pre[x] = find(pre[x]); }
struct node {
  int u;
  long long w;
  node() {}
  node(int u, long long w) : u(u), w(w) {}
  bool operator<(const node &r) const { return w > r.w; }
};
long long dist[100010][30];
bool used[100010];
void Dijkstra(int st, int id) {
  for (int i = 1; i <= n; ++i) dist[i][id] = 0x3f3f3f3f3f3f3f3f, used[i] = 0;
  priority_queue<node> q;
  q.emplace(st, 0);
  dist[st][id] = 0;
  while (!q.empty()) {
    int u = q.top().u;
    q.pop();
    if (used[u]) continue;
    used[u] = 1;
    for (auto it : G[u]) {
      int v = it.first;
      long long w = it.second;
      if (!used[v] && dist[v][id] > dist[u][id] + w) {
        dist[v][id] = dist[u][id] + w;
        q.emplace(v, dist[v][id]);
      }
    }
  }
}
long long dis[100010];
int p[100010], sze[100010], son[100010], top[100010], fa[100010], deep[100010],
    cnt;
void DFS(int u) {
  sze[u] = 1;
  for (auto it : G[u])
    if (it.first != fa[u]) {
      int v = it.first;
      deep[v] = deep[u] + 1;
      fa[v] = u;
      dis[v] = dis[u] + it.second;
      DFS(v);
      sze[u] += sze[v];
      if (son[u] == -1 || sze[v] > sze[son[u]]) son[u] = v;
    }
}
void getpos(int u, int sp) {
  top[u] = sp;
  if (son[u] == -1) return;
  getpos(son[u], sp);
  for (auto it : G[u]) {
    int v = it.first;
    if (v != fa[u] && v != son[u]) getpos(v, v);
  }
}
int querylca(int u, int v) {
  int fu = top[u], fv = top[v];
  while (fu != fv) {
    if (deep[fu] < deep[fv]) {
      swap(fu, fv);
      swap(u, v);
    }
    u = fa[fu];
    fu = top[u];
  }
  if (deep[u] > deep[v]) swap(u, v);
  return u;
}
int main() {
  while (scanf("%d%d", &n, &m) != EOF) {
    for (int i = 1; i <= n; ++i) G[i].clear();
    memset(pre, 0, sizeof pre);
    memset(son, -1, sizeof son);
    remind.clear();
    cnt = 0;
    for (int i = 1, u, v, w; i <= m; ++i) {
      scanf("%d%d%d", &u, &v, &w);
      int fu = find(u), fv = find(v);
      if (fu == fv)
        remind.emplace_back(u, v, w);
      else {
        pre[fu] = fv;
        G[u].emplace_back(v, w);
        G[v].emplace_back(u, w);
      }
    }
    DFS(1);
    getpos(1, 1);
    for (auto it : remind) {
      int u = it.u, v = it.v;
      long long w = it.w;
      G[u].emplace_back(v, w);
      G[v].emplace_back(u, w);
    }
    for (int i = 0, len = remind.size(); i < len; ++i) Dijkstra(remind[i].u, i);
    scanf("%d", &q);
    for (int i = 1, u, v; i <= q; ++i) {
      scanf("%d%d", &u, &v);
      int lca = querylca(u, v);
      long long res = dis[u] + dis[v] - 2 * dis[lca];
      for (int i = 0, len = remind.size(); i < len; ++i) {
        long long w = remind[i].w;
        res = min(res, dist[u][i] + dist[v][i]);
      }
      printf("%lld\n", res);
    }
  }
  return 0;
}
