#include <bits/stdc++.h>
const int maxn = 1e5 + 30;
const long long inf = (1ll << 60);
int n, m, q, head[maxn], cnt, f[maxn][18], dep[maxn], tp;
long long dis[45][maxn];
bool vis[maxn], vi[maxn];
struct edge {
  int t, w, nxt;
} e[maxn << 1];
struct node {
  long long d;
  int x;
  bool operator<(const node o) const { return d > o.d; }
};
std::priority_queue<node> Q;
void add(int a, int b, int c) {
  e[++cnt] = (edge){b, c, head[a]};
  head[a] = cnt;
}
int in() {
  int re = 0;
  char ch = getchar();
  bool f = 0;
  while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    re = (re << 1) + (re << 3) + (ch ^ '0'), ch = getchar();
  return f ? -re : re;
}
void dij(int s, int o) {
  int now;
  Q.push((node){0, s});
  for (int i = 1; i <= n; ++i) dis[o][i] = inf;
  dis[o][s] = 0;
  memset(vi, 0, sizeof(vi));
  while (!Q.empty()) {
    now = Q.top().x;
    Q.pop();
    if (vi[now]) continue;
    vi[now] = 1;
    for (int i = head[now]; i; i = e[i].nxt)
      if (dis[o][now] + (long long)e[i].w < dis[o][e[i].t]) {
        dis[o][e[i].t] = dis[o][now] + (long long)e[i].w;
        Q.push((node){dis[o][e[i].t], e[i].t});
      }
  }
}
void dfs(int x) {
  vis[x] = 1;
  for (int i = 1; i < 18; ++i) {
    f[x][i] = f[f[x][i - 1]][i - 1];
    if (f[x][i] == 0) break;
  }
  for (int i = head[x]; i; i = e[i].nxt) {
    if (e[i].t == f[x][0]) continue;
    if (vis[e[i].t])
      dij(x, ++tp);
    else {
      f[e[i].t][0] = x;
      dep[e[i].t] = dep[x] + 1;
      dis[0][e[i].t] = dis[0][x] + (long long)e[i].w;
      dfs(e[i].t);
    }
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) std::swap(x, y);
  for (int i = 17; i >= 0; --i)
    if (dep[f[x][i]] >= dep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 17; i >= 0; --i)
    if (f[x][i] != f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  return f[x][0];
}
int main() {
  long long ans;
  int a, b, c;
  n = in();
  m = in();
  for (int i = 1; i <= m; ++i) {
    a = in();
    b = in();
    c = in();
    add(a, b, c);
    add(b, a, c);
  }
  dfs(1);
  q = in();
  for (int i = 1; i <= q; ++i) {
    a = in();
    b = in();
    ans = dis[0][a] + dis[0][b] - (dis[0][lca(a, b)] << 1);
    for (int j = 1; j <= tp; ++j) ans = std::min(ans, dis[j][a] + dis[j][b]);
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Develop a solution in Cpp to the problem described below:

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>
const int maxn = 1e5 + 30;
const long long inf = (1ll << 60);
int n, m, q, head[maxn], cnt, f[maxn][18], dep[maxn], tp;
long long dis[45][maxn];
bool vis[maxn], vi[maxn];
struct edge {
  int t, w, nxt;
} e[maxn << 1];
struct node {
  long long d;
  int x;
  bool operator<(const node o) const { return d > o.d; }
};
std::priority_queue<node> Q;
void add(int a, int b, int c) {
  e[++cnt] = (edge){b, c, head[a]};
  head[a] = cnt;
}
int in() {
  int re = 0;
  char ch = getchar();
  bool f = 0;
  while (ch < '0' || ch > '9') f |= (ch == '-'), ch = getchar();
  while (ch >= '0' && ch <= '9')
    re = (re << 1) + (re << 3) + (ch ^ '0'), ch = getchar();
  return f ? -re : re;
}
void dij(int s, int o) {
  int now;
  Q.push((node){0, s});
  for (int i = 1; i <= n; ++i) dis[o][i] = inf;
  dis[o][s] = 0;
  memset(vi, 0, sizeof(vi));
  while (!Q.empty()) {
    now = Q.top().x;
    Q.pop();
    if (vi[now]) continue;
    vi[now] = 1;
    for (int i = head[now]; i; i = e[i].nxt)
      if (dis[o][now] + (long long)e[i].w < dis[o][e[i].t]) {
        dis[o][e[i].t] = dis[o][now] + (long long)e[i].w;
        Q.push((node){dis[o][e[i].t], e[i].t});
      }
  }
}
void dfs(int x) {
  vis[x] = 1;
  for (int i = 1; i < 18; ++i) {
    f[x][i] = f[f[x][i - 1]][i - 1];
    if (f[x][i] == 0) break;
  }
  for (int i = head[x]; i; i = e[i].nxt) {
    if (e[i].t == f[x][0]) continue;
    if (vis[e[i].t])
      dij(x, ++tp);
    else {
      f[e[i].t][0] = x;
      dep[e[i].t] = dep[x] + 1;
      dis[0][e[i].t] = dis[0][x] + (long long)e[i].w;
      dfs(e[i].t);
    }
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) std::swap(x, y);
  for (int i = 17; i >= 0; --i)
    if (dep[f[x][i]] >= dep[y]) x = f[x][i];
  if (x == y) return x;
  for (int i = 17; i >= 0; --i)
    if (f[x][i] != f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  return f[x][0];
}
int main() {
  long long ans;
  int a, b, c;
  n = in();
  m = in();
  for (int i = 1; i <= m; ++i) {
    a = in();
    b = in();
    c = in();
    add(a, b, c);
    add(b, a, c);
  }
  dfs(1);
  q = in();
  for (int i = 1; i <= q; ++i) {
    a = in();
    b = in();
    ans = dis[0][a] + dis[0][b] - (dis[0][lca(a, b)] << 1);
    for (int j = 1; j <= tp; ++j) ans = std::min(ans, dis[j][a] + dis[j][b]);
    printf("%I64d\n", ans);
  }
  return 0;
}

```