#include <bits/stdc++.h>
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
using namespace std;
bool Debug;
const int mod = 1e9 + 7, MAXN = 1e5 + 7, inft = 1e9 + 1;
const long long infl = 1ll << 59;
const long double eps = 1e-7;
template <class T>
inline T sqr(T x) {
  return x * x;
}
template <class T>
inline T abs(T x) {
  return x >= 0 ? x : -x;
}
template <class A, class B>
inline A max(A a, B b) {
  return a > b ? a : b;
}
template <class A, class B>
inline A min(A a, B b) {
  return a < b ? a : b;
}
template <class A, class B>
inline void chmax(A &x, B y) {
  if (x < y) x = y;
}
template <class A, class B>
inline void chmin(A &x, B y) {
  if (x > y) x = y;
}
template <class A, class B>
inline A gcd(A a, B b) {
  if (a < b) swap(a, b);
  if (!b) return a;
  while (A t = a % b) a = b, b = t;
  return b;
}
template <class A, class B>
inline A lcm(A a, B b) {
  return a / gcd(a, b) * b;
}
template <class A, class B>
inline void amod(A &x, B y) {
  x += y;
  if (x >= mod) x -= mod;
}
template <class A, class B>
inline A Pow(A a, B b, int mod) {
  A ret;
  for (ret = 1; b; b >>= 1) {
    if (b & 1) ret = ret * 1ll * a % mod;
    a = a * 1ll * a % mod;
  }
  return ret % mod;
}
struct IO {
  struct Cg {
    inline int operator()() { return getchar(); }
  };
  struct Cp {
    inline void operator()(int x) { putchar(x); }
  };
  template <typename T>
  struct Fr {
    int f;
    T P;
    inline Fr &operator,(int &x) {
      x = 0;
      int t = P();
      while ((t < '0' || t > '9') && t != '-') t = P();
      f = 1;
      if (t == '-') t = P(), f = -1;
      x = t - '0';
      for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
      x *= f;
      return *this;
    }
    inline operator int() {
      int x;
      *this, x;
      return x;
    }
    inline Fr &operator,(long long &x) {
      x = 0;
      int t = P();
      while ((t < '0' || t > '9') && t != '-') t = P();
      f = 1;
      if (t == '-') t = P(), f = -1;
      x = t - '0';
      for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
      x *= f;
      return *this;
    }
    inline operator long long() {
      long long x;
      *this, x;
      return x;
    }
    inline Fr &operator,(char &x) {
      for (x = P(); (x == 10 || x == 13 || x == ' '); x = P())
        ;
      return *this;
    }
    inline operator char() {
      char x;
      *this, x;
      return x;
    }
    inline Fr &operator,(char *x) {
      char t = P();
      for (; (t == 10 || t == 13 || t == ' '); t = P())
        ;
      if (~t) {
        for (; !(t == 10 || t == 13 || t == ' ') && ~t; t = P()) *x++ = t;
      }
      *x++ = 0;
      return *this;
    }
    inline Fr &operator,(double &x) {
      x = 0;
      int t = P();
      while ((t < '0' || t > '9') && t != '-') t = P();
      f = 1;
      if (t == '-') t = P(), f = -1;
      x = t - '0';
      for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
      if (t == '.') {
        double u = 0.1;
        for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1)
          x += u * (t - '0');
      }
      if (f == -1) x = -x;
      ;
      return *this;
    }
    inline operator double() {
      double x;
      *this, x;
      return x;
    }
    inline Fr &operator,(long double &x) {
      x = 0;
      int t = P();
      while ((t < '0' || t > '9') && t != '-') t = P();
      f = 1;
      if (t == '-') t = P(), f = -1;
      x = t - '0';
      for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
      if (t == '.') {
        double u = 0.1;
        for (t = P(); t >= '0' && t <= '9'; t = P(), u *= 0.1)
          x += u * (t - '0');
      }
      if (f == -1) x = -x;
      ;
      return *this;
    }
    inline operator long double() {
      long double x;
      *this, x;
      return x;
    }
    inline Fr &operator,(unsigned int &x) {
      x = 0;
      int t = P();
      while (t < '0' || t > '9') t = P();
      x = t - '0';
      for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
      return *this;
    }
    inline operator unsigned int() {
      unsigned int x;
      *this, x;
      return x;
    }
    inline Fr &operator,(unsigned long long &x) {
      x = 0;
      int t = P();
      while (t < '0' || t > '9') t = P();
      x = t - '0';
      for (t = P(); t >= '0' && t <= '9'; t = P()) x = x * 10 + t - '0';
      return *this;
    }
    inline operator unsigned long long() {
      unsigned long long x;
      *this, x;
      return x;
    }
  };
  Fr<Cg> in;
  template <typename T>
  struct Fw {
    int c, s[24];
    T P;
    inline Fw &operator,(int x) {
      if (x) {
        if (x < 0) P('-'), x = -x;
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator()(int x) {
      if (x) {
        if (x < 0) P('-'), x = -x;
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator,(unsigned int x) {
      if (x) {
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator()(unsigned int x) {
      if (x) {
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator,(long long x) {
      if (x) {
        if (x < 0) P('-'), x = -x;
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator()(long long x) {
      if (x) {
        if (x < 0) P('-'), x = -x;
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator,(unsigned long long x) {
      if (x) {
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator()(unsigned long long x) {
      if (x) {
        c = 0;
        while (x) s[c++] = x % 10 + '0', x /= 10;
        while (c--) P(s[c]);
      } else
        P('0');
      return *this;
    }
    inline Fw &operator,(char x) {
      P(x);
      return *this;
    }
    inline Fw &operator()(char x) {
      P(x);
      return *this;
    }
    inline Fw &operator,(const char *x) {
      while (*x) P(*x++);
      return *this;
    }
    inline Fw &operator()(const char *x) {
      while (*x) P(*x++);
      return *this;
    }
    inline Fw &operator()(double x, int y) {
      if (y) {
        double t = 0.5;
        for (int i = y; i--;) t *= 0.1;
        if (x >= 0)
          x += t;
        else
          x -= t, P('-');
        *this, (long long)(abs(x));
        P('.');
        if (x < 0) x = -x;
        while (y--) {
          x *= 10;
          x -= floor(x * 0.1) * 10;
          P(((int)x) % 10 + '0');
        }
      } else if (x >= 0)
        *this, (long long)(x + 0.5);
      else
        *this, (long long)(x - 0.5);
      ;
      return *this;
    }
    inline Fw &operator()(long double x, int y) {
      if (y) {
        double t = 0.5;
        for (int i = y; i--;) t *= 0.1;
        if (x >= 0)
          x += t;
        else
          x -= t, P('-');
        *this, (long long)(abs(x));
        P('.');
        if (x < 0) x = -x;
        while (y--) {
          x *= 10;
          x -= floor(x * 0.1) * 10;
          P(((int)x) % 10 + '0');
        }
      } else if (x >= 0)
        *this, (long long)(x + 0.5);
      else
        *this, (long long)(x - 0.5);
      ;
      return *this;
    }
  };
  Fw<Cp> out;
} io;
struct Edge {
  int v;
  long long w;
  int next;
} G[MAXN << 1];
int head[MAXN], tot, anc[MAXN], son[MAXN], siz[MAXN], dep[MAXN], top[MAXN];
long long tdis[50][MAXN], dis[MAXN];
int n, m;
inline void add(int u, int v, long long w) {
  G[++tot] = (Edge){v, w, head[u]};
  head[u] = tot;
  G[++tot] = (Edge){u, w, head[v]};
  head[v] = tot;
}
struct Node {
  int u;
  long long w;
  Node() {}
  Node(int u, long long w) : u(u), w(w) {}
  bool operator<(const Node &rhs) const { return w > rhs.w; }
};
bitset<MAXN> vis, vis1;
priority_queue<Node> q;
inline void Dijkstra(long long *dis, int s) {
  vis.reset();
  fill(dis + 1, dis + 1 + n, infl);
  dis[s] = 0;
  q.push((Node){s, dis[s]});
  while (!q.empty()) {
    Node x = q.top();
    q.pop();
    int u = x.u;
    if (vis[u]) continue;
    for (int i = head[u]; i; i = G[i].next) {
      int v = G[i].v;
      long long w = G[i].w;
      if (dis[u] + w < dis[v]) {
        dis[v] = dis[u] + w;
        q.push((Node){v, dis[v]});
      }
    }
    vis[u] = 1;
  }
}
int cur[25], tot1, tot2, spe[500];
inline void dfs1(int u) {
  vis[u] = siz[u] = 1;
  dep[u] = dep[anc[u]] + 1;
  for (int i = head[u]; i; i = G[i].next) {
    if (G[i].v == anc[u])
      continue;
    else if (vis[G[i].v]) {
      spe[++tot2] = u, spe[++tot2] = G[i].v;
      continue;
    }
    anc[G[i].v] = u, dis[G[i].v] = dis[u] + G[i].w;
    dfs1(G[i].v), siz[u] += siz[G[i].v];
    if (siz[G[i].v] > siz[son[u]]) son[u] = G[i].v;
  }
}
inline void dfs2(int u, int t) {
  top[u] = t;
  vis[u] = 1;
  if (son[u]) dfs2(son[u], t);
  for (int i = head[u]; i; i = G[i].next)
    if (!vis[G[i].v] && G[i].v != son[u] && G[i].v != anc[u])
      dfs2(G[i].v, G[i].v);
}
inline int LCA(int u, int v) {
  while (top[u] != top[v]) {
    dep[top[u]] >= dep[top[v]] ? u = anc[top[u]] : v = anc[top[v]];
  }
  return dep[u] < dep[v] ? u : v;
}
int main() {
  io.in, n, m;
  for (int i = 1; i <= m; ++i) {
    int u, v;
    long long w;
    io.in, u, v, w;
    add(u, v, w);
  }
  dfs1(1);
  vis.reset();
  dfs2(1, 1);
  tot = 0;
  for (int i = 1; i <= tot2; ++i)
    if (!vis1[spe[i]]) vis1[spe[i]] = 1, Dijkstra(tdis[++tot], spe[i]);
  io.in, m;
  while (m--) {
    int u, v;
    long long ans = infl;
    io.in, u, v;
    int lca = LCA(u, v);
    chmin(ans, dis[u] + dis[v] - 2 * dis[lca]);
    for (int i = (1), iend = (tot); i <= iend; ++i)
      chmin(ans, tdis[i][u] + tdis[i][v]);
    io.out, ans, '\n';
  }
  return 0;
}
