#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
const int ext = 50;
int n, m, q;
int fa[20][maxn], depth[maxn];
long long dist0[maxn];
bool visited[maxn];
int queU[ext], queV[ext], dd[ext], c;
long long dist[ext][maxn];
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    pq;
struct graph_t {
  int head[maxn], adj[maxn * 2 + ext], nxt[maxn * 2 + ext],
      weight[maxn * 2 + ext], total;
  inline void addedge(int u, int v, int w) {
    ++total, adj[total] = v, nxt[total] = head[u], weight[total] = w,
             head[u] = total;
  }
} G1, G2;
struct ufs_t {
  int fa[maxn];
  int getf(int u) { return (fa[u] == u) ? u : (fa[u] = getf(fa[u])); }
  void init(int _n) {
    for (int i = 1; i <= _n; i++) fa[i] = i;
  }
  void merge(int u, int v, int w) {
    int r1 = getf(u), r2 = getf(v);
    if (r1 == r2) {
      ++c, dd[c] = w, queU[c] = u, queV[c] = v;
      return;
    }
    fa[r1] = r2;
    G2.addedge(u, v, w);
    G2.addedge(v, u, w);
  }
} S;
void dfs(int u, int f) {
  for (int i = 1; i <= 19; i++) fa[i][u] = fa[i - 1][fa[i - 1][u]];
  for (int i = G2.head[u]; i; i = G2.nxt[i])
    if (G2.adj[i] != f) {
      dist0[G2.adj[i]] = dist0[u] + G2.weight[i];
      depth[G2.adj[i]] = depth[u] + 1;
      fa[0][G2.adj[i]] = u;
      dfs(G2.adj[i], u);
    }
}
int get_lca(int u, int v) {
  if (depth[u] != depth[v]) {
    if (depth[u] < depth[v]) swap(u, v);
    for (int i = 0; i <= 19; i++)
      if (((depth[u] - depth[v]) >> i) & 1) u = fa[i][u];
  }
  if (u == v) return u;
  for (int i = 19; i >= 0; i--)
    if (fa[i][u] != fa[i][v]) u = fa[i][u], v = fa[i][v];
  return fa[0][u];
}
long long query(int u, int v) {
  int lca = get_lca(u, v);
  return dist0[u] + dist0[v] - 2 * dist0[lca];
}
void dijkstra(int x, int id) {
  memset(dist[id], 0x3F, sizeof(dist[id]));
  memset(visited, 0, sizeof(visited));
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >()
      .swap(pq);
  dist[id][x] = 0, pq.push(make_pair(0, x));
  while (!pq.empty()) {
    long long d = pq.top().first;
    int p = pq.top().second;
    visited[p] = true, pq.pop();
    for (int i = G1.head[p]; i; i = G1.nxt[i])
      if (!visited[G1.adj[i]] &&
          dist[id][G1.adj[i]] > dist[id][p] + G1.weight[i]) {
        dist[id][G1.adj[i]] = dist[id][p] + G1.weight[i];
        pq.push(make_pair(dist[id][G1.adj[i]], G1.adj[i]));
      }
  }
}
int main() {
  scanf("%d%d", &n, &m), S.init(n);
  for (int u, v, w, i = 1; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    G1.addedge(u, v, w), G1.addedge(v, u, w);
    S.merge(u, v, w);
  }
  dfs(1, 0);
  for (int i = 1; i <= c; i++) {
    cerr << queU[i] << " " << queV[i] << endl;
    dijkstra(queU[i], i * 2 - 1);
    dijkstra(queV[i], i * 2);
  }
  scanf("%d", &q);
  for (int u, v, i = 1; i <= q; i++) {
    scanf("%d%d", &u, &v);
    long long ans = query(u, v);
    for (int j = 1; j <= c; j++)
      ans = min(ans, min(dist[2 * j - 1][u] + dist[2 * j][v],
                         dist[2 * j - 1][v] + dist[2 * j][u]) +
                         dd[j]);
    printf("%lld\n", ans);
  }
  return 0;
}

### Prompt

In cpp, your task is to solve 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;
const int maxn = 100005;
const int ext = 50;
int n, m, q;
int fa[20][maxn], depth[maxn];
long long dist0[maxn];
bool visited[maxn];
int queU[ext], queV[ext], dd[ext], c;
long long dist[ext][maxn];
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    pq;
struct graph_t {
  int head[maxn], adj[maxn * 2 + ext], nxt[maxn * 2 + ext],
      weight[maxn * 2 + ext], total;
  inline void addedge(int u, int v, int w) {
    ++total, adj[total] = v, nxt[total] = head[u], weight[total] = w,
             head[u] = total;
  }
} G1, G2;
struct ufs_t {
  int fa[maxn];
  int getf(int u) { return (fa[u] == u) ? u : (fa[u] = getf(fa[u])); }
  void init(int _n) {
    for (int i = 1; i <= _n; i++) fa[i] = i;
  }
  void merge(int u, int v, int w) {
    int r1 = getf(u), r2 = getf(v);
    if (r1 == r2) {
      ++c, dd[c] = w, queU[c] = u, queV[c] = v;
      return;
    }
    fa[r1] = r2;
    G2.addedge(u, v, w);
    G2.addedge(v, u, w);
  }
} S;
void dfs(int u, int f) {
  for (int i = 1; i <= 19; i++) fa[i][u] = fa[i - 1][fa[i - 1][u]];
  for (int i = G2.head[u]; i; i = G2.nxt[i])
    if (G2.adj[i] != f) {
      dist0[G2.adj[i]] = dist0[u] + G2.weight[i];
      depth[G2.adj[i]] = depth[u] + 1;
      fa[0][G2.adj[i]] = u;
      dfs(G2.adj[i], u);
    }
}
int get_lca(int u, int v) {
  if (depth[u] != depth[v]) {
    if (depth[u] < depth[v]) swap(u, v);
    for (int i = 0; i <= 19; i++)
      if (((depth[u] - depth[v]) >> i) & 1) u = fa[i][u];
  }
  if (u == v) return u;
  for (int i = 19; i >= 0; i--)
    if (fa[i][u] != fa[i][v]) u = fa[i][u], v = fa[i][v];
  return fa[0][u];
}
long long query(int u, int v) {
  int lca = get_lca(u, v);
  return dist0[u] + dist0[v] - 2 * dist0[lca];
}
void dijkstra(int x, int id) {
  memset(dist[id], 0x3F, sizeof(dist[id]));
  memset(visited, 0, sizeof(visited));
  priority_queue<pair<long long, int>, vector<pair<long long, int> >,
                 greater<pair<long long, int> > >()
      .swap(pq);
  dist[id][x] = 0, pq.push(make_pair(0, x));
  while (!pq.empty()) {
    long long d = pq.top().first;
    int p = pq.top().second;
    visited[p] = true, pq.pop();
    for (int i = G1.head[p]; i; i = G1.nxt[i])
      if (!visited[G1.adj[i]] &&
          dist[id][G1.adj[i]] > dist[id][p] + G1.weight[i]) {
        dist[id][G1.adj[i]] = dist[id][p] + G1.weight[i];
        pq.push(make_pair(dist[id][G1.adj[i]], G1.adj[i]));
      }
  }
}
int main() {
  scanf("%d%d", &n, &m), S.init(n);
  for (int u, v, w, i = 1; i <= m; i++) {
    scanf("%d%d%d", &u, &v, &w);
    G1.addedge(u, v, w), G1.addedge(v, u, w);
    S.merge(u, v, w);
  }
  dfs(1, 0);
  for (int i = 1; i <= c; i++) {
    cerr << queU[i] << " " << queV[i] << endl;
    dijkstra(queU[i], i * 2 - 1);
    dijkstra(queV[i], i * 2);
  }
  scanf("%d", &q);
  for (int u, v, i = 1; i <= q; i++) {
    scanf("%d%d", &u, &v);
    long long ans = query(u, v);
    for (int j = 1; j <= c; j++)
      ans = min(ans, min(dist[2 * j - 1][u] + dist[2 * j][v],
                         dist[2 * j - 1][v] + dist[2 * j][u]) +
                         dd[j]);
    printf("%lld\n", ans);
  }
  return 0;
}

```