#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int SUM = 0, ff = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    SUM = (SUM << 3) + (SUM << 1) + (ch ^ 48);
    ch = getchar();
  }
  return SUM * ff;
}
const int N = 100010;
struct ALL {
  int u, v;
  long long w;
  inline friend bool operator<(ALL a1, ALL a2) { return a1.w < a2.w; }
} g[N << 1];
struct edge {
  int nxt, to;
  long long w;
} e[N << 2];
int h[N], cnte = 1;
inline void addedge(int u, int v, long long w) {
  e[++cnte] = (edge){h[u], v, w}, h[u] = cnte;
}
int n, m, Q;
inline void init() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++)
    g[i].u = read(), g[i].v = read(), g[i].w = read();
}
bool use[N];
int dsu[N];
inline int find(int x) { return dsu[x] == x ? x : dsu[x] = find(dsu[x]); }
inline void Kruskal() {
  sort(g + 1, g + m + 1);
  for (int i = 1; i <= n; i++) dsu[i] = i;
  for (int i = 1; i <= m; i++) {
    int fu = find(g[i].u), fv = find(g[i].v);
    if (fu != fv) {
      use[i] = 1;
      addedge(g[i].u, g[i].v, g[i].w);
      addedge(g[i].v, g[i].u, g[i].w);
      dsu[fu] = fv;
    }
  }
}
int dep[N], fa[N][18];
long long dist[N];
inline void dfs(int u, int fu) {
  dep[u] = dep[fu] + 1;
  fa[u][0] = fu;
  for (int i = 1; (1 << i) <= dep[u]; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v != fu) {
      dist[v] = dist[u] + e[i].w;
      dfs(v, u);
    }
  }
}
inline int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int l = dep[x] - dep[y];
  for (int i = 17; i >= 0; i--)
    if ((l >> i) & 1) x = fa[x][i];
  if (x == y) return x;
  for (int i = 17; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  return fa[x][0];
}
long long dis[110][N];
bool done[N];
int cnt;
struct point {
  int u;
  long long D;
  inline friend bool operator<(point a1, point a2) { return a1.D > a2.D; }
};
inline void dijkstra(int op, int S) {
  memset(dis[op], 0x7f, sizeof(dis[op]));
  memset(done, 0, sizeof(done));
  priority_queue<point> q;
  dis[op][S] = 0;
  q.push((point){S, 0});
  while (!q.empty()) {
    point now = q.top();
    q.pop();
    int u = now.u;
    if (done[u]) continue;
    done[u] = 1;
    for (int i = h[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (done[v]) continue;
      if (dis[op][v] > dis[op][u] + e[i].w) {
        dis[op][v] = dis[op][u] + e[i].w;
        q.push((point){v, dis[op][v]});
      }
    }
  }
}
inline void work() {
  for (int i = 1; i <= m; i++)
    if (!use[i]) {
      addedge(g[i].u, g[i].v, g[i].w);
      addedge(g[i].v, g[i].u, g[i].w);
    }
  for (int i = 1; i <= m; i++)
    if (!use[i]) {
      dijkstra(++cnt, g[i].u);
      dijkstra(++cnt, g[i].v);
    }
}
inline void solve() {
  Q = read();
  while (Q--) {
    int u = read(), v = read();
    int lca = getlca(u, v);
    long long ans = dist[u] + dist[v] - 2 * dist[lca];
    for (int i = 1; i <= cnt; i++) ans = min(ans, dis[i][u] + dis[i][v]);
    printf("%lld\n", ans);
  }
}
int main() {
  init();
  Kruskal();
  dfs(1, 0);
  work();
  solve();
  return 0;
}

### Prompt

Create a solution in cpp for 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 int read() {
  int SUM = 0, ff = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    SUM = (SUM << 3) + (SUM << 1) + (ch ^ 48);
    ch = getchar();
  }
  return SUM * ff;
}
const int N = 100010;
struct ALL {
  int u, v;
  long long w;
  inline friend bool operator<(ALL a1, ALL a2) { return a1.w < a2.w; }
} g[N << 1];
struct edge {
  int nxt, to;
  long long w;
} e[N << 2];
int h[N], cnte = 1;
inline void addedge(int u, int v, long long w) {
  e[++cnte] = (edge){h[u], v, w}, h[u] = cnte;
}
int n, m, Q;
inline void init() {
  n = read(), m = read();
  for (int i = 1; i <= m; i++)
    g[i].u = read(), g[i].v = read(), g[i].w = read();
}
bool use[N];
int dsu[N];
inline int find(int x) { return dsu[x] == x ? x : dsu[x] = find(dsu[x]); }
inline void Kruskal() {
  sort(g + 1, g + m + 1);
  for (int i = 1; i <= n; i++) dsu[i] = i;
  for (int i = 1; i <= m; i++) {
    int fu = find(g[i].u), fv = find(g[i].v);
    if (fu != fv) {
      use[i] = 1;
      addedge(g[i].u, g[i].v, g[i].w);
      addedge(g[i].v, g[i].u, g[i].w);
      dsu[fu] = fv;
    }
  }
}
int dep[N], fa[N][18];
long long dist[N];
inline void dfs(int u, int fu) {
  dep[u] = dep[fu] + 1;
  fa[u][0] = fu;
  for (int i = 1; (1 << i) <= dep[u]; i++) fa[u][i] = fa[fa[u][i - 1]][i - 1];
  for (int i = h[u]; i; i = e[i].nxt) {
    int v = e[i].to;
    if (v != fu) {
      dist[v] = dist[u] + e[i].w;
      dfs(v, u);
    }
  }
}
inline int getlca(int x, int y) {
  if (dep[x] < dep[y]) swap(x, y);
  int l = dep[x] - dep[y];
  for (int i = 17; i >= 0; i--)
    if ((l >> i) & 1) x = fa[x][i];
  if (x == y) return x;
  for (int i = 17; i >= 0; i--)
    if (fa[x][i] != fa[y][i]) {
      x = fa[x][i];
      y = fa[y][i];
    }
  return fa[x][0];
}
long long dis[110][N];
bool done[N];
int cnt;
struct point {
  int u;
  long long D;
  inline friend bool operator<(point a1, point a2) { return a1.D > a2.D; }
};
inline void dijkstra(int op, int S) {
  memset(dis[op], 0x7f, sizeof(dis[op]));
  memset(done, 0, sizeof(done));
  priority_queue<point> q;
  dis[op][S] = 0;
  q.push((point){S, 0});
  while (!q.empty()) {
    point now = q.top();
    q.pop();
    int u = now.u;
    if (done[u]) continue;
    done[u] = 1;
    for (int i = h[u]; i; i = e[i].nxt) {
      int v = e[i].to;
      if (done[v]) continue;
      if (dis[op][v] > dis[op][u] + e[i].w) {
        dis[op][v] = dis[op][u] + e[i].w;
        q.push((point){v, dis[op][v]});
      }
    }
  }
}
inline void work() {
  for (int i = 1; i <= m; i++)
    if (!use[i]) {
      addedge(g[i].u, g[i].v, g[i].w);
      addedge(g[i].v, g[i].u, g[i].w);
    }
  for (int i = 1; i <= m; i++)
    if (!use[i]) {
      dijkstra(++cnt, g[i].u);
      dijkstra(++cnt, g[i].v);
    }
}
inline void solve() {
  Q = read();
  while (Q--) {
    int u = read(), v = read();
    int lca = getlca(u, v);
    long long ans = dist[u] + dist[v] - 2 * dist[lca];
    for (int i = 1; i <= cnt; i++) ans = min(ans, dis[i][u] + dis[i][v]);
    printf("%lld\n", ans);
  }
}
int main() {
  init();
  Kruskal();
  dfs(1, 0);
  work();
  solve();
  return 0;
}

```