#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
struct edge {
  int nxt, to, w;
} e[2 * maxn];
struct node {
  int id;
  long long dis;
  bool operator<(const node &cwq) const { return dis > cwq.dis; }
};
int n, m, Q;
int head[maxn], cnt;
int u[maxn], v[maxn], w[maxn];
int vis[maxn], book[maxn], dep[maxn], fa[maxn], f[maxn][25];
long long d[maxn], dis[45][maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void add(int u, int v, int w) {
  e[++cnt].nxt = head[u];
  e[cnt].to = v;
  e[cnt].w = w;
  head[u] = cnt;
}
void dfs(int u, int p, long long w) {
  dep[u] = dep[p] + 1;
  fa[u] = p;
  f[u][0] = p;
  d[u] = w;
  for (int i = 1; i <= 19; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == p) continue;
    dfs(v, u, w + 1ll * e[i].w);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 19; i >= 0; i--)
    if (dep[x] - (1 << i) >= dep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
void DJ(int sx, int t) {
  for (int i = 1; i <= n; i++) dis[t][i] = 1e15, vis[i] = 0;
  priority_queue<node> q;
  q.push(node{sx, 0});
  dis[t][sx] = 0;
  while (!q.empty()) {
    int u = q.top().id;
    q.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (dis[t][v] > dis[t][u] + 1l * e[i].w) {
        dis[t][v] = dis[t][u] + 1ll * e[i].w;
        q.push(node{v, dis[t][v]});
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &u[i], &v[i], &w[i]);
  for (int i = 1; i <= m; i++) {
    int fx = find(u[i]);
    int fy = find(v[i]);
    if (fx != fy) {
      book[i] = 1;
      fa[fy] = fx;
      add(u[i], v[i], w[i]);
      add(v[i], u[i], w[i]);
    }
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= m; i++) {
    if (!book[i]) {
      add(u[i], v[i], w[i]);
      add(v[i], u[i], w[i]);
    }
  }
  int tot = 0;
  for (int i = 1; i <= m; i++) {
    if (!book[i]) {
      tot++;
      DJ(u[i], tot);
      tot++;
      DJ(v[i], tot);
    }
  }
  scanf("%d", &Q);
  while (Q--) {
    int x, y;
    scanf("%d %d", &x, &y);
    int fa = lca(x, y);
    long long ans = d[x] - d[fa] + d[y] - d[fa];
    for (int i = 1; i <= tot; i++) ans = min(dis[i][x] + dis[i][y], ans);
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Please create a solution in cpp 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 int inf = 0x3f3f3f3f;
const int maxn = 2e5 + 5;
struct edge {
  int nxt, to, w;
} e[2 * maxn];
struct node {
  int id;
  long long dis;
  bool operator<(const node &cwq) const { return dis > cwq.dis; }
};
int n, m, Q;
int head[maxn], cnt;
int u[maxn], v[maxn], w[maxn];
int vis[maxn], book[maxn], dep[maxn], fa[maxn], f[maxn][25];
long long d[maxn], dis[45][maxn];
int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
void add(int u, int v, int w) {
  e[++cnt].nxt = head[u];
  e[cnt].to = v;
  e[cnt].w = w;
  head[u] = cnt;
}
void dfs(int u, int p, long long w) {
  dep[u] = dep[p] + 1;
  fa[u] = p;
  f[u][0] = p;
  d[u] = w;
  for (int i = 1; i <= 19; i++) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int i = head[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v == p) continue;
    dfs(v, u, w + 1ll * e[i].w);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int i = 19; i >= 0; i--)
    if (dep[x] - (1 << i) >= dep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 19; i >= 0; i--)
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  return f[x][0];
}
void DJ(int sx, int t) {
  for (int i = 1; i <= n; i++) dis[t][i] = 1e15, vis[i] = 0;
  priority_queue<node> q;
  q.push(node{sx, 0});
  dis[t][sx] = 0;
  while (!q.empty()) {
    int u = q.top().id;
    q.pop();
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = head[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (dis[t][v] > dis[t][u] + 1l * e[i].w) {
        dis[t][v] = dis[t][u] + 1ll * e[i].w;
        q.push(node{v, dis[t][v]});
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= m; i++) scanf("%d%d%d", &u[i], &v[i], &w[i]);
  for (int i = 1; i <= m; i++) {
    int fx = find(u[i]);
    int fy = find(v[i]);
    if (fx != fy) {
      book[i] = 1;
      fa[fy] = fx;
      add(u[i], v[i], w[i]);
      add(v[i], u[i], w[i]);
    }
  }
  dfs(1, 0, 0);
  for (int i = 1; i <= m; i++) {
    if (!book[i]) {
      add(u[i], v[i], w[i]);
      add(v[i], u[i], w[i]);
    }
  }
  int tot = 0;
  for (int i = 1; i <= m; i++) {
    if (!book[i]) {
      tot++;
      DJ(u[i], tot);
      tot++;
      DJ(v[i], tot);
    }
  }
  scanf("%d", &Q);
  while (Q--) {
    int x, y;
    scanf("%d %d", &x, &y);
    int fa = lca(x, y);
    long long ans = d[x] - d[fa] + d[y] - d[fa];
    for (int i = 1; i <= tot; i++) ans = min(dis[i][x] + dis[i][y], ans);
    printf("%I64d\n", ans);
  }
  return 0;
}

```