#include <bits/stdc++.h>
using namespace std;
constexpr int MAX_N = 100009;
constexpr int LOG_MAX_N = 20;
int n, m, q;
array<vector<pair<int, int64_t>>, MAX_N> edges;
array<array<int, LOG_MAX_N>, MAX_N> p;
array<int64_t, MAX_N> s;
array<int, MAX_N> dep;
array<int, MAX_N> v;
vector<vector<int64_t>> extras;
constexpr bool CHECK{true};
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  for (int d = dep[y] - dep[x], i = 0; d > 0; d >>= 1, ++i) {
    if (d & 1) y = p[y][i];
  }
  if (x == y) return x;
  for (int i = LOG_MAX_N - 1; i >= 0; --i) {
    if (p[x][i] != p[y][i]) {
      x = p[x][i];
      y = p[y][i];
    }
  }
  assert(p[x][0] == p[y][0]);
  return p[x][0];
}
vector<int64_t> sssp(int x) {
  vector<int64_t> d(n + 1, INT64_MAX >> 2);
  priority_queue<pair<int64_t, int>> q;
  for (q.emplace(0, x); !q.empty(); q.pop()) {
    int x = q.top().second;
    int64_t td = -q.top().first;
    if (td >= d[x]) continue;
    d[x] = td;
    for (auto const &e : edges[x]) {
      q.emplace(-td - e.second, e.first);
    }
  }
  return d;
}
void dfs(int x) {
  v[x] = 1;
  bool is_extra = false;
  for (auto e : edges[x]) {
    if (v[e.first] == 0) {
      p[e.first][0] = x;
      s[e.first] = s[x] + e.second;
      dep[e.first] = dep[x] + 1;
      dfs(e.first);
    } else if (v[e.first] == 1 && e.first != p[x][0]) {
      is_extra = true;
    }
  }
  if (is_extra) extras.push_back(sssp(x));
  v[x] = 2;
}
int main() {
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int x, y, d;
    cin >> x >> y >> d;
    edges[x].emplace_back(y, d);
    edges[y].emplace_back(x, d);
  }
  dfs(1);
  for (int i = 1; (1 << i) < n; ++i) {
    for (int j = 1; j <= n; ++j) {
      p[j][i] = p[p[j][i - 1]][i - 1];
    }
  }
  assert(extras.size() <= 40);
  cin >> q;
  for (int i = 0; i < q; ++i) {
    int x, y;
    cin >> x >> y;
    int64_t ans = s[x] - s[lca(x, y)] * 2 + s[y];
    for (const auto &d : extras) {
      ans = min(ans, d[x] + d[y]);
    }
    cout << ans << endl;
  }
}
