#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
using ll = long long;
struct Edge {
  int v, w, nxt;
} edge[maxn * 2];
int head[maxn], tot;
void init() {
  memset(head, -1, sizeof head);
  tot = 0;
}
void addedge(int u, int v, int w) {
  edge[tot] = {v, w, head[u]};
  head[u] = tot++;
}
int n, m, vis[maxn], dep[maxn], fa[maxn][20];
ll dis[maxn];
vector<int> a;
void dfs(int u, int pre) {
  vis[u] = 1;
  dep[u] = dep[pre] + 1;
  fa[u][0] = pre;
  for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = head[u]; ~i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (v == pre) continue;
    if (vis[v]) {
      a.push_back(u);
      continue;
    }
    dis[v] = dis[u] + edge[i].w;
    dfs(v, u);
  }
}
int LCA(int u, int v) {
  if (dep[u] > dep[v]) swap(u, v);
  for (int i = 19; i >= 0; i--)
    if (dep[fa[v][i]] >= dep[u]) v = fa[v][i];
  if (u == v) return u;
  for (int i = 19; i >= 0; i--) {
    if (fa[u][i] == fa[v][i]) continue;
    u = fa[u][i];
    v = fa[v][i];
  }
  return fa[u][0];
}
ll dij[42][maxn];
using pli = pair<ll, int>;
void dijkstra(int s, ll d[]) {
  priority_queue<pli, vector<pli>, greater<pli> > q;
  d[s] = 0;
  q.push({d[s], s});
  while (!q.empty()) {
    int u = q.top().second;
    ll t = q.top().first;
    q.pop();
    if (t != d[u]) continue;
    for (int i = head[u]; ~i; i = edge[i].nxt) {
      int v = edge[i].v, w = edge[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        q.push({d[v], v});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    addedge(u, v, w);
    addedge(v, u, w);
  }
  dfs(1, 0);
  memset(dij, 0x3f, sizeof dij);
  for (int i = 0; i < a.size(); i++) dijkstra(a[i], dij[i]);
  int Q;
  cin >> Q;
  while (Q--) {
    int x, y;
    cin >> x >> y;
    ll ans = dis[x] + dis[y] - 2 * dis[LCA(x, y)];
    for (int i = 0; i < a.size(); i++) ans = min(ans, dij[i][x] + dij[i][y]);
    cout << ans << "\n";
  }
  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>
using namespace std;
const int maxn = 1e5 + 5;
using ll = long long;
struct Edge {
  int v, w, nxt;
} edge[maxn * 2];
int head[maxn], tot;
void init() {
  memset(head, -1, sizeof head);
  tot = 0;
}
void addedge(int u, int v, int w) {
  edge[tot] = {v, w, head[u]};
  head[u] = tot++;
}
int n, m, vis[maxn], dep[maxn], fa[maxn][20];
ll dis[maxn];
vector<int> a;
void dfs(int u, int pre) {
  vis[u] = 1;
  dep[u] = dep[pre] + 1;
  fa[u][0] = pre;
  for (int i = 1; i < 20; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = head[u]; ~i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (v == pre) continue;
    if (vis[v]) {
      a.push_back(u);
      continue;
    }
    dis[v] = dis[u] + edge[i].w;
    dfs(v, u);
  }
}
int LCA(int u, int v) {
  if (dep[u] > dep[v]) swap(u, v);
  for (int i = 19; i >= 0; i--)
    if (dep[fa[v][i]] >= dep[u]) v = fa[v][i];
  if (u == v) return u;
  for (int i = 19; i >= 0; i--) {
    if (fa[u][i] == fa[v][i]) continue;
    u = fa[u][i];
    v = fa[v][i];
  }
  return fa[u][0];
}
ll dij[42][maxn];
using pli = pair<ll, int>;
void dijkstra(int s, ll d[]) {
  priority_queue<pli, vector<pli>, greater<pli> > q;
  d[s] = 0;
  q.push({d[s], s});
  while (!q.empty()) {
    int u = q.top().second;
    ll t = q.top().first;
    q.pop();
    if (t != d[u]) continue;
    for (int i = head[u]; ~i; i = edge[i].nxt) {
      int v = edge[i].v, w = edge[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        q.push({d[v], v});
      }
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  init();
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    addedge(u, v, w);
    addedge(v, u, w);
  }
  dfs(1, 0);
  memset(dij, 0x3f, sizeof dij);
  for (int i = 0; i < a.size(); i++) dijkstra(a[i], dij[i]);
  int Q;
  cin >> Q;
  while (Q--) {
    int x, y;
    cin >> x >> y;
    ll ans = dis[x] + dis[y] - 2 * dis[LCA(x, y)];
    for (int i = 0; i < a.size(); i++) ans = min(ans, dij[i][x] + dij[i][y]);
    cout << ans << "\n";
  }
  return 0;
}

```