#include <bits/stdc++.h>
using namespace std;
const int maxn = 100050;
const int inf = 0x3f3f3f3f;
struct Edge {
  int v, w;
};
vector<Edge> G[maxn];
bool vis[maxn];
vector<int> st;
int dep[maxn], f[maxn][20];
long long dis[maxn];
void dfs(int x, int par, long long sum) {
  f[x][0] = par;
  dep[x] = dep[par] + 1;
  dis[x] = sum;
  vis[x] = true;
  for (int i = 1; f[x][i - 1]; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
  for (auto e : G[x]) {
    int v = e.v;
    if (v != par && vis[v]) {
      st.push_back(v), st.push_back(x);
    }
    if (v != par && !vis[v]) dfs(v, x, sum + e.w);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int h = dep[x] - dep[y];
  for (int i = 0; h; ++i) {
    if (h & (1 << i)) {
      x = f[x][i];
      h ^= (1 << i);
    }
  }
  if (x == y) return x;
  for (int i = 17; i >= 0; --i) {
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  }
  return f[x][0];
}
long long Dis(int x, int y) {
  int z = lca(x, y);
  return dis[x] + dis[y] - dis[z] * 2;
}
struct Dijkstra {
  long long d[maxn];
  struct node {
    int x;
    long long d;
    node(int x = 0, long long d = 0) : x(x), d(d) {}
    bool operator<(const node& t) const { return d > t.d; }
  };
  void run(int s) {
    memset(d, 0x3f, sizeof d);
    memset(vis, false, sizeof vis);
    priority_queue<node> q;
    q.push(node(s, 0)), d[s] = 0;
    while (!q.empty()) {
      node u = q.top();
      q.pop();
      if (vis[u.x]) continue;
      vis[u.x] = true;
      for (auto e : G[u.x]) {
        int v = e.v;
        if (!vis[v] && d[v] > u.d + e.w) {
          d[v] = u.d + e.w;
          q.push(node(v, d[v]));
        }
      }
    }
  }
} dij[50];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int x, y, w;
    scanf("%d%d%d", &x, &y, &w);
    G[x].push_back({y, w});
    G[y].push_back({x, w});
  }
  dfs(1, 0, 0);
  sort(st.begin(), st.end());
  int sz = unique(st.begin(), st.end()) - st.begin();
  while (st.size() > sz) st.pop_back();
  for (int i = 0; i < sz; ++i) {
    dij[i].run(st[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v;
    scanf("%d%d", &u, &v);
    long long ans = Dis(u, v);
    for (int i = 0; i < sz; ++i) ans = min(ans, dij[i].d[u] + dij[i].d[v]);
    printf("%lld\n", ans);
  }
  return 0;
}

### Prompt

Your task is to create 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 = 100050;
const int inf = 0x3f3f3f3f;
struct Edge {
  int v, w;
};
vector<Edge> G[maxn];
bool vis[maxn];
vector<int> st;
int dep[maxn], f[maxn][20];
long long dis[maxn];
void dfs(int x, int par, long long sum) {
  f[x][0] = par;
  dep[x] = dep[par] + 1;
  dis[x] = sum;
  vis[x] = true;
  for (int i = 1; f[x][i - 1]; ++i) f[x][i] = f[f[x][i - 1]][i - 1];
  for (auto e : G[x]) {
    int v = e.v;
    if (v != par && vis[v]) {
      st.push_back(v), st.push_back(x);
    }
    if (v != par && !vis[v]) dfs(v, x, sum + e.w);
  }
}
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int h = dep[x] - dep[y];
  for (int i = 0; h; ++i) {
    if (h & (1 << i)) {
      x = f[x][i];
      h ^= (1 << i);
    }
  }
  if (x == y) return x;
  for (int i = 17; i >= 0; --i) {
    if (f[x][i] != f[y][i]) x = f[x][i], y = f[y][i];
  }
  return f[x][0];
}
long long Dis(int x, int y) {
  int z = lca(x, y);
  return dis[x] + dis[y] - dis[z] * 2;
}
struct Dijkstra {
  long long d[maxn];
  struct node {
    int x;
    long long d;
    node(int x = 0, long long d = 0) : x(x), d(d) {}
    bool operator<(const node& t) const { return d > t.d; }
  };
  void run(int s) {
    memset(d, 0x3f, sizeof d);
    memset(vis, false, sizeof vis);
    priority_queue<node> q;
    q.push(node(s, 0)), d[s] = 0;
    while (!q.empty()) {
      node u = q.top();
      q.pop();
      if (vis[u.x]) continue;
      vis[u.x] = true;
      for (auto e : G[u.x]) {
        int v = e.v;
        if (!vis[v] && d[v] > u.d + e.w) {
          d[v] = u.d + e.w;
          q.push(node(v, d[v]));
        }
      }
    }
  }
} dij[50];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i) {
    int x, y, w;
    scanf("%d%d%d", &x, &y, &w);
    G[x].push_back({y, w});
    G[y].push_back({x, w});
  }
  dfs(1, 0, 0);
  sort(st.begin(), st.end());
  int sz = unique(st.begin(), st.end()) - st.begin();
  while (st.size() > sz) st.pop_back();
  for (int i = 0; i < sz; ++i) {
    dij[i].run(st[i]);
  }
  int q;
  scanf("%d", &q);
  while (q--) {
    int u, v;
    scanf("%d%d", &u, &v);
    long long ans = Dis(u, v);
    for (int i = 0; i < sz; ++i) ans = min(ans, dij[i].d[u] + dij[i].d[v]);
    printf("%lld\n", ans);
  }
  return 0;
}

```