#include <bits/stdc++.h>
using namespace std;
struct edge {
  long long nex, to, w;
} e[100100 << 1], te[100100 << 1];
struct node {
  long long x, y, z;
} t[100100];
bool vis[100100];
long long dis[100100], fa[100100], d[100100][22], dep[100100], tr[100100],
    trdis[60][100100], trcnt;
long long head[100100], thead[100100], cnt, tcnt;
long long n, m, q;
void add(long long x, long long y, long long z) {
  e[++cnt].nex = head[x];
  e[cnt].to = y;
  e[cnt].w = z;
  head[x] = cnt;
}
void add2(long long x, long long y, long long z) {
  te[++tcnt].nex = thead[x];
  te[tcnt].to = y;
  te[tcnt].w = z;
  thead[x] = tcnt;
}
long long fd(long long x) {
  if (fa[x] == x) return x;
  return fa[x] = fd(fa[x]);
}
bool cmp(node x, node y) { return x.z < y.z; }
void kr() {
  sort(t + 1, t + 1 + m, cmp);
  for (long long i = 1; i <= n; i++) fa[i] = i;
  long long sum = 0;
  for (long long i = 1; i <= m; i++) {
    long long x = t[i].x, y = t[i].y, z = t[i].z;
    long long fx = fd(x), fy = fd(y);
    if (fx == fy) {
      tr[++trcnt] = x;
      tr[++trcnt] = y;
    } else {
      fa[fy] = fx;
      add(x, y, z);
      add(y, x, z);
    }
  }
}
void dfs(long long x, long long faa) {
  dep[x] = dep[faa] + 1;
  d[x][0] = faa;
  for (long long i = 1; (1 << i) <= dep[x]; i++) {
    d[x][i] = d[d[x][i - 1]][i - 1];
  }
  for (long long i = head[x]; i; i = e[i].nex) {
    long long y = e[i].to;
    if (y != faa) {
      dis[y] = dis[x] + e[i].w;
      dfs(y, x);
    }
  }
}
long long lca(long long x, long long y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (long long i = 20; i >= 0; i--) {
    if (dep[d[x][i]] >= dep[y]) {
      x = d[x][i];
    }
  }
  if (x == y) return x;
  for (long long i = 20; i >= 0; i--) {
    if (d[x][i] != d[y][i]) {
      x = d[x][i];
      y = d[y][i];
    }
  }
  return d[x][0];
}
void spfa(long long id, long long s) {
  queue<long long> q;
  memset(vis, 0, sizeof(vis));
  vis[s] = 1;
  trdis[id][s] = 0;
  q.push(s);
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    vis[x] = 0;
    for (long long i = thead[x]; i; i = te[i].nex) {
      long long y = te[i].to;
      if (trdis[id][y] > trdis[id][x] + te[i].w) {
        trdis[id][y] = trdis[id][x] + te[i].w;
        if (!vis[y]) {
          vis[y] = 1;
          q.push(y);
        }
      }
    }
  }
}
signed main() {
  scanf("%lld%lld", &n, &m);
  long long x, y, z;
  for (long long i = 1; i <= m; i++) {
    scanf("%lld%lld%lld", &t[i].x, &t[i].y, &t[i].z);
    add2(t[i].x, t[i].y, t[i].z);
    add2(t[i].y, t[i].x, t[i].z);
  }
  kr();
  dfs(1, 0);
  for (long long i = 1; i <= 50; i++)
    for (long long j = 1; j <= n; j++) trdis[i][j] = 1e18;
  for (long long i = 1; i <= trcnt; i++) spfa(i, tr[i]);
  scanf("%lld", &q);
  for (long long i = 1; i <= q; i++) {
    scanf("%lld%lld", &x, &y);
    long long ans = dis[x] + dis[y] - 2 * dis[lca(x, y)];
    for (long long i = 1; i <= trcnt; i++)
      ans = min(ans, trdis[i][x] + trdis[i][y]);
    printf("%lld\n", ans);
  }
  return 0;
}

### Prompt

Generate 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;
struct edge {
  long long nex, to, w;
} e[100100 << 1], te[100100 << 1];
struct node {
  long long x, y, z;
} t[100100];
bool vis[100100];
long long dis[100100], fa[100100], d[100100][22], dep[100100], tr[100100],
    trdis[60][100100], trcnt;
long long head[100100], thead[100100], cnt, tcnt;
long long n, m, q;
void add(long long x, long long y, long long z) {
  e[++cnt].nex = head[x];
  e[cnt].to = y;
  e[cnt].w = z;
  head[x] = cnt;
}
void add2(long long x, long long y, long long z) {
  te[++tcnt].nex = thead[x];
  te[tcnt].to = y;
  te[tcnt].w = z;
  thead[x] = tcnt;
}
long long fd(long long x) {
  if (fa[x] == x) return x;
  return fa[x] = fd(fa[x]);
}
bool cmp(node x, node y) { return x.z < y.z; }
void kr() {
  sort(t + 1, t + 1 + m, cmp);
  for (long long i = 1; i <= n; i++) fa[i] = i;
  long long sum = 0;
  for (long long i = 1; i <= m; i++) {
    long long x = t[i].x, y = t[i].y, z = t[i].z;
    long long fx = fd(x), fy = fd(y);
    if (fx == fy) {
      tr[++trcnt] = x;
      tr[++trcnt] = y;
    } else {
      fa[fy] = fx;
      add(x, y, z);
      add(y, x, z);
    }
  }
}
void dfs(long long x, long long faa) {
  dep[x] = dep[faa] + 1;
  d[x][0] = faa;
  for (long long i = 1; (1 << i) <= dep[x]; i++) {
    d[x][i] = d[d[x][i - 1]][i - 1];
  }
  for (long long i = head[x]; i; i = e[i].nex) {
    long long y = e[i].to;
    if (y != faa) {
      dis[y] = dis[x] + e[i].w;
      dfs(y, x);
    }
  }
}
long long lca(long long x, long long y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (long long i = 20; i >= 0; i--) {
    if (dep[d[x][i]] >= dep[y]) {
      x = d[x][i];
    }
  }
  if (x == y) return x;
  for (long long i = 20; i >= 0; i--) {
    if (d[x][i] != d[y][i]) {
      x = d[x][i];
      y = d[y][i];
    }
  }
  return d[x][0];
}
void spfa(long long id, long long s) {
  queue<long long> q;
  memset(vis, 0, sizeof(vis));
  vis[s] = 1;
  trdis[id][s] = 0;
  q.push(s);
  while (!q.empty()) {
    long long x = q.front();
    q.pop();
    vis[x] = 0;
    for (long long i = thead[x]; i; i = te[i].nex) {
      long long y = te[i].to;
      if (trdis[id][y] > trdis[id][x] + te[i].w) {
        trdis[id][y] = trdis[id][x] + te[i].w;
        if (!vis[y]) {
          vis[y] = 1;
          q.push(y);
        }
      }
    }
  }
}
signed main() {
  scanf("%lld%lld", &n, &m);
  long long x, y, z;
  for (long long i = 1; i <= m; i++) {
    scanf("%lld%lld%lld", &t[i].x, &t[i].y, &t[i].z);
    add2(t[i].x, t[i].y, t[i].z);
    add2(t[i].y, t[i].x, t[i].z);
  }
  kr();
  dfs(1, 0);
  for (long long i = 1; i <= 50; i++)
    for (long long j = 1; j <= n; j++) trdis[i][j] = 1e18;
  for (long long i = 1; i <= trcnt; i++) spfa(i, tr[i]);
  scanf("%lld", &q);
  for (long long i = 1; i <= q; i++) {
    scanf("%lld%lld", &x, &y);
    long long ans = dis[x] + dis[y] - 2 * dis[lca(x, y)];
    for (long long i = 1; i <= trcnt; i++)
      ans = min(ans, trdis[i][x] + trdis[i][y]);
    printf("%lld\n", ans);
  }
  return 0;
}

```