#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200000 + 5;
long long n, m, q, dis[maxn], tot, head[maxn], f[maxn][30], depth[maxn],
    fp[maxn];
long long fa[maxn], cntfp, fdis[45][maxn];
bool vis[maxn], dijvis[maxn];
struct edge {
  long long u, v, wi;
} e[maxn << 2];
struct edges {
  long long nxt, v, wi;
} E[maxn << 2];
bool cmp(edge a, edge b) {
  if (a.wi != b.wi)
    return a.wi < b.wi;
  else
    return a.u > b.u;
}
inline long long get(long long x) {
  if (fa[x] == x) return x;
  return fa[x] = get(fa[x]);
}
inline long long lca(long long x, long long y) {
  if (depth[x] < depth[y]) swap(x, y);
  for (long long i = 20; i >= 0; i--)
    if (depth[f[x][i]] >= depth[y]) x = f[x][i];
  if (x == y) return x;
  for (long long i = 20; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
inline void add(long long u, long long v, long long w) {
  tot++;
  E[tot].nxt = head[u];
  E[tot].v = v;
  E[tot].wi = w;
  head[u] = tot;
}
inline void kustarl() {
  for (long long i = 1; i <= n; i++) fa[i] = i;
  for (long long i = 1; i <= m; i++) {
    long long x = get(e[i].u), y = get(e[i].v);
    if (x != y) {
      fa[x] = y;
      add(e[i].u, e[i].v, e[i].wi);
      add(e[i].v, e[i].u, e[i].wi);
    } else
      fp[i] = 1;
  }
}
void dfs(long long x) {
  vis[x] = 1;
  for (long long i = head[x]; i; i = E[i].nxt) {
    long long to = E[i].v;
    if (!vis[to] && to != x) {
      dis[to] = dis[x] + E[i].wi, depth[to] = depth[x] + 1;
      f[to][0] = x;
      dfs(to);
    }
  }
}
struct fsb {
  long long pos, dis;
};
bool operator<(fsb a, fsb b) { return a.dis > b.dis; }
priority_queue<fsb> qu;
inline void dij(long long x) {
  cntfp++;
  memset(dijvis, 0, sizeof dijvis);
  fdis[cntfp][x] = 0;
  qu.push((fsb){x, fdis[cntfp][x]});
  while (!qu.empty()) {
    fsb y = qu.top();
    qu.pop();
    int u = y.pos;
    if (dijvis[u]) continue;
    dijvis[u] = 1;
    for (long long i = head[u]; i; i = E[i].nxt) {
      long long to = E[i].v;
      if (fdis[cntfp][to] > fdis[cntfp][u] + E[i].wi) {
        fdis[cntfp][to] = fdis[cntfp][u] + E[i].wi;
        qu.push((fsb){to, fdis[cntfp][to]});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  memset(fdis, 0x7f, sizeof(fdis));
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) cin >> e[i].u >> e[i].v >> e[i].wi;
  sort(e + 1, e + m + 1, cmp);
  kustarl();
  for (long long i = 1; i <= n; i++)
    if (!vis[i]) {
      depth[i] = 1;
      dfs(i);
    }
  for (long long j = 1; j <= 20; j++)
    for (long long i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
  for (long long i = 1; i <= m; i++) {
    if (fp[i]) {
      add(e[i].u, e[i].v, e[i].wi);
      add(e[i].v, e[i].u, e[i].wi);
    }
  }
  for (long long i = 1; i <= m; i++) {
    if (fp[i]) {
      dij(e[i].u);
      dij(e[i].v);
    }
  }
  cin >> q;
  long long u, v, ans, faq;
  for (long long i = 1; i <= q; i++) {
    cin >> u >> v;
    faq = lca(u, v);
    ans = dis[u] + dis[v] - 2 * dis[faq];
    for (long long j = 1; j <= cntfp; j++) {
      ans = min(fdis[j][u] + fdis[j][v], ans);
    }
    cout << ans << endl;
  }
  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;
const long long maxn = 200000 + 5;
long long n, m, q, dis[maxn], tot, head[maxn], f[maxn][30], depth[maxn],
    fp[maxn];
long long fa[maxn], cntfp, fdis[45][maxn];
bool vis[maxn], dijvis[maxn];
struct edge {
  long long u, v, wi;
} e[maxn << 2];
struct edges {
  long long nxt, v, wi;
} E[maxn << 2];
bool cmp(edge a, edge b) {
  if (a.wi != b.wi)
    return a.wi < b.wi;
  else
    return a.u > b.u;
}
inline long long get(long long x) {
  if (fa[x] == x) return x;
  return fa[x] = get(fa[x]);
}
inline long long lca(long long x, long long y) {
  if (depth[x] < depth[y]) swap(x, y);
  for (long long i = 20; i >= 0; i--)
    if (depth[f[x][i]] >= depth[y]) x = f[x][i];
  if (x == y) return x;
  for (long long i = 20; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
inline void add(long long u, long long v, long long w) {
  tot++;
  E[tot].nxt = head[u];
  E[tot].v = v;
  E[tot].wi = w;
  head[u] = tot;
}
inline void kustarl() {
  for (long long i = 1; i <= n; i++) fa[i] = i;
  for (long long i = 1; i <= m; i++) {
    long long x = get(e[i].u), y = get(e[i].v);
    if (x != y) {
      fa[x] = y;
      add(e[i].u, e[i].v, e[i].wi);
      add(e[i].v, e[i].u, e[i].wi);
    } else
      fp[i] = 1;
  }
}
void dfs(long long x) {
  vis[x] = 1;
  for (long long i = head[x]; i; i = E[i].nxt) {
    long long to = E[i].v;
    if (!vis[to] && to != x) {
      dis[to] = dis[x] + E[i].wi, depth[to] = depth[x] + 1;
      f[to][0] = x;
      dfs(to);
    }
  }
}
struct fsb {
  long long pos, dis;
};
bool operator<(fsb a, fsb b) { return a.dis > b.dis; }
priority_queue<fsb> qu;
inline void dij(long long x) {
  cntfp++;
  memset(dijvis, 0, sizeof dijvis);
  fdis[cntfp][x] = 0;
  qu.push((fsb){x, fdis[cntfp][x]});
  while (!qu.empty()) {
    fsb y = qu.top();
    qu.pop();
    int u = y.pos;
    if (dijvis[u]) continue;
    dijvis[u] = 1;
    for (long long i = head[u]; i; i = E[i].nxt) {
      long long to = E[i].v;
      if (fdis[cntfp][to] > fdis[cntfp][u] + E[i].wi) {
        fdis[cntfp][to] = fdis[cntfp][u] + E[i].wi;
        qu.push((fsb){to, fdis[cntfp][to]});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  memset(fdis, 0x7f, sizeof(fdis));
  cin >> n >> m;
  for (long long i = 1; i <= m; i++) cin >> e[i].u >> e[i].v >> e[i].wi;
  sort(e + 1, e + m + 1, cmp);
  kustarl();
  for (long long i = 1; i <= n; i++)
    if (!vis[i]) {
      depth[i] = 1;
      dfs(i);
    }
  for (long long j = 1; j <= 20; j++)
    for (long long i = 1; i <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
  for (long long i = 1; i <= m; i++) {
    if (fp[i]) {
      add(e[i].u, e[i].v, e[i].wi);
      add(e[i].v, e[i].u, e[i].wi);
    }
  }
  for (long long i = 1; i <= m; i++) {
    if (fp[i]) {
      dij(e[i].u);
      dij(e[i].v);
    }
  }
  cin >> q;
  long long u, v, ans, faq;
  for (long long i = 1; i <= q; i++) {
    cin >> u >> v;
    faq = lca(u, v);
    ans = dis[u] + dis[v] - 2 * dis[faq];
    for (long long j = 1; j <= cntfp; j++) {
      ans = min(fdis[j][u] + fdis[j][v], ans);
    }
    cout << ans << endl;
  }
  return 0;
}

```