#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mxN = 100001;
const int logn = 21;
int n, m, q;
vector<pair<int, ll>> adj[mxN];
vector<pair<pair<int, int>, ll>> bad;
vector<ll> dijk[400];
int par[mxN][logn];
ll dist[mxN][logn];
int depth[mxN];
bool flag[mxN];
void dijkstra(int s, vector<ll> &d) {
  d.resize(n, INF);
  d[s] = 0;
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;
  q.push({0, s});
  while (!q.empty()) {
    int v = q.top().second;
    ll dv = q.top().first;
    q.pop();
    if (dv != d[v]) continue;
    for (auto p : adj[v]) {
      int to = p.first;
      ll w = p.second;
      if (d[v] + w < d[to]) {
        d[to] = d[v] + w;
        q.push({d[to], to});
      }
    }
  }
}
void dfs(int u, int p) {
  par[u][0] = p;
  flag[u] = true;
  for (auto v : adj[u]) {
    if (v.first == p) continue;
    if (flag[v.first]) {
      bad.push_back({{u, v.first}, v.second});
      continue;
    }
    depth[v.first] = depth[u] + 1;
    dist[v.first][0] = v.second;
    dfs(v.first, u);
  }
}
pair<int, ll> walk(int i, int j) {
  ll res = 0;
  for (int d = 0; d < logn; d++) {
    if ((1 << d) & j) {
      res += dist[i][d];
      i = par[i][d];
    }
  }
  return {i, res};
}
ll lca(int a, int b) {
  ll res = 0;
  if (depth[a] > depth[b]) {
    auto p = walk(a, depth[a] - depth[b]);
    a = p.first;
    res += p.second;
  } else if (depth[b] > depth[a]) {
    auto p = walk(b, depth[b] - depth[a]);
    b = p.first;
    res += p.second;
  }
  if (a == b) return res;
  for (int d = logn - 1; d >= 0; d--) {
    if (par[a][d] != par[b][d]) {
      res += dist[a][d];
      res += dist[b][d];
      a = par[a][d];
      b = par[b][d];
    }
  }
  return res + dist[a][0] + dist[b][0];
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  int a, b;
  ll c;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    a--;
    b--;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  depth[0] = 0;
  dist[0][0] = 0;
  dfs(0, 0);
  for (int i = 1; i < logn; ++i) {
    for (int j = 0; j < n; ++j) {
      par[j][i] = par[par[j][i - 1]][i - 1];
      dist[j][i] = dist[j][i - 1] + dist[par[j][i - 1]][i - 1];
    }
  }
  int idx = 0;
  for (auto p : bad) {
    dijkstra(p.first.first, dijk[idx]);
    dijkstra(p.first.second, dijk[idx + 1]);
    idx += 2;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> b;
    a--;
    b--;
    ll ans = lca(a, b);
    idx = 0;
    for (auto p : bad) {
      ans = min(dijk[idx][a] + dijk[idx + 1][b] + p.second, ans);
      ans = min(dijk[idx][b] + dijk[idx + 1][a] + p.second, ans);
      idx += 2;
    }
    cout << ans << "\n";
  }
  return 0;
}

### Prompt

Please create a solution in cpp 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;
using ll = long long;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int mxN = 100001;
const int logn = 21;
int n, m, q;
vector<pair<int, ll>> adj[mxN];
vector<pair<pair<int, int>, ll>> bad;
vector<ll> dijk[400];
int par[mxN][logn];
ll dist[mxN][logn];
int depth[mxN];
bool flag[mxN];
void dijkstra(int s, vector<ll> &d) {
  d.resize(n, INF);
  d[s] = 0;
  priority_queue<pair<ll, int>, vector<pair<ll, int>>, greater<pair<ll, int>>>
      q;
  q.push({0, s});
  while (!q.empty()) {
    int v = q.top().second;
    ll dv = q.top().first;
    q.pop();
    if (dv != d[v]) continue;
    for (auto p : adj[v]) {
      int to = p.first;
      ll w = p.second;
      if (d[v] + w < d[to]) {
        d[to] = d[v] + w;
        q.push({d[to], to});
      }
    }
  }
}
void dfs(int u, int p) {
  par[u][0] = p;
  flag[u] = true;
  for (auto v : adj[u]) {
    if (v.first == p) continue;
    if (flag[v.first]) {
      bad.push_back({{u, v.first}, v.second});
      continue;
    }
    depth[v.first] = depth[u] + 1;
    dist[v.first][0] = v.second;
    dfs(v.first, u);
  }
}
pair<int, ll> walk(int i, int j) {
  ll res = 0;
  for (int d = 0; d < logn; d++) {
    if ((1 << d) & j) {
      res += dist[i][d];
      i = par[i][d];
    }
  }
  return {i, res};
}
ll lca(int a, int b) {
  ll res = 0;
  if (depth[a] > depth[b]) {
    auto p = walk(a, depth[a] - depth[b]);
    a = p.first;
    res += p.second;
  } else if (depth[b] > depth[a]) {
    auto p = walk(b, depth[b] - depth[a]);
    b = p.first;
    res += p.second;
  }
  if (a == b) return res;
  for (int d = logn - 1; d >= 0; d--) {
    if (par[a][d] != par[b][d]) {
      res += dist[a][d];
      res += dist[b][d];
      a = par[a][d];
      b = par[b][d];
    }
  }
  return res + dist[a][0] + dist[b][0];
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  int a, b;
  ll c;
  for (int i = 0; i < m; ++i) {
    cin >> a >> b >> c;
    a--;
    b--;
    adj[a].push_back({b, c});
    adj[b].push_back({a, c});
  }
  depth[0] = 0;
  dist[0][0] = 0;
  dfs(0, 0);
  for (int i = 1; i < logn; ++i) {
    for (int j = 0; j < n; ++j) {
      par[j][i] = par[par[j][i - 1]][i - 1];
      dist[j][i] = dist[j][i - 1] + dist[par[j][i - 1]][i - 1];
    }
  }
  int idx = 0;
  for (auto p : bad) {
    dijkstra(p.first.first, dijk[idx]);
    dijkstra(p.first.second, dijk[idx + 1]);
    idx += 2;
  }
  cin >> q;
  for (int i = 0; i < q; ++i) {
    cin >> a >> b;
    a--;
    b--;
    ll ans = lca(a, b);
    idx = 0;
    for (auto p : bad) {
      ans = min(dijk[idx][a] + dijk[idx + 1][b] + p.second, ans);
      ans = min(dijk[idx][b] + dijk[idx + 1][a] + p.second, ans);
      idx += 2;
    }
    cout << ans << "\n";
  }
  return 0;
}

```