#include <bits/stdc++.h>
using namespace std;
inline char get() {
  const int TOP = 1000000;
  static char T[TOP], *x = T, *y = T;
  return x == y && (y = (x = T) + fread(T, 1, TOP, stdin), x == y) ? EOF : *x++;
}
inline int read() {
  register int num;
  int sign = 1;
  register char c;
  while (((c = get()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? num = sign = 0 : num = c - 48;
  while ((c = get()) >= '0' && c <= '9')
    num = (num << 3) + (num << 1) + (c - 48);
  return sign ? num : -num;
}
template <class Node>
inline Node max(Node &x, Node &y) {
  return x > y ? x : y;
}
template <class Node>
inline Node min(Node &x, Node &y) {
  return x < y ? x : y;
}
struct node {
  int to, data;
  node(int T, int D) : to(T), data(D){};
};
vector<node> G1[300005], G2[300005];
int n, m, x, y, z, len, qu;
int vis[300005], use[300005], father[300005], Q[300005];
int fa[300005], size[300005], dep[300005], top[300005], son[300005];
long long dis[45][300005], dist[300005];
void spfa(int u, int id) {
  int v;
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  dis[id][u] = 0;
  q.push(u);
  while (!q.empty()) {
    vis[u = q.front()] = 0;
    q.pop();
    for (int i = 0; i < G1[u].size(); ++i) {
      v = G1[u][i].to;
      if (dis[id][u] + G1[u][i].data < dis[id][v]) {
        dis[id][v] = dis[id][u] + G1[u][i].data;
        if (!vis[v]) {
          vis[v] = 1;
          q.push(v);
        }
      }
    }
  }
}
int find(int x) { return father[x] == x ? x : father[x] = find(father[x]); }
inline bool merge(int u, int v) {
  int x = find(u), y = find(v);
  if (x != y) {
    father[y] = x;
    return 1;
  }
  return 0;
}
void dfs1(int u) {
  int v;
  size[u] = 1;
  for (int i = 0; i < G2[u].size(); ++i) {
    v = G2[u][i].to;
    if (v == fa[u]) continue;
    fa[v] = u;
    dep[v] = dep[u] + 1;
    dist[v] = dist[u] + G2[u][i].data;
    dfs1(v);
    size[u] += size[v];
    if (son[u] == -1 || size[son[u]] < size[v]) son[u] = v;
  }
}
void dfs2(int u, int tp) {
  int v;
  top[u] = tp;
  if (son[u] != -1) dfs2(son[u], tp);
  for (int i = 0; i < G2[u].size(); ++i) {
    v = G2[u][i].to;
    if (v == fa[u] || son[u] == v) continue;
    dfs2(v, v);
  }
}
int LCA(int x, int y) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    x = fa[top[x]];
  }
  return dep[x] < dep[y] ? x : y;
}
int main() {
  memset(son, -1, sizeof(son));
  memset(dis, 0x3f, sizeof(dis));
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i) father[i] = i;
  for (int i = 1; i <= m; ++i) {
    x = read();
    y = read();
    z = read();
    G1[x].push_back(node(y, z));
    G1[y].push_back(node(x, z));
    if (merge(x, y)) {
      G2[x].push_back(node(y, z));
      G2[y].push_back(node(x, z));
    } else
      Q[++len] = x, Q[++len] = y;
  }
  qu = read();
  sort(Q + 1, Q + 1 + len);
  len = unique(Q + 1, Q + 1 + len) - Q - 1;
  for (int i = 1; i <= len; ++i) spfa(Q[i], i);
  dfs1(1);
  dfs2(1, 1);
  while (qu--) {
    long long ans;
    x = read();
    y = read();
    ans = dist[x] + dist[y] - 2 * dist[LCA(x, y)];
    for (int i = 1; i <= len; ++i) ans = min(ans, dis[i][x] + dis[i][y]);
    printf("%I64d\n", ans);
  }
}

### 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;
inline char get() {
  const int TOP = 1000000;
  static char T[TOP], *x = T, *y = T;
  return x == y && (y = (x = T) + fread(T, 1, TOP, stdin), x == y) ? EOF : *x++;
}
inline int read() {
  register int num;
  int sign = 1;
  register char c;
  while (((c = get()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? num = sign = 0 : num = c - 48;
  while ((c = get()) >= '0' && c <= '9')
    num = (num << 3) + (num << 1) + (c - 48);
  return sign ? num : -num;
}
template <class Node>
inline Node max(Node &x, Node &y) {
  return x > y ? x : y;
}
template <class Node>
inline Node min(Node &x, Node &y) {
  return x < y ? x : y;
}
struct node {
  int to, data;
  node(int T, int D) : to(T), data(D){};
};
vector<node> G1[300005], G2[300005];
int n, m, x, y, z, len, qu;
int vis[300005], use[300005], father[300005], Q[300005];
int fa[300005], size[300005], dep[300005], top[300005], son[300005];
long long dis[45][300005], dist[300005];
void spfa(int u, int id) {
  int v;
  queue<int> q;
  memset(vis, 0, sizeof(vis));
  dis[id][u] = 0;
  q.push(u);
  while (!q.empty()) {
    vis[u = q.front()] = 0;
    q.pop();
    for (int i = 0; i < G1[u].size(); ++i) {
      v = G1[u][i].to;
      if (dis[id][u] + G1[u][i].data < dis[id][v]) {
        dis[id][v] = dis[id][u] + G1[u][i].data;
        if (!vis[v]) {
          vis[v] = 1;
          q.push(v);
        }
      }
    }
  }
}
int find(int x) { return father[x] == x ? x : father[x] = find(father[x]); }
inline bool merge(int u, int v) {
  int x = find(u), y = find(v);
  if (x != y) {
    father[y] = x;
    return 1;
  }
  return 0;
}
void dfs1(int u) {
  int v;
  size[u] = 1;
  for (int i = 0; i < G2[u].size(); ++i) {
    v = G2[u][i].to;
    if (v == fa[u]) continue;
    fa[v] = u;
    dep[v] = dep[u] + 1;
    dist[v] = dist[u] + G2[u][i].data;
    dfs1(v);
    size[u] += size[v];
    if (son[u] == -1 || size[son[u]] < size[v]) son[u] = v;
  }
}
void dfs2(int u, int tp) {
  int v;
  top[u] = tp;
  if (son[u] != -1) dfs2(son[u], tp);
  for (int i = 0; i < G2[u].size(); ++i) {
    v = G2[u][i].to;
    if (v == fa[u] || son[u] == v) continue;
    dfs2(v, v);
  }
}
int LCA(int x, int y) {
  while (top[x] != top[y]) {
    if (dep[top[x]] < dep[top[y]]) swap(x, y);
    x = fa[top[x]];
  }
  return dep[x] < dep[y] ? x : y;
}
int main() {
  memset(son, -1, sizeof(son));
  memset(dis, 0x3f, sizeof(dis));
  n = read();
  m = read();
  for (int i = 1; i <= n; ++i) father[i] = i;
  for (int i = 1; i <= m; ++i) {
    x = read();
    y = read();
    z = read();
    G1[x].push_back(node(y, z));
    G1[y].push_back(node(x, z));
    if (merge(x, y)) {
      G2[x].push_back(node(y, z));
      G2[y].push_back(node(x, z));
    } else
      Q[++len] = x, Q[++len] = y;
  }
  qu = read();
  sort(Q + 1, Q + 1 + len);
  len = unique(Q + 1, Q + 1 + len) - Q - 1;
  for (int i = 1; i <= len; ++i) spfa(Q[i], i);
  dfs1(1);
  dfs2(1, 1);
  while (qu--) {
    long long ans;
    x = read();
    y = read();
    ans = dist[x] + dist[y] - 2 * dist[LCA(x, y)];
    for (int i = 1; i <= len; ++i) ans = min(ans, dis[i][x] + dis[i][y]);
    printf("%I64d\n", ans);
  }
}

```