#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 2;
const long long INF = 1e18;
struct edge {
  int u;
  long long cost;
};
vector<edge> edges[MAXN];
int timer = 0, tin[MAXN], tout[MAXN], up[MAXN][20], used[MAXN];
long long h[MAXN];
set<int> vert;
void dfs_lca(int v, int p = 1, long long c = 0) {
  used[v] = 1;
  tin[v] = timer++;
  up[v][0] = p;
  h[v] = c;
  for (int i = 1; i < 20; i++) {
    up[v][i] = up[up[v][i - 1]][i - 1];
  }
  for (auto e : edges[v]) {
    if (e.u == p)
      continue;
    else if (used[e.u]) {
      vert.insert(v);
      vert.insert(e.u);
    } else {
      dfs_lca(e.u, v, e.cost + c);
    }
  }
  tout[v] = timer++;
}
bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = 19; i >= 0; i--) {
    if (!upper(up[a][i], b)) a = up[a][i];
  }
  return up[a][0];
}
long long dst(int a, int b) { return h[a] + h[b] - 2 * h[lca(a, b)]; }
long long d[42][MAXN];
void dijkstra_set(int s, int i, int n) {
  fill(d[i] + 1, d[i] + 1 + n, INF);
  d[i][s] = 0;
  set<pair<long long, int> > st;
  st.insert({0LL, s});
  while (!st.empty()) {
    int v = st.begin()->second;
    st.erase(st.begin());
    for (auto e : edges[v]) {
      if (d[i][e.u] > d[i][v] + e.cost) {
        st.erase({d[i][e.u], e.u});
        d[i][e.u] = d[i][v] + e.cost;
        st.insert({d[i][e.u], e.u});
      }
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    long long z;
    cin >> x >> y >> z;
    edges[x].push_back({y, z});
    edges[y].push_back({x, z});
  }
  dfs_lca(1);
  vector<int> vt;
  for (auto x : vert) vt.push_back(x);
  for (int i = 0; i < vt.size(); i++) {
    dijkstra_set(vt[i], i, n);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    long long ans = dst(a, b);
    for (int j = 0; j < vt.size(); j++) {
      ans = min(ans, d[j][a] + d[j][b]);
    }
    cout << ans << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  solve();
}

### 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;
const int MAXN = 1e5 + 2;
const long long INF = 1e18;
struct edge {
  int u;
  long long cost;
};
vector<edge> edges[MAXN];
int timer = 0, tin[MAXN], tout[MAXN], up[MAXN][20], used[MAXN];
long long h[MAXN];
set<int> vert;
void dfs_lca(int v, int p = 1, long long c = 0) {
  used[v] = 1;
  tin[v] = timer++;
  up[v][0] = p;
  h[v] = c;
  for (int i = 1; i < 20; i++) {
    up[v][i] = up[up[v][i - 1]][i - 1];
  }
  for (auto e : edges[v]) {
    if (e.u == p)
      continue;
    else if (used[e.u]) {
      vert.insert(v);
      vert.insert(e.u);
    } else {
      dfs_lca(e.u, v, e.cost + c);
    }
  }
  tout[v] = timer++;
}
bool upper(int a, int b) { return tin[a] <= tin[b] && tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = 19; i >= 0; i--) {
    if (!upper(up[a][i], b)) a = up[a][i];
  }
  return up[a][0];
}
long long dst(int a, int b) { return h[a] + h[b] - 2 * h[lca(a, b)]; }
long long d[42][MAXN];
void dijkstra_set(int s, int i, int n) {
  fill(d[i] + 1, d[i] + 1 + n, INF);
  d[i][s] = 0;
  set<pair<long long, int> > st;
  st.insert({0LL, s});
  while (!st.empty()) {
    int v = st.begin()->second;
    st.erase(st.begin());
    for (auto e : edges[v]) {
      if (d[i][e.u] > d[i][v] + e.cost) {
        st.erase({d[i][e.u], e.u});
        d[i][e.u] = d[i][v] + e.cost;
        st.insert({d[i][e.u], e.u});
      }
    }
  }
}
void solve() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y;
    long long z;
    cin >> x >> y >> z;
    edges[x].push_back({y, z});
    edges[y].push_back({x, z});
  }
  dfs_lca(1);
  vector<int> vt;
  for (auto x : vert) vt.push_back(x);
  for (int i = 0; i < vt.size(); i++) {
    dijkstra_set(vt[i], i, n);
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    long long ans = dst(a, b);
    for (int j = 0; j < vt.size(); j++) {
      ans = min(ans, d[j][a] + d[j][b]);
    }
    cout << ans << endl;
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  solve();
}

```