#include <bits/stdc++.h>
using namespace std;
int LIM = 1e5 + 10;
vector<vector<pair<long long int, long long int> > > adj(LIM);
vector<vector<pair<long long int, long long int> > > tree(LIM);
vector<long long int> parent(LIM);
void dfs(int n, vector<long long int> &visited) {
  for (int i = 0; i < adj[n].size(); i++) {
    if (visited[adj[n][i].first] == -1) {
      visited[adj[n][i].first] = 1;
      tree[n].push_back(adj[n][i]);
      parent[adj[n][i].first] = n;
      dfs(adj[n][i].first, visited);
    }
  }
}
int l = log2(LIM) + 2;
vector<vector<long long int> > pa(LIM, vector<long long int>(l + 1, 1));
vector<long long int> dist(LIM);
vector<long long int> tin(LIM);
vector<long long int> tout(LIM);
int t;
void dfs2(int n) {
  tin[n] = t++;
  for (int i = 0; i < tree[n].size(); i++) {
    pa[tree[n][i].first][0] = n;
    int node = tree[n][i].first;
    for (int j = 1; j < l; j++) pa[node][j] = pa[pa[node][j - 1]][j - 1];
    dist[node] = dist[n] + tree[n][i].second;
    dfs2(node);
  }
  tout[n] = t++;
}
bool is_ancestor(int a, int b) {
  return (tin[a] <= tin[b] && tout[a] >= tout[b]);
}
int lca(int a, int b) {
  int temp = a;
  if (is_ancestor(a, b)) return a;
  if (is_ancestor(b, a)) return b;
  for (int i = l; i >= 0; i--) {
    if (!is_ancestor(pa[temp][i], b)) temp = pa[temp][i];
  }
  return pa[temp][0];
}
long long int distanc(int a, int b) {
  return (dist[a] + dist[b] - 2 * dist[lca(a, b)]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    adj[a].push_back(pair<long long int, long long int>(b, w));
    adj[b].push_back(pair<long long int, long long int>(a, w));
  }
  vector<long long int> others;
  vector<long long int> visited(LIM, -1);
  visited[1] = 1;
  parent[1] = 1;
  dfs(1, visited);
  dfs2(1);
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < adj[i].size(); j++) {
      if (parent[adj[i][j].first] == i || parent[i] == adj[i][j].first)
        continue;
      else {
        others.push_back(i);
      }
    }
  }
  int counter = others.size();
  vector<vector<long long int> > precomputed(counter + 1,
                                             vector<long long int>(n + 1, -1));
  for (int i = 0; i < counter; i++) {
    int s = others[i];
    priority_queue<pair<long long int, long long int>,
                   vector<pair<long long int, long long int> >,
                   greater<pair<long long int, long long int> > >
        mini;
    mini.push(pair<long long int, long long int>(0, s));
    while (!mini.empty()) {
      pair<long long int, long long int> temp = mini.top();
      mini.pop();
      int node = temp.second;
      if (precomputed[i][node] != -1) continue;
      precomputed[i][node] = temp.first;
      for (int j = 0; j < adj[node].size(); j++)
        mini.push(pair<long long int, long long int>(
            adj[node][j].second + precomputed[i][node], adj[node][j].first));
    }
  }
  int q;
  cin >> q;
  int counte = 1;
  while (counte <= q) {
    counte++;
    int a, b;
    cin >> a >> b;
    long long int ans = distanc(a, b);
    for (int i = 0; i < others.size(); i++) {
      ans = min(ans, precomputed[i][a] + precomputed[i][b]);
    }
    cout << ans << endl;
  }
  return 0;
}
