#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 4, M = N + 20;
inline int Get() {
  register char c = getchar();
  register int t = 0, f = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') t = (t << 3) + (t << 1) + c - '0', c = getchar();
  return f ? -t : t;
}
bool usd[N];
int fa[N][20], prt[N], d[N];
int h[N], h_[N];
int n, m, Q, cnt = 0, cnt_ = 0;
long long len[N], dis[44][N];
vector<int> ve;
struct Edge {
  int to, nxt, v;
} edge[M << 1], edge_[M << 1];
inline void Add(int x, int y, int z) {
  edge[++cnt] = (Edge){y, h[x], z};
  h[x] = cnt;
}
inline void Add_(int x, int y, int z) {
  edge_[++cnt_] = (Edge){y, h_[x], z};
  h_[x] = cnt_;
}
void Dfs(int u, long long Len) {
  d[u] = d[fa[u][0]] + 1;
  len[u] = Len;
  for (int i = h_[u]; i; i = edge_[i].nxt) {
    int v = edge_[i].to;
    if (v == fa[u][0]) continue;
    fa[v][0] = u;
    Dfs(v, Len + edge_[i].v);
  }
}
inline int Find(int x) { return prt[x] == x ? x : prt[x] = Find(prt[x]); }
inline int Lca(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  for (int i = (19); i >= (0); --i)
    if (d[fa[u][i]] >= d[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = (19); i >= (0); --i)
    if (fa[u][i] ^ fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
bool vis[N];
inline void SPFA(int x) {
  queue<int> Q;
  memset(dis[x], 0x3f, sizeof(dis[x]));
  dis[x][ve[x]] = 0;
  Q.push(ve[x]);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    vis[u] = 0;
    for (int i = h[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      if (dis[x][v] > dis[x][u] + edge[i].v) {
        dis[x][v] = dis[x][u] + edge[i].v;
        if (!vis[v]) vis[v] = 1, Q.push(v);
      }
    }
  }
}
int main() {
  int x, y, z;
  n = Get(), m = Get();
  for (int i = (1); i <= (n); ++i) prt[i] = i;
  for (int i = (1); i <= (m); ++i) {
    x = Get(), y = Get(), z = Get();
    Add(x, y, z);
    Add(y, x, z);
    int r1 = Find(x), r2 = Find(y);
    if (r1 ^ r2) {
      prt[r1] = r2;
      Add_(x, y, z);
      Add_(y, x, z);
    } else {
      if (!usd[x]) ve.push_back(x), usd[x] = 1;
      if (!usd[y]) ve.push_back(y), usd[y] = 1;
    }
  }
  Dfs(1, 0ll);
  for (int j = (1); j <= (18); ++j)
    for (int i = (1); i <= (n); ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  for (int i = 0, lim = ve.size(); i < lim; ++i) SPFA(i);
  Q = Get();
  while (Q--) {
    x = Get(), y = Get();
    int p = Lca(x, y);
    long long ans = len[x] + len[y] - (len[p] << 1);
    for (int i = 0, lim = ve.size(); i < lim; ++i) {
      (ans) = min((ans), (dis[i][x] + dis[i][y]));
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

### Prompt

Your challenge is to write 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 N = 1e5 + 4, M = N + 20;
inline int Get() {
  register char c = getchar();
  register int t = 0, f = 0;
  while (c < '0' || c > '9') {
    if (c == '-') f = 1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') t = (t << 3) + (t << 1) + c - '0', c = getchar();
  return f ? -t : t;
}
bool usd[N];
int fa[N][20], prt[N], d[N];
int h[N], h_[N];
int n, m, Q, cnt = 0, cnt_ = 0;
long long len[N], dis[44][N];
vector<int> ve;
struct Edge {
  int to, nxt, v;
} edge[M << 1], edge_[M << 1];
inline void Add(int x, int y, int z) {
  edge[++cnt] = (Edge){y, h[x], z};
  h[x] = cnt;
}
inline void Add_(int x, int y, int z) {
  edge_[++cnt_] = (Edge){y, h_[x], z};
  h_[x] = cnt_;
}
void Dfs(int u, long long Len) {
  d[u] = d[fa[u][0]] + 1;
  len[u] = Len;
  for (int i = h_[u]; i; i = edge_[i].nxt) {
    int v = edge_[i].to;
    if (v == fa[u][0]) continue;
    fa[v][0] = u;
    Dfs(v, Len + edge_[i].v);
  }
}
inline int Find(int x) { return prt[x] == x ? x : prt[x] = Find(prt[x]); }
inline int Lca(int u, int v) {
  if (d[u] < d[v]) swap(u, v);
  for (int i = (19); i >= (0); --i)
    if (d[fa[u][i]] >= d[v]) u = fa[u][i];
  if (u == v) return u;
  for (int i = (19); i >= (0); --i)
    if (fa[u][i] ^ fa[v][i]) u = fa[u][i], v = fa[v][i];
  return fa[u][0];
}
bool vis[N];
inline void SPFA(int x) {
  queue<int> Q;
  memset(dis[x], 0x3f, sizeof(dis[x]));
  dis[x][ve[x]] = 0;
  Q.push(ve[x]);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    vis[u] = 0;
    for (int i = h[u]; i; i = edge[i].nxt) {
      int v = edge[i].to;
      if (dis[x][v] > dis[x][u] + edge[i].v) {
        dis[x][v] = dis[x][u] + edge[i].v;
        if (!vis[v]) vis[v] = 1, Q.push(v);
      }
    }
  }
}
int main() {
  int x, y, z;
  n = Get(), m = Get();
  for (int i = (1); i <= (n); ++i) prt[i] = i;
  for (int i = (1); i <= (m); ++i) {
    x = Get(), y = Get(), z = Get();
    Add(x, y, z);
    Add(y, x, z);
    int r1 = Find(x), r2 = Find(y);
    if (r1 ^ r2) {
      prt[r1] = r2;
      Add_(x, y, z);
      Add_(y, x, z);
    } else {
      if (!usd[x]) ve.push_back(x), usd[x] = 1;
      if (!usd[y]) ve.push_back(y), usd[y] = 1;
    }
  }
  Dfs(1, 0ll);
  for (int j = (1); j <= (18); ++j)
    for (int i = (1); i <= (n); ++i) fa[i][j] = fa[fa[i][j - 1]][j - 1];
  for (int i = 0, lim = ve.size(); i < lim; ++i) SPFA(i);
  Q = Get();
  while (Q--) {
    x = Get(), y = Get();
    int p = Lca(x, y);
    long long ans = len[x] + len[y] - (len[p] << 1);
    for (int i = 0, lim = ve.size(); i < lim; ++i) {
      (ans) = min((ans), (dis[i][x] + dis[i][y]));
    }
    printf("%I64d\n", ans);
  }
  return 0;
}

```