#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar();
  x *= neg;
}
const int maxn = 100005;
const int maxm = 45;
const int mlog = 20;
int n, m, q, k;
int u[maxn], v[maxn], w[maxn], dep[maxn], f[maxn][mlog];
long long val[maxn], dist[maxm][maxn];
bool used[maxn], vis[maxn];
vector<pair<int, int> > edge[maxn];
vector<int> p;
priority_queue<pair<long long, int> > pq;
void dij(int s, long long *dist) {
  for (int(i) = 1; (i) <= n; i++) dist[i] = 1e18;
  dist[s] = 0;
  pq.push(make_pair(0, s));
  while (!pq.empty()) {
    pair<long long, int> x = pq.top();
    pq.pop();
    x.first = -x.first;
    if (dist[x.second] != x.first) continue;
    for (int(i) = 0; (i) < edge[x.second].size(); i++) {
      pair<int, int> y = edge[x.second][i];
      if (dist[y.first] > x.first + w[y.second]) {
        dist[y.first] = x.first + w[y.second];
        pq.push(make_pair(-dist[y.first], y.first));
      }
    }
  }
}
void dfs(int x, int par, long long cur) {
  f[x][0] = par;
  val[x] = cur;
  vis[x] = true;
  for (int(i) = 0; (i) < edge[x].size(); i++) {
    int y = edge[x][i].first;
    if (!vis[y]) {
      used[edge[x][i].second] = true;
      dep[y] = dep[x] + 1;
      dfs(y, x, cur + w[edge[x][i].second]);
    }
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  int diff = dep[y] - dep[x];
  for (int i = mlog - 1; i >= 0; i--)
    if (diff & (1 << i)) y = f[y][i];
  if (x == y) return x;
  for (int i = mlog - 1; i >= 0; i--) {
    if (f[x][i] != f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  }
  return f[x][0];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = 1; (i) <= m; i++) {
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
    edge[u[i]].push_back(make_pair(v[i], i));
    edge[v[i]].push_back(make_pair(u[i], i));
  }
  dfs(1, 0, 0);
  for (int(j) = 1; (j) <= mlog - 1; j++)
    for (int(i) = 1; (i) <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
  for (int(i) = 1; (i) <= m; i++)
    if (!used[i]) p.push_back(u[i]), p.push_back(v[i]);
  sort(p.begin(), p.end());
  p.resize(unique(p.begin(), p.end()) - p.begin());
  for (int(i) = 0; (i) < p.size(); i++) dij(p[i], dist[i]);
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    long long ans = val[x] + val[y] - 2 * val[lca(x, y)];
    for (int(i) = 0; (i) < p.size(); i++) {
      ans = min(ans, val[x] + val[p[i]] - 2 * val[lca(x, p[i])] + dist[i][y]);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Construct a Cpp code solution to the problem outlined:

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(2)
using namespace std;
void qread(int &x) {
  int neg = 1;
  x = 0;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') neg = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') x = 10 * x + c - '0', c = getchar();
  x *= neg;
}
const int maxn = 100005;
const int maxm = 45;
const int mlog = 20;
int n, m, q, k;
int u[maxn], v[maxn], w[maxn], dep[maxn], f[maxn][mlog];
long long val[maxn], dist[maxm][maxn];
bool used[maxn], vis[maxn];
vector<pair<int, int> > edge[maxn];
vector<int> p;
priority_queue<pair<long long, int> > pq;
void dij(int s, long long *dist) {
  for (int(i) = 1; (i) <= n; i++) dist[i] = 1e18;
  dist[s] = 0;
  pq.push(make_pair(0, s));
  while (!pq.empty()) {
    pair<long long, int> x = pq.top();
    pq.pop();
    x.first = -x.first;
    if (dist[x.second] != x.first) continue;
    for (int(i) = 0; (i) < edge[x.second].size(); i++) {
      pair<int, int> y = edge[x.second][i];
      if (dist[y.first] > x.first + w[y.second]) {
        dist[y.first] = x.first + w[y.second];
        pq.push(make_pair(-dist[y.first], y.first));
      }
    }
  }
}
void dfs(int x, int par, long long cur) {
  f[x][0] = par;
  val[x] = cur;
  vis[x] = true;
  for (int(i) = 0; (i) < edge[x].size(); i++) {
    int y = edge[x][i].first;
    if (!vis[y]) {
      used[edge[x][i].second] = true;
      dep[y] = dep[x] + 1;
      dfs(y, x, cur + w[edge[x][i].second]);
    }
  }
}
int lca(int x, int y) {
  if (dep[x] > dep[y]) swap(x, y);
  int diff = dep[y] - dep[x];
  for (int i = mlog - 1; i >= 0; i--)
    if (diff & (1 << i)) y = f[y][i];
  if (x == y) return x;
  for (int i = mlog - 1; i >= 0; i--) {
    if (f[x][i] != f[y][i]) {
      x = f[x][i];
      y = f[y][i];
    }
  }
  return f[x][0];
}
int main() {
  scanf("%d%d", &n, &m);
  for (int(i) = 1; (i) <= m; i++) {
    scanf("%d%d%d", &u[i], &v[i], &w[i]);
    edge[u[i]].push_back(make_pair(v[i], i));
    edge[v[i]].push_back(make_pair(u[i], i));
  }
  dfs(1, 0, 0);
  for (int(j) = 1; (j) <= mlog - 1; j++)
    for (int(i) = 1; (i) <= n; i++) f[i][j] = f[f[i][j - 1]][j - 1];
  for (int(i) = 1; (i) <= m; i++)
    if (!used[i]) p.push_back(u[i]), p.push_back(v[i]);
  sort(p.begin(), p.end());
  p.resize(unique(p.begin(), p.end()) - p.begin());
  for (int(i) = 0; (i) < p.size(); i++) dij(p[i], dist[i]);
  scanf("%d", &q);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    long long ans = val[x] + val[y] - 2 * val[lca(x, y)];
    for (int(i) = 0; (i) < p.size(); i++) {
      ans = min(ans, val[x] + val[p[i]] - 2 * val[lca(x, p[i])] + dist[i][y]);
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

```