#include <bits/stdc++.h>
using namespace std;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  bool f = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  x = f ? -x : x;
}
const int N = 1e5 + 7, M = 2e5 + 7, K = 45;
int n, m;
struct Graph {
  int head[N], to[M], nxt[M], w[M], tot;
  void adde(int u, int v, int l) {
    to[++tot] = v;
    nxt[tot] = head[u];
    head[u] = tot;
    w[tot] = l;
  }
} G, T;
long long dis[K][N];
bool vis[N];
void dij(long long *dis, int S) {
  for (int i = (1), _ = (n); i <= _; ++i) vis[i] = false;
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      q;
  dis[S] = 0;
  q.push(make_pair(0, S));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u]) continue;
    vis[u] = true;
    for (int i = G.head[u], v = G.to[i]; i; i = G.nxt[i], v = G.to[i]) {
      if (dis[v] > dis[u] + G.w[i]) {
        dis[v] = dis[u] + G.w[i];
        q.push(make_pair(dis[v], v));
      }
    }
  }
}
int dep[N], f[N][17];
long long dst[N];
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int len = dep[x] - dep[y], i = 0; len; len >>= 1, ++i) {
    if (len & 1) x = f[x][i];
  }
  if (!(x ^ y)) return x;
  for (int i = (16), _ = (0); i >= _; --i) {
    if (f[x][i] ^ f[y][i]) x = f[x][i], y = f[y][i];
  }
  return f[x][0];
}
long long dist(int x, int y) { return dst[x] + dst[y] - (dst[lca(x, y)] << 1); }
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  f[u][0] = fa;
  for (int i = 1; (1 << i) < dep[u]; ++i) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int i = T.head[u], v = T.to[i]; i; i = T.nxt[i], v = T.to[i]) {
    if (v ^ fa) dst[v] = dst[u] + T.w[i], dfs(v, u);
  }
}
vector<int> wt;
void df(int u, int fa) {
  vis[u] = true;
  for (int i = G.head[u], v = G.to[i]; i; i = G.nxt[i], v = G.to[i]) {
    if (v ^ fa) {
      if (vis[v])
        wt.push_back(u);
      else {
        T.adde(u, v, G.w[i]);
        df(v, u);
      }
    }
  }
}
signed main() {
  read(n);
  read(m);
  for (int i = (1), _ = (m); i <= _; ++i) {
    int x, y, z;
    read(x);
    read(y);
    read(z);
    G.adde(x, y, z);
    G.adde(y, x, z);
  }
  df(1, 0);
  dfs(1, 0);
  memset(dis, 0x3f, sizeof(dis));
  for (int i = (0), _ = (wt.size() - 1); i <= _; ++i) dij(dis[i], wt[i]);
  int q;
  read(q);
  while (q--) {
    int x, y;
    read(x);
    read(y);
    long long ans = dist(x, y);
    for (int i = (0), _ = (wt.size() - 1); i <= _; ++i)
      ans = min(ans, dis[i][x] + dis[i][y]);
    cout << ans << endl;
  }
  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;
template <typename T>
void read(T &x) {
  x = 0;
  char ch = getchar();
  bool f = 0;
  while (ch < '0' || ch > '9') {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9')
    x = (x << 1) + (x << 3) + ch - '0', ch = getchar();
  x = f ? -x : x;
}
const int N = 1e5 + 7, M = 2e5 + 7, K = 45;
int n, m;
struct Graph {
  int head[N], to[M], nxt[M], w[M], tot;
  void adde(int u, int v, int l) {
    to[++tot] = v;
    nxt[tot] = head[u];
    head[u] = tot;
    w[tot] = l;
  }
} G, T;
long long dis[K][N];
bool vis[N];
void dij(long long *dis, int S) {
  for (int i = (1), _ = (n); i <= _; ++i) vis[i] = false;
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      q;
  dis[S] = 0;
  q.push(make_pair(0, S));
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (vis[u]) continue;
    vis[u] = true;
    for (int i = G.head[u], v = G.to[i]; i; i = G.nxt[i], v = G.to[i]) {
      if (dis[v] > dis[u] + G.w[i]) {
        dis[v] = dis[u] + G.w[i];
        q.push(make_pair(dis[v], v));
      }
    }
  }
}
int dep[N], f[N][17];
long long dst[N];
int lca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  for (int len = dep[x] - dep[y], i = 0; len; len >>= 1, ++i) {
    if (len & 1) x = f[x][i];
  }
  if (!(x ^ y)) return x;
  for (int i = (16), _ = (0); i >= _; --i) {
    if (f[x][i] ^ f[y][i]) x = f[x][i], y = f[y][i];
  }
  return f[x][0];
}
long long dist(int x, int y) { return dst[x] + dst[y] - (dst[lca(x, y)] << 1); }
void dfs(int u, int fa) {
  dep[u] = dep[fa] + 1;
  f[u][0] = fa;
  for (int i = 1; (1 << i) < dep[u]; ++i) f[u][i] = f[f[u][i - 1]][i - 1];
  for (int i = T.head[u], v = T.to[i]; i; i = T.nxt[i], v = T.to[i]) {
    if (v ^ fa) dst[v] = dst[u] + T.w[i], dfs(v, u);
  }
}
vector<int> wt;
void df(int u, int fa) {
  vis[u] = true;
  for (int i = G.head[u], v = G.to[i]; i; i = G.nxt[i], v = G.to[i]) {
    if (v ^ fa) {
      if (vis[v])
        wt.push_back(u);
      else {
        T.adde(u, v, G.w[i]);
        df(v, u);
      }
    }
  }
}
signed main() {
  read(n);
  read(m);
  for (int i = (1), _ = (m); i <= _; ++i) {
    int x, y, z;
    read(x);
    read(y);
    read(z);
    G.adde(x, y, z);
    G.adde(y, x, z);
  }
  df(1, 0);
  dfs(1, 0);
  memset(dis, 0x3f, sizeof(dis));
  for (int i = (0), _ = (wt.size() - 1); i <= _; ++i) dij(dis[i], wt[i]);
  int q;
  read(q);
  while (q--) {
    int x, y;
    read(x);
    read(y);
    long long ans = dist(x, y);
    for (int i = (0), _ = (wt.size() - 1); i <= _; ++i)
      ans = min(ans, dis[i][x] + dis[i][y]);
    cout << ans << endl;
  }
  return 0;
}

```