#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 1e5 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int i, n, j, m, q, x, y, insert[nmax], out[nmax], par[nmax][20], tt, p[nmax],
    sz[nmax], rt;
long long rs, l[nmax], d[30][nmax], z;
set<pair<long long, int> > s;
vector<pair<int, long long> > a[nmax], g[nmax];
vector<int> vec;
int fnd(int x) {
  if (p[x] == x) return x;
  return p[x] = fnd(p[x]);
}
void uni(int x, int y, long long z) {
  int px, py;
  px = fnd(x), py = fnd(y);
  if (px == py) {
    vec.push_back(x);
    return;
  }
  if (sz[px] < sz[py]) swap(px, py);
  sz[px] += sz[py];
  p[py] = px;
  a[x].push_back(make_pair(y, z));
  a[y].push_back(make_pair(x, z));
  rt = x;
}
void dfs(int x, int p, long long c) {
  insert[x] = ++tt;
  l[x] = c;
  par[x][0] = p;
  for (int i = 1; i <= 16; i++) par[x][i] = par[par[x][i - 1]][i - 1];
  for (int i = 0; i < (int)a[x].size(); i++) {
    int y = a[x][i].first;
    if (y == p) continue;
    dfs(y, x, c + a[x][i].second);
  }
  out[x] = ++tt;
}
int anc(int x, int y) { return insert[x] <= insert[y] && out[x] >= out[y]; }
int lca(int x, int y) {
  if (anc(x, y)) return x;
  if (anc(y, x)) return y;
  for (int i = 16; i >= 0; i--)
    if (!anc(par[x][i], y)) x = par[x][i];
  return par[x][0];
}
long long dist(int x, int y) { return l[x] + l[y] - 2LL * l[lca(x, y)]; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    p[i] = i;
    sz[i] = 1;
  }
  while (m--) {
    cin >> x >> y >> z;
    g[x].push_back(make_pair(y, z));
    g[y].push_back(make_pair(x, z));
    uni(x, y, z);
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  for (i = 0; i < (int)vec.size(); i++) {
    for (j = 1; j <= n; j++) d[i][j] = llinf;
    d[i][vec[i]] = 0;
    s.insert(make_pair(0, vec[i]));
    while (!s.empty()) {
      x = s.begin()->second, z = s.begin()->first;
      s.erase(s.begin());
      for (j = 0; j < (int)g[x].size(); j++) {
        y = g[x][j].first;
        if (z + g[x][j].second >= d[i][y]) continue;
        if (s.find(make_pair(d[i][y], y)) != s.end())
          s.erase(s.find(make_pair(d[i][y], y)));
        d[i][y] = z + g[x][j].second;
        s.insert(make_pair(d[i][y], y));
      }
    }
  }
  dfs(rt, rt, 0);
  cin >> q;
  while (q--) {
    cin >> x >> y;
    rs = dist(x, y);
    for (i = 0; i < (int)vec.size(); i++) rs = min(rs, d[i][x] + d[i][y]);
    cout << rs << '\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>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = (1LL << 62);
const int inf = (1 << 30);
const int nmax = 1e5 + 50;
const long long mod = 1e9 + 7;
using namespace std;
int i, n, j, m, q, x, y, insert[nmax], out[nmax], par[nmax][20], tt, p[nmax],
    sz[nmax], rt;
long long rs, l[nmax], d[30][nmax], z;
set<pair<long long, int> > s;
vector<pair<int, long long> > a[nmax], g[nmax];
vector<int> vec;
int fnd(int x) {
  if (p[x] == x) return x;
  return p[x] = fnd(p[x]);
}
void uni(int x, int y, long long z) {
  int px, py;
  px = fnd(x), py = fnd(y);
  if (px == py) {
    vec.push_back(x);
    return;
  }
  if (sz[px] < sz[py]) swap(px, py);
  sz[px] += sz[py];
  p[py] = px;
  a[x].push_back(make_pair(y, z));
  a[y].push_back(make_pair(x, z));
  rt = x;
}
void dfs(int x, int p, long long c) {
  insert[x] = ++tt;
  l[x] = c;
  par[x][0] = p;
  for (int i = 1; i <= 16; i++) par[x][i] = par[par[x][i - 1]][i - 1];
  for (int i = 0; i < (int)a[x].size(); i++) {
    int y = a[x][i].first;
    if (y == p) continue;
    dfs(y, x, c + a[x][i].second);
  }
  out[x] = ++tt;
}
int anc(int x, int y) { return insert[x] <= insert[y] && out[x] >= out[y]; }
int lca(int x, int y) {
  if (anc(x, y)) return x;
  if (anc(y, x)) return y;
  for (int i = 16; i >= 0; i--)
    if (!anc(par[x][i], y)) x = par[x][i];
  return par[x][0];
}
long long dist(int x, int y) { return l[x] + l[y] - 2LL * l[lca(x, y)]; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> n >> m;
  for (i = 1; i <= n; i++) {
    p[i] = i;
    sz[i] = 1;
  }
  while (m--) {
    cin >> x >> y >> z;
    g[x].push_back(make_pair(y, z));
    g[y].push_back(make_pair(x, z));
    uni(x, y, z);
  }
  sort(vec.begin(), vec.end());
  vec.resize(unique(vec.begin(), vec.end()) - vec.begin());
  for (i = 0; i < (int)vec.size(); i++) {
    for (j = 1; j <= n; j++) d[i][j] = llinf;
    d[i][vec[i]] = 0;
    s.insert(make_pair(0, vec[i]));
    while (!s.empty()) {
      x = s.begin()->second, z = s.begin()->first;
      s.erase(s.begin());
      for (j = 0; j < (int)g[x].size(); j++) {
        y = g[x][j].first;
        if (z + g[x][j].second >= d[i][y]) continue;
        if (s.find(make_pair(d[i][y], y)) != s.end())
          s.erase(s.find(make_pair(d[i][y], y)));
        d[i][y] = z + g[x][j].second;
        s.insert(make_pair(d[i][y], y));
      }
    }
  }
  dfs(rt, rt, 0);
  cin >> q;
  while (q--) {
    cin >> x >> y;
    rs = dist(x, y);
    for (i = 0; i < (int)vec.size(); i++) rs = min(rs, d[i][x] + d[i][y]);
    cout << rs << '\n';
  }
  return 0;
}

```