#include <bits/stdc++.h>
using namespace std;
long long _gcd(long long a, long long b) { return b == 0 ? a : _gcd(b, a % b); }
long long gcd(long long a, long long b) { return _gcd(abs(a), abs(b)); }
const long long N = 112345;
long long tempo = 0;
long long st[N];
long long ed[N];
long long vis[N];
long long dad[N];
vector<pair<long long, long long> > adj[N];
long long anc[20][N];
long long dis[N];
void dfs_makedad(long long u) {
  vis[u] = true;
  st[u] = tempo++;
  for (long long i = 0; i < adj[u].size(); i++) {
    long long v = adj[u][i].first;
    if (!vis[v]) {
      dad[v] = u;
      dis[v] = dis[u] + adj[u][i].second;
      dfs_makedad(v);
    }
  }
  ed[u] = tempo++;
}
bool is_ancestor(long long u, long long v) {
  return st[u] <= st[v] && st[v] <= ed[u];
}
long long lca(long long u, long long v) {
  if (is_ancestor(u, v)) return u;
  for (long long i = 19; i >= 0; i--) {
    if (anc[i][u] == -1) continue;
    if (!is_ancestor(anc[i][u], v)) u = anc[i][u];
  }
  return dad[u];
}
void pre_process(long long n) {
  dad[1] = -1;
  dis[1] = 0;
  dfs_makedad(1);
  for (long long i = 1; i <= n; i++) {
    anc[0][i] = dad[i];
  }
  for (long long i = 1; i < 20; i++) {
    for (long long j = 1; j <= n; j++) {
      if (anc[i - 1][j] != -1) {
        anc[i][j] = anc[i - 1][anc[i - 1][j]];
      } else {
        anc[i][j] = -1;
      }
    }
  }
}
vector<pair<long long, long long> > adj_t[N];
vector<tuple<long long, long long, long long> > test;
void dfs(long long v, long long p) {
  vis[v] = 1;
  for (pair<long long, long long> ad : adj_t[v]) {
    if (vis[ad.first] && ad.first != p) {
      test.push_back(make_tuple(v, ad.first, ad.second));
    } else if (!vis[ad.first]) {
      adj[v].push_back(ad);
      adj[ad.first].push_back(make_pair(v, ad.second));
      dfs(ad.first, v);
    }
  }
}
long long distance(long long a, long long b) {
  long long lc = lca(a, b);
  return dis[a] + dis[b] - 2 * dis[lc];
}
struct DJ {
  long long v, d;
  bool operator<(const DJ& rhs) const { return d > rhs.d; }
};
long long dist[60][N];
void dijikstra(long long v, long long n, long long id) {
  for (long long i = 1; i <= n; i++) {
    dist[id][i] = LLONG_MAX;
  }
  priority_queue<DJ> pq;
  dist[id][v] = 0;
  pq.push({v, 0});
  while (!pq.empty()) {
    DJ cur = pq.top();
    pq.pop();
    if (cur.d != dist[id][cur.v]) continue;
    for (pair<long long, long long> ad : adj_t[cur.v]) {
      if (cur.d + ad.second < dist[id][ad.first]) {
        dist[id][ad.first] = cur.d + ad.second;
        pq.push({ad.first, dist[id][ad.first]});
      }
    }
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, m;
  cin >> n >> m;
  long long a, b, c;
  for (long long i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj_t[a].push_back(make_pair(b, c));
    adj_t[b].push_back(make_pair(a, c));
  }
  memset((vis), 0, sizeof(vis));
  dfs(1, 0);
  long long x, y, h;
  long long auxid = 0;
  set<long long> svs;
  for (long long j = 0; j < test.size(); j++) {
    tie(x, y, h) = test[j];
    svs.insert(x);
    svs.insert(y);
  }
  long long id = 0;
  for (long long sv : svs) {
    dijikstra(sv, n, id);
    id++;
  }
  memset((vis), 0, sizeof(vis));
  pre_process(n);
  long long q;
  cin >> q;
  for (long long i = 0; i < q; i++) {
    cin >> a >> b;
    long long lc = lca(a, b);
    long long ans = distance(a, b);
    long long id = 0;
    for (long long sv : svs) {
      if (dist[id][a] < LLONG_MAX && dist[id][b] < LLONG_MAX)
        ans = min(ans, dist[id][a] + dist[id][b]);
      id++;
    }
    cout << ans << "\n";
  }
}
