#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long sum = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar();
  return sum * ff;
}
const long long mod = 1e9 + 7;
const long long mo = 998244353;
const long long N = 1e5 + 5;
long long n, m, cnt, f[N][23], dep[N], head[N];
long long pa[N], vis[N], dis[55][N], siz[N];
vector<pair<long long, long long> > G[N], g[N];
vector<long long> rem;
struct Node {
  long long nex, to, w;
};
Node e[N << 1];
struct number {
  long long x, y, w;
};
number a[N << 1];
inline void jia(long long u, long long v, long long w) {
  e[++cnt].nex = head[u];
  head[u] = cnt;
  e[cnt].to = v;
  e[cnt].w = w;
}
inline void dfs(long long u, long long fa) {
  dep[u] = dep[fa] + 1;
  f[u][0] = fa;
  for (long long i = (1); i <= (21); i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (long long i = head[u]; i; i = e[i].nex) {
    long long v = e[i].to;
    if (v == fa) continue;
    siz[v] = siz[u] + e[i].w;
    dfs(v, u);
  }
}
inline long long LCA(long long x, long long y) {
  if (dep[x] > dep[y]) swap(x, y);
  for (long long i = (21); i >= (0); i--)
    if (dep[y] - (1 << i) >= dep[x]) y = f[y][i];
  if (x == y) return x;
  for (long long i = (21); i >= (0); i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
inline long long calc(long long x, long long y) {
  long long z = LCA(x, y);
  return siz[x] + siz[y] - 2ll * siz[z];
}
inline long long find(long long x) {
  if (x == pa[x]) return x;
  return pa[x] = find(pa[x]);
}
inline bool merge(long long x, long long y) {
  x = find(x), y = find(y);
  if (x ^ y) {
    pa[x] = y;
    return true;
  }
  return false;
}
inline void spfa(long long s, long long *dis) {
  memset(vis, 0, sizeof(vis));
  for (long long i = (1); i <= (n); i++) dis[i] = 1e12;
  queue<long long> q;
  q.push(s);
  dis[s] = 0, vis[s] = 1;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    vis[u] = 0;
    for (long long i = (0); i <= ((long long)G[u].size() - 1); i++) {
      long long v = G[u][i].first;
      long long w = G[u][i].second;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
    }
  }
}
signed main() {
  n = read(), m = read();
  for (long long i = (1); i <= (m); i++) {
    long long x, y, z;
    x = read(), y = read(), z = read();
    a[i] = (number){x, y, z};
    G[x].push_back(make_pair(y, z));
    G[y].push_back(make_pair(x, z));
  }
  for (long long i = (1); i <= (n); i++) pa[i] = i;
  for (long long i = (1); i <= (m); i++) {
    long long x = a[i].x, y = a[i].y;
    if (merge(x, y)) {
      jia(x, y, a[i].w);
      jia(y, x, a[i].w);
    } else {
      rem.push_back(x), rem.push_back(y);
    }
  }
  dfs(1, 0);
  sort(rem.begin(), rem.end());
  rem.erase(unique(rem.begin(), rem.end()), rem.end());
  long long tot = 0;
  for (long long i = (0); i <= ((long long)rem.size() - 1); i++)
    spfa(rem[i], dis[++tot]);
  long long Q = read();
  for (; Q--;) {
    long long x, y;
    x = read(), y = read();
    long long sum = calc(x, y);
    for (long long i = (1); i <= (tot); i++)
      sum = min(sum, dis[i][x] + dis[i][y]);
    printf("%lld\n", sum);
  }
  return 0;
}

### Prompt

Your challenge is to write a cpp solution to the following problem:

You are given a weighed undirected connected graph, consisting of n vertices and m edges.

You should answer q queries, the i-th query is to find the shortest distance between vertices u_i and v_i.

Input

The first line contains two integers n and m~(1 ≤ n, m ≤ 10^5, m - n ≤ 20) — the number of vertices and edges in the graph.

Next m lines contain the edges: the i-th edge is a triple of integers v_i, u_i, d_i~(1 ≤ u_i, v_i ≤ n, 1 ≤ d_i ≤ 10^9, u_i ≠ v_i). This triple means that there is an edge between vertices u_i and v_i of weight d_i. It is guaranteed that graph contains no self-loops and multiple edges.

The next line contains a single integer q~(1 ≤ q ≤ 10^5) — the number of queries.

Each of the next q lines contains two integers u_i and v_i~(1 ≤ u_i, v_i ≤ n) — descriptions of the queries.

Pay attention to the restriction m - n ~ ≤ ~ 20.

Output

Print q lines.

The i-th line should contain the answer to the i-th query — the shortest distance between vertices u_i and v_i.

Examples

Input

3 3
1 2 3
2 3 1
3 1 5
3
1 2
1 3
2 3


Output

3
4
1


Input

8 13
1 2 4
2 3 6
3 4 1
4 5 12
5 6 3
6 7 8
7 8 7
1 4 1
1 8 3
2 6 9
2 7 1
4 6 3
6 8 2
8
1 5
1 7
2 3
2 8
3 7
3 4
6 8
7 8


Output

7
5
6
7
7
1
2
7

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long sum = 0, ff = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (isdigit(ch)) sum = sum * 10 + (ch ^ 48), ch = getchar();
  return sum * ff;
}
const long long mod = 1e9 + 7;
const long long mo = 998244353;
const long long N = 1e5 + 5;
long long n, m, cnt, f[N][23], dep[N], head[N];
long long pa[N], vis[N], dis[55][N], siz[N];
vector<pair<long long, long long> > G[N], g[N];
vector<long long> rem;
struct Node {
  long long nex, to, w;
};
Node e[N << 1];
struct number {
  long long x, y, w;
};
number a[N << 1];
inline void jia(long long u, long long v, long long w) {
  e[++cnt].nex = head[u];
  head[u] = cnt;
  e[cnt].to = v;
  e[cnt].w = w;
}
inline void dfs(long long u, long long fa) {
  dep[u] = dep[fa] + 1;
  f[u][0] = fa;
  for (long long i = (1); i <= (21); i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (long long i = head[u]; i; i = e[i].nex) {
    long long v = e[i].to;
    if (v == fa) continue;
    siz[v] = siz[u] + e[i].w;
    dfs(v, u);
  }
}
inline long long LCA(long long x, long long y) {
  if (dep[x] > dep[y]) swap(x, y);
  for (long long i = (21); i >= (0); i--)
    if (dep[y] - (1 << i) >= dep[x]) y = f[y][i];
  if (x == y) return x;
  for (long long i = (21); i >= (0); i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
inline long long calc(long long x, long long y) {
  long long z = LCA(x, y);
  return siz[x] + siz[y] - 2ll * siz[z];
}
inline long long find(long long x) {
  if (x == pa[x]) return x;
  return pa[x] = find(pa[x]);
}
inline bool merge(long long x, long long y) {
  x = find(x), y = find(y);
  if (x ^ y) {
    pa[x] = y;
    return true;
  }
  return false;
}
inline void spfa(long long s, long long *dis) {
  memset(vis, 0, sizeof(vis));
  for (long long i = (1); i <= (n); i++) dis[i] = 1e12;
  queue<long long> q;
  q.push(s);
  dis[s] = 0, vis[s] = 1;
  while (!q.empty()) {
    long long u = q.front();
    q.pop();
    vis[u] = 0;
    for (long long i = (0); i <= ((long long)G[u].size() - 1); i++) {
      long long v = G[u][i].first;
      long long w = G[u][i].second;
      if (dis[v] > dis[u] + w) {
        dis[v] = dis[u] + w;
        if (!vis[v]) vis[v] = 1, q.push(v);
      }
    }
  }
}
signed main() {
  n = read(), m = read();
  for (long long i = (1); i <= (m); i++) {
    long long x, y, z;
    x = read(), y = read(), z = read();
    a[i] = (number){x, y, z};
    G[x].push_back(make_pair(y, z));
    G[y].push_back(make_pair(x, z));
  }
  for (long long i = (1); i <= (n); i++) pa[i] = i;
  for (long long i = (1); i <= (m); i++) {
    long long x = a[i].x, y = a[i].y;
    if (merge(x, y)) {
      jia(x, y, a[i].w);
      jia(y, x, a[i].w);
    } else {
      rem.push_back(x), rem.push_back(y);
    }
  }
  dfs(1, 0);
  sort(rem.begin(), rem.end());
  rem.erase(unique(rem.begin(), rem.end()), rem.end());
  long long tot = 0;
  for (long long i = (0); i <= ((long long)rem.size() - 1); i++)
    spfa(rem[i], dis[++tot]);
  long long Q = read();
  for (; Q--;) {
    long long x, y;
    x = read(), y = read();
    long long sum = calc(x, y);
    for (long long i = (1); i <= (tot); i++)
      sum = min(sum, dis[i][x] + dis[i][y]);
    printf("%lld\n", sum);
  }
  return 0;
}

```