#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 100;
const int M = 21;
const long long mod = 1e9 + 7;
const long long MOD = 998244353;
const long double eps = 0.000000001;
const int P = 1336;
const long long inf = 1e18;
const int CONST = 5001;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
vector<pair<int, int> > g[N], ng[N];
int tim = 0;
int tin[N], tout[N], u[N], sz[N], pred[N];
long long h[N], r[M][N];
int up[N][M];
int n, m;
int get(int v) {
  if (pred[v] == v) return v;
  return pred[v] = get(pred[v]);
}
void unite(int a, int b) {
  if (sz[a] < sz[b]) swap(a, b);
  sz[a] += sz[b];
  pred[b] = a;
}
void DFS(int v, int pr) {
  u[v] = 1;
  tim++;
  tin[v] = tim;
  up[v][0] = pr;
  for (int i = 1; i < M; i++) {
    up[v][i] = up[up[v][i - 1]][i - 1];
  }
  for (auto to : ng[v]) {
    if (to.first == pr) continue;
    h[to.first] = h[v] + to.second;
    DFS(to.first, v);
  }
  tim++;
  tout[v] = tim;
}
bool upper(int a, int b) {
  return ((tin[a] <= tin[b]) && (tout[a] >= tout[b]));
}
int LCA(int a, int b) {
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = M - 1; i >= 0; i--) {
    if (!upper(up[a][i], b)) a = up[a][i];
  }
  return up[a][0];
}
void Dijkstra(int v, int uk) {
  priority_queue<pair<long long, int> > q;
  q.push({0, v});
  for (int i = 0; i < n; i++) {
    r[uk][i] = inf;
  }
  while (!q.empty()) {
    long long ra = q.top().first;
    int v = q.top().second;
    ra = -ra;
    q.pop();
    if (r[uk][v] < ra) continue;
    r[uk][v] = ra;
    for (auto to : g[v]) {
      if (r[uk][to.first] > ra + to.second) {
        r[uk][to.first] = ra + to.second;
        q.push({-r[uk][to.first], to.first});
      }
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  srand(time(0));
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    pred[i] = i;
    sz[i] = 1;
  }
  int uk = 0;
  vector<int> e;
  for (int i = 0; i < m; i++) {
    int x, y, z;
    cin >> x >> y >> z;
    x--;
    y--;
    g[x].push_back({y, z});
    g[y].push_back({x, z});
    int a = get(x), b = get(y);
    if (a != b) {
      ng[x].push_back({y, z});
      ng[y].push_back({x, z});
      unite(a, b);
    } else
      e.push_back(x);
  }
  for (auto to : e) {
    Dijkstra(to, uk);
    uk++;
  }
  DFS(0, 0);
  int q;
  cin >> q;
  for (int w = 0; w < q; w++) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int lca = LCA(a, b);
    long long ans = h[a] + h[b] - 2 * h[lca];
    for (int i = 0; i < uk; i++) {
      ans = min(ans, r[i][a] + r[i][b]);
    }
    cout << ans << '\n';
  }
}
