#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1e5 + 7;
int m, n;
int par[MAXN];
vector<pair<int, int> > edges[MAXN];
long long d[MAXN];
struct Node {
  int l, r, w;
};
vector<Node> save;
bool cmp(Node x, Node y) { return (x.w < y.w); }
int root(int u) { return par[u] < 0 ? u : (par[u] = root(par[u])); }
int q;
vector<int> pos, vertex;
bool Merge(int u, int v) {
  if ((u = root(u)) == (v = root(v))) return false;
  if (par[u] > par[v]) swap(u, v);
  par[u] += par[v];
  par[v] = u;
  return true;
}
int dep[MAXN];
int t[MAXN][20];
void MST() {
  memset(par, -1, sizeof(par));
  sort(save.begin(), save.end(), cmp);
  for (int i = 0; i < (int)save.size(); i++)
    if (Merge(save[i].l, save[i].r)) {
      edges[save[i].l].push_back({save[i].w, save[i].r});
      edges[save[i].r].push_back({save[i].w, save[i].l});
    } else {
      pos.push_back(i);
      vertex.push_back(save[i].l);
      vertex.push_back(save[i].r);
    }
}
void DFS(int u, int cnt) {
  dep[u] = cnt;
  for (int i = 0; i < (int)edges[u].size(); i++) {
    int v = edges[u][i].second;
    if (!dep[v]) {
      t[v][0] = u;
      DFS(v, cnt + 1);
    }
  }
}
void LCA() {
  for (int i = 1; i <= 18; i++)
    for (int j = 1; j <= n; j++) t[j][i] = t[t[j][i - 1]][i - 1];
}
int Find_LCA(int x, int y) {
  for (int i = 18; i >= 0; i--)
    if (dep[t[x][i]] >= dep[y]) x = t[x][i];
  for (int i = 18; i >= 0; i--)
    if (dep[t[y][i]] >= dep[x]) y = t[y][i];
  for (int i = 18; i >= 0; i--)
    if (t[x][i] != t[y][i]) {
      x = t[x][i];
      y = t[y][i];
    }
  if (x != y) return t[x][0];
  return x;
}
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    pq;
vector<long long> dis[MAXN];
void Dijkstra(int k, long long d[], bool ok) {
  for (int i = 1; i <= n; i++) d[i] = 1e18;
  d[k] = 0;
  while (!pq.empty()) pq.pop();
  pq.push({0, k});
  while (!pq.empty()) {
    int u = pq.top().second;
    long long du = pq.top().first;
    pq.pop();
    if (d[u] != du) continue;
    for (int i = 0; i < (int)edges[u].size(); i++) {
      int v = edges[u][i].second;
      int dv = edges[u][i].first;
      if (d[v] > du + dv) {
        d[v] = du + dv;
        pq.push({d[v], v});
      }
    }
  }
  if (ok) {
    dis[k].push_back(0);
    for (int i = 1; i <= n; i++) dis[k].push_back(d[i]);
  }
}
long long dis_MST[MAXN];
void Init() {
  MST();
  DFS(1, 1);
  LCA();
  Dijkstra(1, dis_MST, false);
  for (int i = 0; i < (int)pos.size(); i++) {
    int j = pos[i];
    edges[save[j].l].push_back({save[j].w, save[j].r});
    edges[save[j].r].push_back({save[j].w, save[j].l});
  }
  sort(vertex.begin(), vertex.end());
  vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
  for (int i = 0; i < (int)vertex.size(); i++) Dijkstra(vertex[i], d, true);
}
void Input() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int l, r, w;
    cin >> l >> r >> w;
    save.push_back({l, r, w});
  }
}
long long calc(int u, int v) {
  int pt = Find_LCA(u, v);
  return (dis_MST[v] + dis_MST[u] - 2 * dis_MST[pt]);
}
void Output() {
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    long long ans = calc(u, v);
    for (int i = 0; i < (int)vertex.size(); i++) {
      int ll = vertex[i];
      ans = min(ans, dis[ll][u] + dis[ll][v]);
    }
    cout << ans << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  Input();
  Init();
  Output();
  return 0;
}

### 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 + 7;
int m, n;
int par[MAXN];
vector<pair<int, int> > edges[MAXN];
long long d[MAXN];
struct Node {
  int l, r, w;
};
vector<Node> save;
bool cmp(Node x, Node y) { return (x.w < y.w); }
int root(int u) { return par[u] < 0 ? u : (par[u] = root(par[u])); }
int q;
vector<int> pos, vertex;
bool Merge(int u, int v) {
  if ((u = root(u)) == (v = root(v))) return false;
  if (par[u] > par[v]) swap(u, v);
  par[u] += par[v];
  par[v] = u;
  return true;
}
int dep[MAXN];
int t[MAXN][20];
void MST() {
  memset(par, -1, sizeof(par));
  sort(save.begin(), save.end(), cmp);
  for (int i = 0; i < (int)save.size(); i++)
    if (Merge(save[i].l, save[i].r)) {
      edges[save[i].l].push_back({save[i].w, save[i].r});
      edges[save[i].r].push_back({save[i].w, save[i].l});
    } else {
      pos.push_back(i);
      vertex.push_back(save[i].l);
      vertex.push_back(save[i].r);
    }
}
void DFS(int u, int cnt) {
  dep[u] = cnt;
  for (int i = 0; i < (int)edges[u].size(); i++) {
    int v = edges[u][i].second;
    if (!dep[v]) {
      t[v][0] = u;
      DFS(v, cnt + 1);
    }
  }
}
void LCA() {
  for (int i = 1; i <= 18; i++)
    for (int j = 1; j <= n; j++) t[j][i] = t[t[j][i - 1]][i - 1];
}
int Find_LCA(int x, int y) {
  for (int i = 18; i >= 0; i--)
    if (dep[t[x][i]] >= dep[y]) x = t[x][i];
  for (int i = 18; i >= 0; i--)
    if (dep[t[y][i]] >= dep[x]) y = t[y][i];
  for (int i = 18; i >= 0; i--)
    if (t[x][i] != t[y][i]) {
      x = t[x][i];
      y = t[y][i];
    }
  if (x != y) return t[x][0];
  return x;
}
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    pq;
vector<long long> dis[MAXN];
void Dijkstra(int k, long long d[], bool ok) {
  for (int i = 1; i <= n; i++) d[i] = 1e18;
  d[k] = 0;
  while (!pq.empty()) pq.pop();
  pq.push({0, k});
  while (!pq.empty()) {
    int u = pq.top().second;
    long long du = pq.top().first;
    pq.pop();
    if (d[u] != du) continue;
    for (int i = 0; i < (int)edges[u].size(); i++) {
      int v = edges[u][i].second;
      int dv = edges[u][i].first;
      if (d[v] > du + dv) {
        d[v] = du + dv;
        pq.push({d[v], v});
      }
    }
  }
  if (ok) {
    dis[k].push_back(0);
    for (int i = 1; i <= n; i++) dis[k].push_back(d[i]);
  }
}
long long dis_MST[MAXN];
void Init() {
  MST();
  DFS(1, 1);
  LCA();
  Dijkstra(1, dis_MST, false);
  for (int i = 0; i < (int)pos.size(); i++) {
    int j = pos[i];
    edges[save[j].l].push_back({save[j].w, save[j].r});
    edges[save[j].r].push_back({save[j].w, save[j].l});
  }
  sort(vertex.begin(), vertex.end());
  vertex.erase(unique(vertex.begin(), vertex.end()), vertex.end());
  for (int i = 0; i < (int)vertex.size(); i++) Dijkstra(vertex[i], d, true);
}
void Input() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int l, r, w;
    cin >> l >> r >> w;
    save.push_back({l, r, w});
  }
}
long long calc(int u, int v) {
  int pt = Find_LCA(u, v);
  return (dis_MST[v] + dis_MST[u] - 2 * dis_MST[pt]);
}
void Output() {
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    long long ans = calc(u, v);
    for (int i = 0; i < (int)vertex.size(); i++) {
      int ll = vertex[i];
      ans = min(ans, dis[ll][u] + dis[ll][v]);
    }
    cout << ans << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  Input();
  Init();
  Output();
  return 0;
}

```