#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const long long inf = 9223372036854775807;
int dp[maxn * 2][20];
int deep[maxn], in[maxn], num, top;
long long dis[50][maxn];
struct node {
  int x;
  long long va;
};
bool judge[maxn];
bool judge1[maxn];
vector<node> v[maxn];
void spfa(int m, int n) {
  judge1[m] = false;
  for (int a = 1; a <= n; a++) {
    dis[num][a] = inf;
    judge[a] = false;
  }
  dis[num][m] = 0;
  queue<int> q;
  q.push(m);
  judge[m] = true;
  while (!q.empty()) {
    int i = q.front();
    q.pop();
    judge[i] = false;
    for (int a = 0; a < v[i].size(); a++) {
      int b = v[i][a].x;
      int len = v[i][a].va;
      if (dis[num][b] > dis[num][i] + len) {
        dis[num][b] = dis[num][i] + len;
        if (!judge[b]) {
          q.push(b);
          judge[b] = true;
        }
      }
    }
  }
  num++;
}
void dfs(int n, int fa, int m) {
  in[n] = ++top;
  deep[n] = deep[fa] + 1;
  dp[top][0] = n;
  for (int a = 0; a < v[n].size(); a++) {
    int b = v[n][a].x;
    int len = v[n][a].va;
    if (b == fa)
      continue;
    else if (len + dis[0][n] == dis[0][b]) {
      dfs(b, n, m);
      dp[++top][0] = n;
    } else {
      if (judge1[b]) {
        spfa(b, m);
        judge1[b] = false;
      }
      if (judge1[n]) {
        spfa(n, m);
        judge1[n] = false;
      }
    }
  }
}
void get_dp() {
  for (int a = top; a > 0; a--) {
    for (int b = 1; (1 << b) + a <= top; b++) {
      int i = dp[a][b - 1];
      int j = dp[a + (1 << (b - 1))][b - 1];
      if (deep[i] < deep[j])
        dp[a][b] = i;
      else
        dp[a][b] = j;
    }
  }
}
int get_lca(int l, int r) {
  int k = 0;
  while (1 << (k + 1) <= r - l + 1) k++;
  int i = dp[l][k];
  int j = dp[r - (1 << k) + 1][k];
  if (deep[i] < deep[j])
    return i;
  else
    return j;
}
int main() {
  int n, m;
  scanf("%d %d", &n, &m);
  for (int a = 1; a <= m; a++) {
    int i, j, k;
    scanf("%d %d %d", &i, &j, &k);
    v[i].push_back({j, k});
    v[j].push_back({i, k});
  }
  memset(judge1, true, sizeof(judge1));
  num = 0;
  top = 0;
  deep[0] = 0;
  spfa(1, n);
  dfs(1, 0, n);
  get_dp();
  int q;
  scanf("%d", &q);
  while (q--) {
    int i, j;
    scanf("%d %d", &i, &j);
    long long ans;
    if (in[i] > in[j]) swap(i, j);
    int k = get_lca(in[i], in[j]);
    ans = dis[0][i] + dis[0][j] - dis[0][k] * 2;
    for (int a = 1; a < num; a++)
      if (dis[a][i] + dis[a][j] < ans) ans = dis[a][i] + dis[a][j];
    printf("%lld\n", ans);
  }
}
