#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 10;
const int maxm = 2e5 + 10;
const int max0 = 20;
const int maxp = 30;
const long long INF = 1e17 + 10;
struct node {
  int dep;
  int num;
  long long dis;
};
bool operator<(const node &v, const node &u) { return v.dis < u.dis; }
struct edge {
  int from;
  int to;
  int nxt;
  long long val;
};
set<int> s;
int headg[maxn];
int headt[maxn];
long long dis[maxp][maxn];
edge et[maxm];
int p[maxn];
int fa[maxn][max0];
edge eg[maxm];
int dep[maxn];
bool vis[maxn];
long long dist[maxn];
int n, m;
int cntt = 0;
int cntg = 0;
int num = 0;
int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
void addg(int x, int y, long long val) {
  eg[cntg].from = x;
  eg[cntg].to = y;
  eg[cntg].val = val;
  eg[cntg].nxt = headg[x];
  headg[x] = cntg++;
}
void addt(int x, int y, long long val) {
  et[cntt].from = x;
  et[cntt].to = y;
  et[cntt].val = val;
  et[cntt].nxt = headt[x];
  headt[x] = cntt++;
}
void dij(int x, int id) {
  priority_queue<node> pque;
  for (int i = 0; i < maxn; i++) {
    dis[id][i] = INF;
  }
  dis[id][x] = 0;
  node k;
  k.dis = 0;
  k.num = x;
  pque.push(k);
  while (!pque.empty()) {
    k = pque.top();
    pque.pop();
    for (int i = headg[k.num]; i != -1; i = eg[i].nxt) {
      int to = eg[i].to;
      if (dis[id][to] > dis[id][k.num] + eg[i].val) {
        dis[id][to] = dis[id][k.num] + eg[i].val;
        node r;
        r.num = to;
        r.dis = dis[id][to];
        pque.push(r);
      }
    }
  }
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int sub = dep[u] - dep[v];
  for (int i = max0 - 1; i >= 0; i--) {
    if (sub & (1 << i)) {
      u = fa[u][i];
    }
  }
  if (u == v) return u;
  for (int i = max0 - 1; i >= 0; i--) {
    if (fa[u][i] != fa[v][i]) {
      u = fa[u][i];
      v = fa[v][i];
    }
  }
  return fa[u][0];
}
long long query(int u, int v) {
  int lc = lca(u, v);
  long long ans = dist[u] + dist[v] - dist[lc] * 2;
  for (int i = 0; i < num; i++) {
    ans = min(ans, dis[i][u] + dis[i][v]);
  }
  return ans;
}
void bfs() {
  queue<pair<node, int> > que;
  node k;
  k.dep = 0;
  k.dis = 0;
  k.num = 0;
  que.push(make_pair(k, 0));
  while (!que.empty()) {
    pair<node, int> k = que.front();
    que.pop();
    int nump = k.first.num;
    int fp = k.second;
    long long disp = k.first.dis;
    int depp = k.first.dep;
    vis[nump] = true;
    dist[nump] = disp;
    dep[nump] = depp;
    for (int i = headt[nump]; i != -1; i = et[i].nxt) {
      if (vis[et[i].to]) continue;
      fa[et[i].to][0] = fp;
      node r;
      r.dis = disp + et[i].val;
      r.num = et[i].to;
      r.dep = depp + 1;
      que.push(make_pair(r, et[i].to));
    }
  }
}
void get_fa() {
  for (int i = 1; i < max0; i++) {
    for (int j = 0; j < maxn; j++) {
      fa[j][i] = fa[fa[j][i - 1]][i - 1];
    }
  }
}
void work() {
  memset(headg, -1, sizeof(headg));
  memset(headt, -1, sizeof(headt));
  for (int i = 0; i < maxn; i++) {
    p[i] = i;
  }
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    long long val;
    scanf("%d%d%lld", &x, &y, &val);
    x--;
    y--;
    addg(x, y, val);
    addg(y, x, val);
    int a = find(x);
    int b = find(y);
    if (a != b) {
      p[a] = b;
      addt(x, y, val);
      addt(y, x, val);
    } else {
      s.insert(x);
    }
  }
  set<int>::iterator it = s.begin(), en = s.end();
  while (it != en) {
    dij((*it), num);
    it++;
    num++;
  }
  bfs();
  get_fa();
  int _;
  scanf("%d", &_);
  while (_--) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    printf("%lld\n", query(u, v));
  }
}
int main() {
  work();
  return 0;
}

### Prompt

In Cpp, your task is to solve 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 = 1e5 + 10;
const int maxm = 2e5 + 10;
const int max0 = 20;
const int maxp = 30;
const long long INF = 1e17 + 10;
struct node {
  int dep;
  int num;
  long long dis;
};
bool operator<(const node &v, const node &u) { return v.dis < u.dis; }
struct edge {
  int from;
  int to;
  int nxt;
  long long val;
};
set<int> s;
int headg[maxn];
int headt[maxn];
long long dis[maxp][maxn];
edge et[maxm];
int p[maxn];
int fa[maxn][max0];
edge eg[maxm];
int dep[maxn];
bool vis[maxn];
long long dist[maxn];
int n, m;
int cntt = 0;
int cntg = 0;
int num = 0;
int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); }
void addg(int x, int y, long long val) {
  eg[cntg].from = x;
  eg[cntg].to = y;
  eg[cntg].val = val;
  eg[cntg].nxt = headg[x];
  headg[x] = cntg++;
}
void addt(int x, int y, long long val) {
  et[cntt].from = x;
  et[cntt].to = y;
  et[cntt].val = val;
  et[cntt].nxt = headt[x];
  headt[x] = cntt++;
}
void dij(int x, int id) {
  priority_queue<node> pque;
  for (int i = 0; i < maxn; i++) {
    dis[id][i] = INF;
  }
  dis[id][x] = 0;
  node k;
  k.dis = 0;
  k.num = x;
  pque.push(k);
  while (!pque.empty()) {
    k = pque.top();
    pque.pop();
    for (int i = headg[k.num]; i != -1; i = eg[i].nxt) {
      int to = eg[i].to;
      if (dis[id][to] > dis[id][k.num] + eg[i].val) {
        dis[id][to] = dis[id][k.num] + eg[i].val;
        node r;
        r.num = to;
        r.dis = dis[id][to];
        pque.push(r);
      }
    }
  }
}
int lca(int u, int v) {
  if (dep[u] < dep[v]) swap(u, v);
  int sub = dep[u] - dep[v];
  for (int i = max0 - 1; i >= 0; i--) {
    if (sub & (1 << i)) {
      u = fa[u][i];
    }
  }
  if (u == v) return u;
  for (int i = max0 - 1; i >= 0; i--) {
    if (fa[u][i] != fa[v][i]) {
      u = fa[u][i];
      v = fa[v][i];
    }
  }
  return fa[u][0];
}
long long query(int u, int v) {
  int lc = lca(u, v);
  long long ans = dist[u] + dist[v] - dist[lc] * 2;
  for (int i = 0; i < num; i++) {
    ans = min(ans, dis[i][u] + dis[i][v]);
  }
  return ans;
}
void bfs() {
  queue<pair<node, int> > que;
  node k;
  k.dep = 0;
  k.dis = 0;
  k.num = 0;
  que.push(make_pair(k, 0));
  while (!que.empty()) {
    pair<node, int> k = que.front();
    que.pop();
    int nump = k.first.num;
    int fp = k.second;
    long long disp = k.first.dis;
    int depp = k.first.dep;
    vis[nump] = true;
    dist[nump] = disp;
    dep[nump] = depp;
    for (int i = headt[nump]; i != -1; i = et[i].nxt) {
      if (vis[et[i].to]) continue;
      fa[et[i].to][0] = fp;
      node r;
      r.dis = disp + et[i].val;
      r.num = et[i].to;
      r.dep = depp + 1;
      que.push(make_pair(r, et[i].to));
    }
  }
}
void get_fa() {
  for (int i = 1; i < max0; i++) {
    for (int j = 0; j < maxn; j++) {
      fa[j][i] = fa[fa[j][i - 1]][i - 1];
    }
  }
}
void work() {
  memset(headg, -1, sizeof(headg));
  memset(headt, -1, sizeof(headt));
  for (int i = 0; i < maxn; i++) {
    p[i] = i;
  }
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++) {
    int x, y;
    long long val;
    scanf("%d%d%lld", &x, &y, &val);
    x--;
    y--;
    addg(x, y, val);
    addg(y, x, val);
    int a = find(x);
    int b = find(y);
    if (a != b) {
      p[a] = b;
      addt(x, y, val);
      addt(y, x, val);
    } else {
      s.insert(x);
    }
  }
  set<int>::iterator it = s.begin(), en = s.end();
  while (it != en) {
    dij((*it), num);
    it++;
    num++;
  }
  bfs();
  get_fa();
  int _;
  scanf("%d", &_);
  while (_--) {
    int u, v;
    scanf("%d%d", &u, &v);
    u--;
    v--;
    printf("%lld\n", query(u, v));
  }
}
int main() {
  work();
  return 0;
}

```