#include <bits/stdc++.h>
using namespace std;
int n, m, t[100005], siz[100005], nrq, vizk[100005], nrspec, viz[100005],
    niv[100005], a[100005][21];
long long dist[50][100005], d[100005];
vector<pair<int, long long>> v[100005], vk[100005];
struct muchii {
  int x, y;
  long long z;
} mm[100005], mk[100005];
void citire() {
  int i, x, y, z;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
    mm[i].x = x;
    mm[i].y = y;
    mm[i].z = z;
  }
}
int Find(int x) {
  while (t[x] != 0) x = t[x];
  return x;
}
int Union(int x, int y) {
  int rx, ry;
  rx = Find(x);
  ry = Find(y);
  if (rx != ry) {
    if (siz[rx] > siz[ry]) {
      siz[rx] += siz[ry];
      t[ry] = rx;
    } else {
      siz[ry] += siz[rx];
      t[rx] = ry;
    }
    return 1;
  }
  return 0;
}
bool cmp(muchii a, muchii b) { return (a.z < b.z); }
void kruskal() {
  int nrk = 0, i;
  sort(mm + 1, mm + m + 1, cmp);
  i = 1;
  while ((i <= m) && (nrk < n - 1)) {
    if (Union(mm[i].x, mm[i].y)) {
      nrk++;
      vizk[i] = 1;
      mk[nrk] = mm[i];
      vk[mm[i].x].push_back({mm[i].y, mm[i].z});
      vk[mm[i].y].push_back({mm[i].x, mm[i].z});
    }
    i++;
  }
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    q;
void dijkstra(int nodd) {
  nrspec++;
  int i, nod, vec;
  long long cost, ct;
  for (i = 1; i <= n; i++) {
    viz[i] = 0;
    dist[nrspec][i] = 1e15 + 5;
  }
  dist[nrspec][nodd] = 0;
  q.push({0, nodd});
  while (!q.empty()) {
    nod = q.top().second;
    ct = q.top().first;
    viz[nod] = 1;
    q.pop();
    for (auto it = v[nod].begin(); it != v[nod].end(); ++it)
      if (!viz[(*it).first]) {
        vec = (*it).first;
        cost = (*it).second;
        if (dist[nrspec][vec] > dist[nrspec][nod] + cost) {
          dist[nrspec][vec] = dist[nrspec][nod] + cost;
          q.push({dist[nrspec][vec], vec});
        }
      }
  }
}
void dinamica() {
  int i, j;
  for (i = 1; i <= n; i++) a[i][0] = t[i];
  for (j = 1; j <= log2(n); j++)
    for (i = 1; i <= n; i++) a[i][j] = a[a[i][j - 1]][j - 1];
}
int lca(int x, int y) {
  int j;
  if (niv[y] < niv[x]) swap(x, y);
  for (j = log2(n); j >= 0; j--)
    if (niv[a[y][j]] >= niv[x]) y = a[y][j];
  if (x == y) return x;
  for (j = log2(n); j >= 0; j--)
    if (a[x][j] != a[y][j]) {
      x = a[x][j];
      y = a[y][j];
    }
  return t[x];
}
void dfs(int tata, int nod) {
  viz[nod] = 1;
  t[nod] = tata;
  niv[nod] = niv[t[nod]] + 1;
  for (auto it = vk[nod].begin(); it != vk[nod].end(); ++it)
    if (!viz[(*it).first]) {
      d[(*it).first] += ((*it).second + d[nod]);
      dfs(nod, (*it).first);
    }
}
void sol() {
  kruskal();
  for (int i = 1; i <= m; i++)
    if (!vizk[i]) {
      dijkstra(mm[i].x);
      dijkstra(mm[i].y);
    }
  for (int i = 1; i <= n; i++) viz[i] = 0;
  dfs(0, 1);
  dinamica();
}
void afis() {
  int i, x, y, lcaa;
  long long mini;
  cin >> nrq;
  while (nrq--) {
    cin >> x >> y;
    lcaa = lca(x, y);
    mini = d[x] + d[y] - 2LL * d[lcaa];
    for (i = 1; i <= nrspec; i++) mini = min(mini, dist[i][x] + dist[i][y]);
    cout << mini << "\n";
  }
}
int main() {
  citire();
  sol();
  afis();
  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;
int n, m, t[100005], siz[100005], nrq, vizk[100005], nrspec, viz[100005],
    niv[100005], a[100005][21];
long long dist[50][100005], d[100005];
vector<pair<int, long long>> v[100005], vk[100005];
struct muchii {
  int x, y;
  long long z;
} mm[100005], mk[100005];
void citire() {
  int i, x, y, z;
  cin >> n >> m;
  for (i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
    mm[i].x = x;
    mm[i].y = y;
    mm[i].z = z;
  }
}
int Find(int x) {
  while (t[x] != 0) x = t[x];
  return x;
}
int Union(int x, int y) {
  int rx, ry;
  rx = Find(x);
  ry = Find(y);
  if (rx != ry) {
    if (siz[rx] > siz[ry]) {
      siz[rx] += siz[ry];
      t[ry] = rx;
    } else {
      siz[ry] += siz[rx];
      t[rx] = ry;
    }
    return 1;
  }
  return 0;
}
bool cmp(muchii a, muchii b) { return (a.z < b.z); }
void kruskal() {
  int nrk = 0, i;
  sort(mm + 1, mm + m + 1, cmp);
  i = 1;
  while ((i <= m) && (nrk < n - 1)) {
    if (Union(mm[i].x, mm[i].y)) {
      nrk++;
      vizk[i] = 1;
      mk[nrk] = mm[i];
      vk[mm[i].x].push_back({mm[i].y, mm[i].z});
      vk[mm[i].y].push_back({mm[i].x, mm[i].z});
    }
    i++;
  }
}
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    q;
void dijkstra(int nodd) {
  nrspec++;
  int i, nod, vec;
  long long cost, ct;
  for (i = 1; i <= n; i++) {
    viz[i] = 0;
    dist[nrspec][i] = 1e15 + 5;
  }
  dist[nrspec][nodd] = 0;
  q.push({0, nodd});
  while (!q.empty()) {
    nod = q.top().second;
    ct = q.top().first;
    viz[nod] = 1;
    q.pop();
    for (auto it = v[nod].begin(); it != v[nod].end(); ++it)
      if (!viz[(*it).first]) {
        vec = (*it).first;
        cost = (*it).second;
        if (dist[nrspec][vec] > dist[nrspec][nod] + cost) {
          dist[nrspec][vec] = dist[nrspec][nod] + cost;
          q.push({dist[nrspec][vec], vec});
        }
      }
  }
}
void dinamica() {
  int i, j;
  for (i = 1; i <= n; i++) a[i][0] = t[i];
  for (j = 1; j <= log2(n); j++)
    for (i = 1; i <= n; i++) a[i][j] = a[a[i][j - 1]][j - 1];
}
int lca(int x, int y) {
  int j;
  if (niv[y] < niv[x]) swap(x, y);
  for (j = log2(n); j >= 0; j--)
    if (niv[a[y][j]] >= niv[x]) y = a[y][j];
  if (x == y) return x;
  for (j = log2(n); j >= 0; j--)
    if (a[x][j] != a[y][j]) {
      x = a[x][j];
      y = a[y][j];
    }
  return t[x];
}
void dfs(int tata, int nod) {
  viz[nod] = 1;
  t[nod] = tata;
  niv[nod] = niv[t[nod]] + 1;
  for (auto it = vk[nod].begin(); it != vk[nod].end(); ++it)
    if (!viz[(*it).first]) {
      d[(*it).first] += ((*it).second + d[nod]);
      dfs(nod, (*it).first);
    }
}
void sol() {
  kruskal();
  for (int i = 1; i <= m; i++)
    if (!vizk[i]) {
      dijkstra(mm[i].x);
      dijkstra(mm[i].y);
    }
  for (int i = 1; i <= n; i++) viz[i] = 0;
  dfs(0, 1);
  dinamica();
}
void afis() {
  int i, x, y, lcaa;
  long long mini;
  cin >> nrq;
  while (nrq--) {
    cin >> x >> y;
    lcaa = lca(x, y);
    mini = d[x] + d[y] - 2LL * d[lcaa];
    for (i = 1; i <= nrspec; i++) mini = min(mini, dist[i][x] + dist[i][y]);
    cout << mini << "\n";
  }
}
int main() {
  citire();
  sol();
  afis();
  return 0;
}

```