#include <bits/stdc++.h>
using namespace std;
void err(istream_iterator<string>) { cout << "NEXT\n"; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << ", ";
  err(++it, args...);
}
const int MOD = 1e9 + 7, MOD1 = (119 << 23) | 1;
const long long INF = 1e18 + 5, MAX = 1e5 + 5;
int n, m;
int ve[22], h[MAX];
long long rec[MAX];
int fat[MAX], siz[MAX];
vector<pair<int, pair<int, int>>> Edges;
vector<pair<int, int>> d[MAX];
vector<pair<int, int>> D[MAX];
long long dis[MAX][22];
int nx[MAX][22];
void start() {
  for (int i = 1; i <= n; i++) {
    fat[i] = i;
    siz[i] = 1;
  }
}
int find(int x) { return ((fat[x] == x) ? x : fat[x] = find(fat[x])); }
bool join(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y) return 0;
  if (siz[x] < siz[y]) {
    swap(x, y);
  }
  siz[x] += siz[y];
  fat[y] = x;
  return 1;
}
void dijk(int x) {
  int u = ve[x];
  for (int i = 0; i <= n; i++) {
    dis[i][x] = INF;
  }
  dis[u][x] = 0;
  priority_queue<pair<long long, int>> q;
  q.push({0, u});
  while (!q.empty()) {
    long long id = q.top().second, cost = -q.top().first;
    q.pop();
    if (cost != dis[id][x]) continue;
    for (pair<int, int> it : d[id]) {
      long long ncos = it.second, nid = it.first;
      if (dis[nid][x] > dis[id][x] + ncos) {
        dis[nid][x] = dis[id][x] + ncos;
        q.push({-dis[nid][x], nid});
      }
    }
  }
}
void dfs(int u = 1, int v = 0) {
  nx[u][0] = v;
  for (int i = 1; i < 22; i++) {
    nx[u][i] = nx[nx[u][i - 1]][i - 1];
    if (nx[u][i] == 0) break;
  }
  for (auto it : D[u]) {
    if (it.first == v) continue;
    rec[it.first] = rec[u] + it.second;
    h[it.first] = h[u] + 1;
    dfs(it.first, u);
  }
}
int LCA(int u, int v) {
  if (h[u] > h[v]) {
    swap(u, v);
  }
  int dif = h[v] - h[u];
  for (int i = 0; i < 22; i++) {
    if ((1 << i) & dif) {
      v = nx[v][i];
    }
  }
  if (u == v) return u;
  for (int i = 21; i >= 0; i--) {
    if (nx[u][i] != nx[v][i]) {
      u = nx[u][i];
      v = nx[v][i];
    }
  }
  return nx[u][0];
}
long long dist(int u, int v) { return rec[u] - rec[v]; }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> n >> m;
  Edges.reserve(m);
  start();
  for (int i = 0; i < m; i++) {
    int x, y;
    cin >> x;
    int c;
    cin >> y >> c;
    d[x].push_back({y, c});
    d[y].push_back({x, c});
    Edges.push_back({c, {x, y}});
  }
  int id = 0;
  for (auto it : Edges) {
    if (join(it.second.first, it.second.second)) {
      D[it.second.first].push_back({it.second.second, it.first});
      D[it.second.second].push_back({it.second.first, it.first});
    } else {
      ve[id++] = it.second.first;
    }
  }
  assert(id <= 22);
  for (int i = 0; i < id; i++) {
    dijk(i);
  }
  dfs();
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int lca = LCA(u, v);
    long long res = dist(u, lca) + dist(v, lca);
    for (int i = 0; i < id; i++) {
      res = min(res, dis[u][i] + dis[v][i]);
    }
    cout << res << "\n";
  }
  return 0;
}
