#include <bits/stdc++.h>
using namespace std;
long long read() {
  char ch = getchar();
  long long x = 0;
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  return x;
}
struct point {
  long long to, next, dis;
} e[200010 << 1];
long long n, m, num, Q, top;
long long q[200010], head[200010], deep[200010], dist[200010];
long long dis[50][200010], fa[200010][25];
bool vis[200010];
struct node {
  long long id, dis;
};
bool operator<(node a1, node a2) { return a1.dis > a2.dis; }
void add(long long from, long long to, long long dis) {
  e[++num].next = head[from];
  e[num].to = to;
  e[num].dis = dis;
  head[from] = num;
}
void dfs(long long x, long long f) {
  vis[x] = true;
  fa[x][0] = f;
  for (long long i = head[x]; i; i = e[i].next) {
    long long to = e[i].to;
    if (to == f) continue;
    if (vis[to])
      q[++top] = x, q[++top] = to;
    else {
      deep[to] = deep[x] + 1;
      dist[to] = dist[x] + e[i].dis;
      dfs(to, x);
    }
  }
}
long long lca(long long x, long long y) {
  if (deep[x] < deep[y]) swap(x, y);
  for (long long i = 19; i >= 0; i--)
    if (deep[fa[x][i]] >= deep[y]) x = fa[x][i];
  if (x == y) return x;
  for (long long i = 19; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
void Dijkstra(long long id) {
  memset(dis[id], 63, sizeof(dis[id]));
  memset(vis, false, sizeof(vis));
  priority_queue<node> Q;
  dis[id][q[id]] = 0;
  Q.push((node){q[id], 0});
  while (!Q.empty()) {
    long long x = Q.top().id;
    Q.pop();
    if (vis[x]) continue;
    vis[x] = true;
    for (long long i = head[x]; i; i = e[i].next) {
      long long to = e[i].to;
      if (!vis[to] && dis[id][x] + e[i].dis < dis[id][to]) {
        dis[id][to] = dis[id][x] + e[i].dis;
        Q.push((node){to, dis[id][to]});
      }
    }
  }
}
int main() {
  n = read();
  m = read();
  for (long long i = 1; i <= m; i++) {
    long long a = read(), b = read(), c = read();
    add(a, b, c);
    add(b, a, c);
  }
  deep[1] = 1;
  dfs(1, 0);
  for (long long j = 1; j <= 19; j++)
    for (long long i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  sort(q + 1, q + 1 + top);
  top = unique(q + 1, q + 1 + top) - q - 1;
  for (long long i = 1; i <= top; i++) Dijkstra(i);
  Q = read();
  while (Q--) {
    long long x = read(), y = read();
    long long ans = dist[x] + dist[y] - 2 * dist[lca(x, y)];
    for (long long i = 1; i <= top; i++) ans = min(ans, dis[i][x] + dis[i][y]);
    printf("%lld\n", ans);
  }
  return 0;
}

### Prompt

Please formulate 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;
long long read() {
  char ch = getchar();
  long long x = 0;
  while (ch > '9' || ch < '0') ch = getchar();
  while (ch <= '9' && ch >= '0') x = x * 10 + ch - '0', ch = getchar();
  return x;
}
struct point {
  long long to, next, dis;
} e[200010 << 1];
long long n, m, num, Q, top;
long long q[200010], head[200010], deep[200010], dist[200010];
long long dis[50][200010], fa[200010][25];
bool vis[200010];
struct node {
  long long id, dis;
};
bool operator<(node a1, node a2) { return a1.dis > a2.dis; }
void add(long long from, long long to, long long dis) {
  e[++num].next = head[from];
  e[num].to = to;
  e[num].dis = dis;
  head[from] = num;
}
void dfs(long long x, long long f) {
  vis[x] = true;
  fa[x][0] = f;
  for (long long i = head[x]; i; i = e[i].next) {
    long long to = e[i].to;
    if (to == f) continue;
    if (vis[to])
      q[++top] = x, q[++top] = to;
    else {
      deep[to] = deep[x] + 1;
      dist[to] = dist[x] + e[i].dis;
      dfs(to, x);
    }
  }
}
long long lca(long long x, long long y) {
  if (deep[x] < deep[y]) swap(x, y);
  for (long long i = 19; i >= 0; i--)
    if (deep[fa[x][i]] >= deep[y]) x = fa[x][i];
  if (x == y) return x;
  for (long long i = 19; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) x = fa[x][i], y = fa[y][i];
  return fa[x][0];
}
void Dijkstra(long long id) {
  memset(dis[id], 63, sizeof(dis[id]));
  memset(vis, false, sizeof(vis));
  priority_queue<node> Q;
  dis[id][q[id]] = 0;
  Q.push((node){q[id], 0});
  while (!Q.empty()) {
    long long x = Q.top().id;
    Q.pop();
    if (vis[x]) continue;
    vis[x] = true;
    for (long long i = head[x]; i; i = e[i].next) {
      long long to = e[i].to;
      if (!vis[to] && dis[id][x] + e[i].dis < dis[id][to]) {
        dis[id][to] = dis[id][x] + e[i].dis;
        Q.push((node){to, dis[id][to]});
      }
    }
  }
}
int main() {
  n = read();
  m = read();
  for (long long i = 1; i <= m; i++) {
    long long a = read(), b = read(), c = read();
    add(a, b, c);
    add(b, a, c);
  }
  deep[1] = 1;
  dfs(1, 0);
  for (long long j = 1; j <= 19; j++)
    for (long long i = 1; i <= n; i++) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  sort(q + 1, q + 1 + top);
  top = unique(q + 1, q + 1 + top) - q - 1;
  for (long long i = 1; i <= top; i++) Dijkstra(i);
  Q = read();
  while (Q--) {
    long long x = read(), y = read();
    long long ans = dist[x] + dist[y] - 2 * dist[lca(x, y)];
    for (long long i = 1; i <= top; i++) ans = min(ans, dis[i][x] + dis[i][y]);
    printf("%lld\n", ans);
  }
  return 0;
}

```