#include <bits/stdc++.h>
using namespace std;
const long long INF = 200000000000000000LL;
const long double EPS = 1e-9;
struct edge {
  int u, v, w;
  edge() {}
  edge(int uu, int vv, int ww) { u = uu, v = vv, w = ww; }
  bool operator<(const edge &p) const { return w < p.w; }
};
const int N = 100000 + 7;
int par[N], rep[N];
vector<edge> e;
vector<int> g[N], c[N];
vector<int> nottaken;
map<int, int> id;
vector<edge> lfted;
inline int Find(int r) { return par[r] == r ? r : par[r] = Find(par[r]); }
long long mst(int n) {
  if (n == 1) return 0;
  sort(e.begin(), e.end());
  for (int i = 1; i <= n; ++i) par[i] = i, rep[i] = 1;
  set<int> st;
  long long res = 0, cnt = 0;
  for (int i = 0; i < (int)e.size(); ++i) {
    int u = Find(e[i].u);
    int v = Find(e[i].v);
    if (u != v and cnt < n - 1) {
      if (rep[u] > rep[v])
        par[v] = u, ++rep[u];
      else
        par[u] = v, ++rep[v];
      res += e[i].w;
      g[e[i].u].push_back(e[i].v);
      g[e[i].v].push_back(e[i].u);
      c[e[i].u].push_back(e[i].w);
      c[e[i].v].push_back(e[i].w);
    } else {
      lfted.push_back(e[i]);
      st.insert(e[i].u);
      st.insert(e[i].v);
    }
  }
  for (auto x : st) {
    nottaken.push_back(x);
    int sz = id.size();
    id[x] = sz;
  }
  return -1;
}
vector<int> G[N], C[N];
int n, m;
int lev[N];
int sp[N][20];
long long dism[N];
void dfs(int u, int pr, int lv, long long cost) {
  par[u] = pr;
  lev[u] = lv;
  dism[u] = cost;
  for (int i = 0; i < (int)g[u].size(); ++i) {
    int v = g[u][i];
    if (v != pr) dfs(v, u, lv + 1, cost + c[u][i]);
  }
}
void lca_init() {
  dfs(1, -1, 0, 0);
  memset(sp, -1, sizeof sp);
  for (int i = 1; i <= n; ++i) sp[i][0] = par[i];
  for (int j = 1; (1 << j) < n; ++j) {
    for (int i = 1; i <= n; ++i) {
      if (sp[i][j - 1] != -1) sp[i][j] = sp[sp[i][j - 1]][j - 1];
    }
  }
}
int lca(int u, int v) {
  if (lev[u] < lev[v]) swap(u, v);
  int logn = 0;
  while ((1 << (1 + logn)) <= n) ++logn;
  for (int i = logn; i >= 0; --i) {
    if (lev[u] - (1 << i) >= lev[v]) u = sp[u][i];
  }
  if (u == v) return u;
  for (int i = logn; i >= 0; --i) {
    if (sp[u][i] != -1 and sp[u][i] != sp[v][i]) {
      u = sp[u][i];
      v = sp[v][i];
    }
  }
  return par[u];
}
long long d[45][N];
bool vis[45][N];
void dijkstra(int idx) {
  int src = nottaken[idx];
  d[idx][src] = 0;
  vis[idx][src] = true;
  priority_queue<pair<long long, int> > pq;
  pq.push(make_pair(0LL, src));
  while (!pq.empty()) {
    auto top = pq.top();
    pq.pop();
    long long dd = -top.first;
    int u = top.second;
    if (dd > d[idx][u]) continue;
    for (int i = 0; i < (int)G[u].size(); ++i) {
      int v = G[u][i];
      long long w = C[u][i];
      long long temp = dd + w;
      if (!vis[idx][v] or d[idx][v] > temp) {
        vis[idx][v] = true;
        d[idx][v] = temp;
        pq.push(make_pair(-temp, v));
      }
    }
  }
}
long long getdis(int u, int v) {
  long long ret = INF;
  for (auto p : lfted) {
    int a = id[p.u];
    int b = id[p.v];
    long long temp = min(d[a][u] + d[b][v], d[a][v] + d[b][u]);
    temp += p.w;
    ret = min(ret, temp);
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    int u, v, w;
    cin >> u >> v >> w;
    G[u].push_back(v);
    G[v].push_back(u);
    C[u].push_back(w);
    C[v].push_back(w);
    e.push_back(edge(u, v, w));
  }
  mst(n);
  lca_init();
  for (int i = 0; i < (int)nottaken.size(); ++i) dijkstra(i);
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    int who = lca(u, v);
    long long res = dism[u] - dism[who] + dism[v] - dism[who];
    res = min(res, getdis(u, v));
    cout << res << "\n";
  }
  return 0;
}
