#include <bits/stdc++.h>
using namespace std;
int M, n, m, k, q, x, y, tot, a[200010], b[200010], c[200010], fa[200010],
    head[200010], exist[200010], dep[200010], p[200010][31];
long long dist[200010], d[41][200010];
struct ed {
  int to, nxt;
  long long val;
} edge[200010 << 1];
void addedge(int u, int v, long long w) {
  edge[++M] = (ed){v, head[u], w};
  head[u] = M;
  edge[++M] = (ed){u, head[v], w};
  head[v] = M;
}
void dfs(int u, int f, int d, long long ds) {
  p[u][0] = f;
  dep[u] = d;
  dist[u] = ds;
  int v;
  for (int i = head[u]; i; i = edge[i].nxt)
    if ((v = edge[i].to) != f) dfs(v, u, d + 1, ds + edge[i].val);
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) {
    int t = x;
    x = y;
    y = t;
  }
  for (int i = 20; i >= 0; --i)
    if (dep[x] - (1 << i) >= dep[y]) x = p[x][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; --i)
    if (p[x][i] != p[y][i]) x = p[x][i], y = p[y][i];
  return p[x][0];
}
int find(int x) {
  if (x != fa[x]) fa[x] = find(fa[x]);
  return fa[x];
}
struct node {
  int id;
  long long val;
} tr[200010];
void push(node x) {
  tr[++tot] = x;
  int i = tot;
  node t;
  while (i > 1 && tr[i].val < tr[i >> 1].val)
    t = tr[i], tr[i] = tr[i >> 1], tr[i >> 1] = t, i >>= 1;
}
void pop() {
  tr[1] = tr[tot--];
  int i = 2;
  node t;
  while (i <= tot) {
    if (i < tot && tr[i + 1].val < tr[i].val) ++i;
    if (tr[i].val >= tr[i >> 1].val) return;
    t = tr[i], tr[i] = tr[i >> 1], tr[i >> 1] = t, i <<= 1;
  }
}
void solve(int s, long long dis[]) {
  for (int i = 1; i <= n; ++i) dis[i] = 1ll << 60;
  memset(exist, 0, sizeof(exist));
  tr[tot = 1] = (node){s, dis[s] = 0};
  while (tot) {
    while (tot && exist[tr[1].id]) pop();
    if (!tot) return;
    int u = tr[1].id, v;
    exist[u] = 1;
    pop();
    for (int i = head[u]; i; i = edge[i].nxt)
      if (!exist[v = edge[i].to] && dis[v] > dis[u] + edge[i].val)
        dis[v] = dis[u] + edge[i].val, push((node){v, dis[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%I64d", &a[i], &b[i], &c[i]);
    int fx = find(a[i]), fy = find(b[i]);
    if (fx != fy)
      fa[fx] = fy, addedge(a[i], b[i], c[i]);
    else
      exist[a[i]] = 1;
  }
  dfs(1, 1, 1, 0);
  for (int j = 1; j <= 20; ++j)
    for (int i = 1; i <= n; ++i) p[i][j] = p[p[i][j - 1]][j - 1];
  memset(head, 0, sizeof(head));
  M = 0;
  for (int i = 1; i <= m; ++i) addedge(a[i], b[i], c[i]);
  for (int i = 1; i <= n; ++i)
    if (exist[i]) a[++k] = i;
  for (int i = 1; i <= k; ++i) solve(a[i], d[i]);
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &x, &y);
    int z = lca(x, y);
    long long ans = dist[x] + dist[y] - 2ll * dist[z];
    for (int j = 1; j <= k; ++j) ans = min(ans, d[j][x] + d[j][y]);
    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;
int M, n, m, k, q, x, y, tot, a[200010], b[200010], c[200010], fa[200010],
    head[200010], exist[200010], dep[200010], p[200010][31];
long long dist[200010], d[41][200010];
struct ed {
  int to, nxt;
  long long val;
} edge[200010 << 1];
void addedge(int u, int v, long long w) {
  edge[++M] = (ed){v, head[u], w};
  head[u] = M;
  edge[++M] = (ed){u, head[v], w};
  head[v] = M;
}
void dfs(int u, int f, int d, long long ds) {
  p[u][0] = f;
  dep[u] = d;
  dist[u] = ds;
  int v;
  for (int i = head[u]; i; i = edge[i].nxt)
    if ((v = edge[i].to) != f) dfs(v, u, d + 1, ds + edge[i].val);
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) {
    int t = x;
    x = y;
    y = t;
  }
  for (int i = 20; i >= 0; --i)
    if (dep[x] - (1 << i) >= dep[y]) x = p[x][i];
  if (x == y) return x;
  for (int i = 20; i >= 0; --i)
    if (p[x][i] != p[y][i]) x = p[x][i], y = p[y][i];
  return p[x][0];
}
int find(int x) {
  if (x != fa[x]) fa[x] = find(fa[x]);
  return fa[x];
}
struct node {
  int id;
  long long val;
} tr[200010];
void push(node x) {
  tr[++tot] = x;
  int i = tot;
  node t;
  while (i > 1 && tr[i].val < tr[i >> 1].val)
    t = tr[i], tr[i] = tr[i >> 1], tr[i >> 1] = t, i >>= 1;
}
void pop() {
  tr[1] = tr[tot--];
  int i = 2;
  node t;
  while (i <= tot) {
    if (i < tot && tr[i + 1].val < tr[i].val) ++i;
    if (tr[i].val >= tr[i >> 1].val) return;
    t = tr[i], tr[i] = tr[i >> 1], tr[i >> 1] = t, i <<= 1;
  }
}
void solve(int s, long long dis[]) {
  for (int i = 1; i <= n; ++i) dis[i] = 1ll << 60;
  memset(exist, 0, sizeof(exist));
  tr[tot = 1] = (node){s, dis[s] = 0};
  while (tot) {
    while (tot && exist[tr[1].id]) pop();
    if (!tot) return;
    int u = tr[1].id, v;
    exist[u] = 1;
    pop();
    for (int i = head[u]; i; i = edge[i].nxt)
      if (!exist[v = edge[i].to] && dis[v] > dis[u] + edge[i].val)
        dis[v] = dis[u] + edge[i].val, push((node){v, dis[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%I64d", &a[i], &b[i], &c[i]);
    int fx = find(a[i]), fy = find(b[i]);
    if (fx != fy)
      fa[fx] = fy, addedge(a[i], b[i], c[i]);
    else
      exist[a[i]] = 1;
  }
  dfs(1, 1, 1, 0);
  for (int j = 1; j <= 20; ++j)
    for (int i = 1; i <= n; ++i) p[i][j] = p[p[i][j - 1]][j - 1];
  memset(head, 0, sizeof(head));
  M = 0;
  for (int i = 1; i <= m; ++i) addedge(a[i], b[i], c[i]);
  for (int i = 1; i <= n; ++i)
    if (exist[i]) a[++k] = i;
  for (int i = 1; i <= k; ++i) solve(a[i], d[i]);
  scanf("%d", &q);
  for (int i = 1; i <= q; ++i) {
    scanf("%d%d", &x, &y);
    int z = lca(x, y);
    long long ans = dist[x] + dist[y] - 2ll * dist[z];
    for (int j = 1; j <= k; ++j) ans = min(ans, d[j][x] + d[j][y]);
    printf("%I64d\n", ans);
  }
  return 0;
}

```