#include <bits/stdc++.h>
using namespace std;
struct ins {
  int ans;
  ins() { ans = 1; }
  void read() {}
  void read1(char &s) {
    char c = getchar();
    for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
      ;
    s = c;
    if (c == EOF) ans = 0;
  }
  void read1(string &s) {
    s = "";
    char c = getchar();
    for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
      ;
    for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar())
      s += c;
    if (c == EOF) ans = 0;
  }
  void read1(char *s) {
    char c = getchar();
    int tt = 0;
    for (; !isprint(c) || c == ' ' || c == '\n' || c == '\t'; c = getchar())
      ;
    for (; isprint(c) && c != ' ' && c != '\n' && c != '\t'; c = getchar())
      s[tt++] = c;
    s[tt] = '\0';
    if (c == EOF) ans = 0;
  }
  template <typename T>
  void read1(T &n) {
    T x = 0;
    int f = 1;
    char c = getchar();
    for (; !isdigit(c); c = getchar()) {
      if (c == '-') f = -1;
      if (c == EOF) {
        ans = 0;
        return;
      }
    }
    for (; isdigit(c); c = getchar()) x = x * 10 + c - 48;
    n = x * f;
    if (c == EOF) ans = 0;
    if (c != '.') return;
    T l = 0.1;
    while ((c = getchar()) <= '9' && c >= '0') x = x + (c & 15) * l, l *= 0.1;
    n = x * f;
    if (c == EOF) ans = 0;
  }
  void write() {}
  void write1(string s) {
    int n = s.size();
    for (int i = 0; i < n; i++) putchar(s[i]);
  }
  void write1(const char *s) {
    int n = strlen(s);
    for (int i = 0; i < n; i++) putchar(s[i]);
  }
  void write1(char *s) {
    int n = strlen(s);
    for (int i = 0; i < n; i++) putchar(s[i]);
  }
  void write1(char s) { putchar(s); }
  void write1(float s, int x = 6) {
    char y[10001];
    sprintf(y, "%%.%df", x);
    printf(y, s);
  }
  void write1(double s, int x = 6) {
    char y[10001];
    sprintf(y, "%%.%dlf", x);
    printf(y, s);
  }
  void write1(long double s, int x = 6) {
    char y[10001];
    sprintf(y, "%%.%dLf", x);
    printf(y, s);
  }
  template <typename T>
  void write1(T n) {
    if (n < 0) n = -n, putchar('-');
    if (n > 9) write1(n / 10);
    putchar('0' + n % 10);
  }
  friend ins operator>>(ins x, char *n);
  template <typename T>
  friend ins operator>>(ins x, T &n);
  template <typename T>
  friend ins operator<<(ins x, T n);
  operator bool() { return ans; }
};
ins operator>>(ins x, char *n) {
  if (!x.ans) return x;
  x.read1(n);
  return x;
}
template <typename T>
ins operator>>(ins x, T &n) {
  if (!x.ans) return x;
  x.read1(n);
  return x;
}
template <typename T>
ins operator<<(ins x, T n) {
  x.write1(n);
  return x;
}
ins yin;
ins yout;
const long long maxn = 1e5 + 10;
vector<pair<long long, long long>> e[maxn];
long long de[maxn], di[maxn], vi[maxn << 1][23], vis[maxn];
long long tot;
set<long long> qq;
void dfs(long long u, long long fa) {
  vi[vis[u] = ++tot][0] = u;
  for (auto x : e[u]) {
    long long v = x.first, w = x.second;
    if (vis[v]) {
      if (v == fa) continue;
      qq.insert(u);
      qq.insert(v);
      continue;
    }
    di[v] = di[u] + w;
    de[v] = de[u] + 1;
    dfs(v, u);
    vi[++tot][0] = u;
  }
}
long long lg[maxn << 1];
long long merge(long long x, long long y) { return de[x] < de[y] ? x : y; }
long long lca(long long x, long long y) {
  if (vis[x] > vis[y]) swap(x, y);
  long long tmp = lg[vis[y] - vis[x] + 1];
  return merge(vi[vis[x]][tmp], vi[vis[y] - (1 << tmp) + 1][tmp]);
}
long long dis[50][maxn];
long long c[maxn];
long long n, m;
void dij(long long x, long long dis[]) {
  priority_queue<pair<long long, long long>> q;
  dis[x] = 0;
  q.push({0, x});
  while (!q.empty()) {
    long long u = q.top().second, ee = q.top().first;
    q.pop();
    if (dis[u] != -ee) continue;
    for (auto x : e[u]) {
      long long v = x.first, w = x.second;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        q.push({-dis[v], v});
      }
    }
  }
}
signed main() {
  yin >> n >> m;
  for (long long i = 1; i <= m; i++) {
    long long u;
    yin >> u;
    ;
    long long v;
    yin >> v;
    ;
    long long w;
    yin >> w;
    ;
    e[u].push_back({v, w});
    e[v].push_back({u, w});
  }
  dfs(1, 0);
  for (long long i = 2; i <= n * 2; i++) lg[i] = lg[i >> 1] + 1;
  for (long long i = 1; i <= lg[n * 2]; i++)
    for (long long j = 1; j <= tot - (1 << i) + 1; j++)
      vi[j][i] = merge(vi[j][i - 1], vi[j + (1 << (i - 1))][i - 1]);
  tot = 0;
  memset(dis, 0x7f7f7f7f, sizeof(dis));
  for (auto x : qq) {
    c[x] = ++tot;
    dij(x, dis[tot]);
  }
  long long TTT;
  yin >> TTT;
  ;
  while (TTT--) {
    long long x;
    yin >> x;
    ;
    long long y;
    yin >> y;
    ;
    long long ans = di[x] + di[y] - 2 * di[lca(x, y)];
    for (auto xx : qq) {
      ans = min(ans, dis[c[xx]][y] + dis[c[xx]][x]);
    }
    yout << ans << '\n';
  }
  return 0;
}
