#include <bits/stdc++.h>
using namespace std;
const int maxn = 2e5 + 100;
vector<pair<long long, long long> > graph[100010];
struct subset {
  int parent;
  int rank;
};
struct Node {
  int u, v;
  long long w;
  Node() {}
  Node(int _u, int _v, long long _w) {
    u = _u;
    v = _v;
    w = _w;
  }
  bool operator<(const Node &tmp) const { return w > tmp.w; }
};
priority_queue<Node> pq;
vector<int> special;
vector<long long> dist[maxn];
subset subsets[maxn];
vector<pair<long long, long long> > g[maxn];
int n, test;
long long d[maxn];
int dep[maxn];
int f[maxn][22];
void dfs(int u, int fa, long long w) {
  f[u][0] = fa;
  d[u] = w;
  dep[u] = dep[fa] + 1;
  for (int i = 1; i <= 20; i++) {
    f[u][i] = f[f[u][i - 1]][i - 1];
  }
  for (auto v : g[u]) {
    if (v.first == fa) continue;
    dfs(v.first, u, w + v.second);
  }
}
int get_lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  int delta = dep[a] - dep[b];
  for (int i = 20; i >= 0; i--) {
    if (delta >> i & 1) {
      a = f[a][i];
    }
  }
  if (a == b) return a;
  for (int i = 20; i >= 0; i--) {
    if (f[a][i] != f[b][i]) {
      a = f[a][i];
      b = f[b][i];
    }
  }
  return f[a][0];
}
void SET() {
  for (int i = 0; i < maxn; i++) {
    subsets[i].parent = i;
    subsets[i].rank = 0;
  }
}
int find(struct subset subsets[], int i) {
  if (subsets[i].parent != i)
    subsets[i].parent = find(subsets, subsets[i].parent);
  return subsets[i].parent;
}
void Union(struct subset subsets[], int x, int y) {
  int xroot = find(subsets, x);
  int yroot = find(subsets, y);
  if (subsets[xroot].rank < subsets[yroot].rank)
    subsets[xroot].parent = yroot;
  else if (subsets[xroot].rank > subsets[yroot].rank)
    subsets[yroot].parent = xroot;
  else {
    subsets[yroot].parent = xroot;
    subsets[xroot].rank++;
  }
}
vector<long long> Dijkstra(int N, long long s) {
  vector<long long> dis(N + 1, 1e18 + 7);
  dis[s] = 0;
  priority_queue<Node> pq;
  pq.push(Node(s, 0, 0));
  while (pq.size()) {
    long long u = pq.top().u;
    long long dd = pq.top().w;
    pq.pop();
    for (auto pr : graph[u]) {
      long long nw = dd + pr.second;
      long long v = pr.first;
      if (dis[v] <= nw) continue;
      dis[v] = nw;
      pq.push(Node(v, 0, dis[v]));
    }
  }
  return dis;
}
int main() {
  int n, m;
  ios_base::sync_with_stdio(false);
  SET();
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    graph[u].push_back(make_pair(v, w));
    graph[v].push_back(make_pair(u, w));
    pq.push(Node(u, v, w));
  }
  while (!pq.empty()) {
    int u, v;
    u = pq.top().u;
    v = pq.top().v;
    long long w = pq.top().w;
    pq.pop();
    int f1 = find(subsets, u);
    int f2 = find(subsets, v);
    if (f1 != f2) {
      g[u].push_back(make_pair(v, w));
      g[v].push_back(make_pair(u, w));
      Union(subsets, f1, f2);
    } else {
      special.push_back(u);
      special.push_back(v);
    }
  }
  dfs(1, 0, 0);
  for (auto x : special) {
    dist[x] = Dijkstra(n, x);
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    long long lc = get_lca(u, v);
    long long mn = d[u] + d[v] - 2 * d[lc];
    for (auto x : special) {
      mn = min(mn, dist[x][u] + dist[x][v]);
    }
    cout << mn << endl;
  }
  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>
using namespace std;
const int maxn = 2e5 + 100;
vector<pair<long long, long long> > graph[100010];
struct subset {
  int parent;
  int rank;
};
struct Node {
  int u, v;
  long long w;
  Node() {}
  Node(int _u, int _v, long long _w) {
    u = _u;
    v = _v;
    w = _w;
  }
  bool operator<(const Node &tmp) const { return w > tmp.w; }
};
priority_queue<Node> pq;
vector<int> special;
vector<long long> dist[maxn];
subset subsets[maxn];
vector<pair<long long, long long> > g[maxn];
int n, test;
long long d[maxn];
int dep[maxn];
int f[maxn][22];
void dfs(int u, int fa, long long w) {
  f[u][0] = fa;
  d[u] = w;
  dep[u] = dep[fa] + 1;
  for (int i = 1; i <= 20; i++) {
    f[u][i] = f[f[u][i - 1]][i - 1];
  }
  for (auto v : g[u]) {
    if (v.first == fa) continue;
    dfs(v.first, u, w + v.second);
  }
}
int get_lca(int a, int b) {
  if (dep[a] < dep[b]) swap(a, b);
  int delta = dep[a] - dep[b];
  for (int i = 20; i >= 0; i--) {
    if (delta >> i & 1) {
      a = f[a][i];
    }
  }
  if (a == b) return a;
  for (int i = 20; i >= 0; i--) {
    if (f[a][i] != f[b][i]) {
      a = f[a][i];
      b = f[b][i];
    }
  }
  return f[a][0];
}
void SET() {
  for (int i = 0; i < maxn; i++) {
    subsets[i].parent = i;
    subsets[i].rank = 0;
  }
}
int find(struct subset subsets[], int i) {
  if (subsets[i].parent != i)
    subsets[i].parent = find(subsets, subsets[i].parent);
  return subsets[i].parent;
}
void Union(struct subset subsets[], int x, int y) {
  int xroot = find(subsets, x);
  int yroot = find(subsets, y);
  if (subsets[xroot].rank < subsets[yroot].rank)
    subsets[xroot].parent = yroot;
  else if (subsets[xroot].rank > subsets[yroot].rank)
    subsets[yroot].parent = xroot;
  else {
    subsets[yroot].parent = xroot;
    subsets[xroot].rank++;
  }
}
vector<long long> Dijkstra(int N, long long s) {
  vector<long long> dis(N + 1, 1e18 + 7);
  dis[s] = 0;
  priority_queue<Node> pq;
  pq.push(Node(s, 0, 0));
  while (pq.size()) {
    long long u = pq.top().u;
    long long dd = pq.top().w;
    pq.pop();
    for (auto pr : graph[u]) {
      long long nw = dd + pr.second;
      long long v = pr.first;
      if (dis[v] <= nw) continue;
      dis[v] = nw;
      pq.push(Node(v, 0, dis[v]));
    }
  }
  return dis;
}
int main() {
  int n, m;
  ios_base::sync_with_stdio(false);
  SET();
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    graph[u].push_back(make_pair(v, w));
    graph[v].push_back(make_pair(u, w));
    pq.push(Node(u, v, w));
  }
  while (!pq.empty()) {
    int u, v;
    u = pq.top().u;
    v = pq.top().v;
    long long w = pq.top().w;
    pq.pop();
    int f1 = find(subsets, u);
    int f2 = find(subsets, v);
    if (f1 != f2) {
      g[u].push_back(make_pair(v, w));
      g[v].push_back(make_pair(u, w));
      Union(subsets, f1, f2);
    } else {
      special.push_back(u);
      special.push_back(v);
    }
  }
  dfs(1, 0, 0);
  for (auto x : special) {
    dist[x] = Dijkstra(n, x);
  }
  int q;
  cin >> q;
  while (q--) {
    int u, v;
    cin >> u >> v;
    long long lc = get_lca(u, v);
    long long mn = d[u] + d[v] - 2 * d[lc];
    for (auto x : special) {
      mn = min(mn, dist[x][u] + dist[x][v]);
    }
    cout << mn << endl;
  }
  return 0;
}

```