#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 100;
const int L = 26;
const long long inf = 1e18 + 10;
vector<pair<int, long long> > g[N];
vector<pair<int, long long> > t[N];
bool taken[N];
vector<pair<int, int> > save;
int up[N][L];
int n, m, l;
int q;
int tin[N], tout[N];
long long dis[N];
int timer = 0;
void dfs(int u, int p, long long d = 0) {
  tin[u] = ++timer;
  up[u][0] = p;
  dis[u] = d;
  for (int i = 1; i <= l; i++) {
    up[u][i] = up[up[u][i - 1]][i - 1];
  }
  for (auto v : t[u]) {
    if (v.first == p) continue;
    dfs(v.first, u, d + v.second);
  }
  tout[u] = ++timer;
}
bool ancestor(int u, int v) { return tin[u] <= tin[v] and tout[v] <= tout[u]; }
int lca(int u, int v) {
  if (ancestor(u, v)) return u;
  if (ancestor(v, u)) return v;
  for (int i = l; i >= 0; i--) {
    if (!ancestor(up[u][i], v)) {
      u = up[u][i];
    }
  }
  return up[u][0];
}
long long get(int u, int v) { return dis[u] + dis[v] - 2 * dis[lca(u, v)]; }
void process(int root) {
  memset(up, 0, sizeof up);
  timer = 0;
  l = ceil(log2(n));
  dfs(root, root);
}
int par[N];
vector<pair<pair<int, int>, long long> > edges;
int find(int u) { return par[u] == u ? u : par[u] = find(par[u]); }
void join(int u, int v) {
  u = find(u), v = find(v);
  if (u != v) {
    if (rand() & 1) {
      swap(u, v);
    }
    par[u] = v;
  }
}
long long d[N][25];
vector<pair<int, int> > gettis;
bool inside[N];
void path(int u, int id) {
  queue<int> q;
  memset(inside, 0, sizeof inside);
  d[u][id] = 0;
  inside[u] = 1;
  q.push(u);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    inside[v] = 0;
    for (auto x : g[v]) {
      if (d[x.first][id] > d[v][id] + x.second) {
        d[x.first][id] = d[v][id] + x.second;
        if (!inside[x.first]) {
          q.push(x.first);
          inside[x.first] = 1;
        }
      }
    }
  }
}
int main() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 25; j++) {
      d[i][j] = inf;
    }
  }
  scanf("%d %d", &n, &m);
  for (int i = 0; i < m; i++) {
    int u, v;
    long long w;
    scanf("%d %d %lld", &u, &v, &w);
    g[u].push_back({v, w});
    g[v].push_back({u, w});
    edges.push_back({{u, v}, w});
  }
  for (int i = 1; i <= n; i++) {
    par[i] = i;
  }
  for (int i = 0; i < m; i++) {
    int u = edges[i].first.first, v = edges[i].first.second;
    long long w = edges[i].second;
    if (find(u) != find(v)) {
      join(u, v);
      t[u].push_back({v, w}), t[v].push_back({u, w});
      taken[i] = 1;
    }
  }
  process(1);
  for (int i = 0; i < m; i++) {
    if (!taken[i]) {
      gettis.push_back({edges[i].first.first, edges[i].first.second});
    }
  }
  for (int i = 0; i < gettis.size(); i++) {
    path(gettis[i].first, i);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v;
    scanf("%d %d", &u, &v);
    long long ans = get(u, v);
    for (int i = 0; i < gettis.size(); i++) {
      ans = min(ans, d[u][i] + d[v][i]);
    }
    printf("%lld\n", ans);
  }
}
