#include <bits/stdc++.h>
using namespace std;
const int NN = (int)1e5 + 5;
const int LOG = 20;
int n, m;
long long dist[21][NN];
bool in_tree[NN];
vector<int> adj[NN];
vector<int> w[NN];
int parent[NN];
int H[NN];
vector<int> tree_adj[NN];
vector<int> tree_w[NN];
long long tree_dist[NN];
int tree_depth[NN];
int jump[LOG][NN];
void dfs(int node, int father, long long d_sofar, int depth) {
  tree_dist[node] = d_sofar;
  jump[0][node] = father;
  tree_depth[node] = depth;
  for (int i = 0; i < ((int)tree_adj[node].size()); i++) {
    int child = tree_adj[node][i];
    int w_e = tree_w[node][i];
    if (child == father) continue;
    dfs(child, node, d_sofar + w_e, depth + 1);
  }
}
void jumping() {
  memset(jump, 0, sizeof(jump));
  dfs(1, 0, 0, 0);
  for (int i = 1; i < LOG; i++) {
    for (int j = 1; j <= n; j++) {
      jump[i][j] = jump[i - 1][jump[i - 1][j]];
    }
  }
}
int lca(int x, int y) {
  if (tree_depth[x] < tree_depth[y]) swap(x, y);
  for (int i = LOG - 1; i >= 0; i--) {
    if (jump[i][x] > 0 && tree_depth[jump[i][x]] >= tree_depth[y]) {
      x = jump[i][x];
    }
  }
  assert(tree_depth[y] == tree_depth[x]);
  if (x == y) return x;
  for (int i = LOG - 1; i >= 0; i--) {
    if (jump[i][x] != jump[i][y]) {
      x = jump[i][x];
      y = jump[i][y];
    }
  }
  return jump[0][x];
}
long long distance(int x, int y) {
  return tree_dist[x] + tree_dist[y] - 2 * tree_dist[lca(x, y)];
}
struct Node {
  int node;
  long long dist;
  Node(int _node, long long _dist) {
    this->node = _node;
    this->dist = _dist;
  }
  friend bool operator<(const Node& A, const Node& B) {
    return A.dist > B.dist;
  }
};
void dijkstra(int id, int src) {
  fill(in_tree + 1, in_tree + n + 1, false);
  fill(dist[id] + 1, dist[id] + n + 1, -1);
  priority_queue<Node> s;
  dist[id][src] = 0;
  s.push(Node(src, dist[id][src]));
  while (!s.empty()) {
    Node x = s.top();
    s.pop();
    long long d_sofar = x.dist;
    int node = x.node;
    if (in_tree[node]) continue;
    in_tree[node] = true;
    for (int i = 0; i < ((int)adj[node].size()); i++) {
      int ngh = adj[node][i];
      int edge_weight = w[node][i];
      if (dist[id][ngh] == -1 || dist[id][ngh] > d_sofar + edge_weight) {
        dist[id][ngh] = d_sofar + edge_weight;
        s.push(Node(ngh, dist[id][ngh]));
      }
    }
  }
  return;
}
int root(int x) { return parent[x] = (x == parent[x] ? x : root(parent[x])); }
bool join(int x, int y) {
  x = root(x), y = root(y);
  if (x == y)
    return false;
  else {
    if (H[x] < H[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (H[x] == H[y]) H[x]++;
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  vector<tuple<int, int, int> > edges;
  for (int i = 0; i < m; i++) {
    int u, v, e_w;
    scanf("%d%d%d", &u, &v, &e_w);
    adj[u].push_back(v);
    w[u].push_back(e_w);
    adj[v].push_back(u);
    w[v].push_back(e_w);
    edges.push_back({u, v, e_w});
  }
  for (int i = 1; i <= n; i++) parent[i] = i, H[i] = 0;
  vector<int> src_nodes;
  for (auto& e : edges) {
    auto& [u, v, w_e] = e;
    if (join(u, v)) {
      tree_adj[u].push_back(v);
      tree_adj[v].push_back(u);
      tree_w[u].push_back(w_e);
      tree_w[v].push_back(w_e);
    } else {
      src_nodes.push_back(u);
    }
  }
  sort(src_nodes.begin(), src_nodes.end());
  src_nodes.resize(unique(src_nodes.begin(), src_nodes.end()) -
                   src_nodes.begin());
  for (int id = 0; id < ((int)src_nodes.size()); id++)
    dijkstra(id, src_nodes[id]);
  jumping();
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    long long best = 1LL * NN * ((long long)1e9 + 100);
    for (int i = 0; i < ((int)src_nodes.size()); i++) {
      int src = src_nodes[i];
      assert(dist[i][x] != -1);
      assert(dist[i][y] != -1);
      best = min(best, dist[i][x] + dist[i][y]);
    }
    best = min(best, distance(x, y));
    printf("%lld\n", best);
  }
  return 0;
}

### Prompt

Please provide a cpp coded solution 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>
using namespace std;
const int NN = (int)1e5 + 5;
const int LOG = 20;
int n, m;
long long dist[21][NN];
bool in_tree[NN];
vector<int> adj[NN];
vector<int> w[NN];
int parent[NN];
int H[NN];
vector<int> tree_adj[NN];
vector<int> tree_w[NN];
long long tree_dist[NN];
int tree_depth[NN];
int jump[LOG][NN];
void dfs(int node, int father, long long d_sofar, int depth) {
  tree_dist[node] = d_sofar;
  jump[0][node] = father;
  tree_depth[node] = depth;
  for (int i = 0; i < ((int)tree_adj[node].size()); i++) {
    int child = tree_adj[node][i];
    int w_e = tree_w[node][i];
    if (child == father) continue;
    dfs(child, node, d_sofar + w_e, depth + 1);
  }
}
void jumping() {
  memset(jump, 0, sizeof(jump));
  dfs(1, 0, 0, 0);
  for (int i = 1; i < LOG; i++) {
    for (int j = 1; j <= n; j++) {
      jump[i][j] = jump[i - 1][jump[i - 1][j]];
    }
  }
}
int lca(int x, int y) {
  if (tree_depth[x] < tree_depth[y]) swap(x, y);
  for (int i = LOG - 1; i >= 0; i--) {
    if (jump[i][x] > 0 && tree_depth[jump[i][x]] >= tree_depth[y]) {
      x = jump[i][x];
    }
  }
  assert(tree_depth[y] == tree_depth[x]);
  if (x == y) return x;
  for (int i = LOG - 1; i >= 0; i--) {
    if (jump[i][x] != jump[i][y]) {
      x = jump[i][x];
      y = jump[i][y];
    }
  }
  return jump[0][x];
}
long long distance(int x, int y) {
  return tree_dist[x] + tree_dist[y] - 2 * tree_dist[lca(x, y)];
}
struct Node {
  int node;
  long long dist;
  Node(int _node, long long _dist) {
    this->node = _node;
    this->dist = _dist;
  }
  friend bool operator<(const Node& A, const Node& B) {
    return A.dist > B.dist;
  }
};
void dijkstra(int id, int src) {
  fill(in_tree + 1, in_tree + n + 1, false);
  fill(dist[id] + 1, dist[id] + n + 1, -1);
  priority_queue<Node> s;
  dist[id][src] = 0;
  s.push(Node(src, dist[id][src]));
  while (!s.empty()) {
    Node x = s.top();
    s.pop();
    long long d_sofar = x.dist;
    int node = x.node;
    if (in_tree[node]) continue;
    in_tree[node] = true;
    for (int i = 0; i < ((int)adj[node].size()); i++) {
      int ngh = adj[node][i];
      int edge_weight = w[node][i];
      if (dist[id][ngh] == -1 || dist[id][ngh] > d_sofar + edge_weight) {
        dist[id][ngh] = d_sofar + edge_weight;
        s.push(Node(ngh, dist[id][ngh]));
      }
    }
  }
  return;
}
int root(int x) { return parent[x] = (x == parent[x] ? x : root(parent[x])); }
bool join(int x, int y) {
  x = root(x), y = root(y);
  if (x == y)
    return false;
  else {
    if (H[x] < H[y]) {
      parent[x] = y;
    } else {
      parent[y] = x;
      if (H[x] == H[y]) H[x]++;
    }
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &m);
  vector<tuple<int, int, int> > edges;
  for (int i = 0; i < m; i++) {
    int u, v, e_w;
    scanf("%d%d%d", &u, &v, &e_w);
    adj[u].push_back(v);
    w[u].push_back(e_w);
    adj[v].push_back(u);
    w[v].push_back(e_w);
    edges.push_back({u, v, e_w});
  }
  for (int i = 1; i <= n; i++) parent[i] = i, H[i] = 0;
  vector<int> src_nodes;
  for (auto& e : edges) {
    auto& [u, v, w_e] = e;
    if (join(u, v)) {
      tree_adj[u].push_back(v);
      tree_adj[v].push_back(u);
      tree_w[u].push_back(w_e);
      tree_w[v].push_back(w_e);
    } else {
      src_nodes.push_back(u);
    }
  }
  sort(src_nodes.begin(), src_nodes.end());
  src_nodes.resize(unique(src_nodes.begin(), src_nodes.end()) -
                   src_nodes.begin());
  for (int id = 0; id < ((int)src_nodes.size()); id++)
    dijkstra(id, src_nodes[id]);
  jumping();
  int q;
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    long long best = 1LL * NN * ((long long)1e9 + 100);
    for (int i = 0; i < ((int)src_nodes.size()); i++) {
      int src = src_nodes[i];
      assert(dist[i][x] != -1);
      assert(dist[i][y] != -1);
      best = min(best, dist[i][x] + dist[i][y]);
    }
    best = min(best, distance(x, y));
    printf("%lld\n", best);
  }
  return 0;
}

```