#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) { return n | (1LL << i); }
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long &a) {
  a %= 1000000007;
  (a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline long long modPow(long long b, long long p) {
  long long r = 1;
  while (p) {
    if (p & 1) r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline long long modInverse(long long a) {
  normal(a);
  return modPow(a, 1000000007 - 2);
}
inline long long modDiv(long long a, long long b) {
  return modMul(a, modInverse(b));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, edge[100025], m, qu, q1[100025], q2[100025], use[100025], par[100025],
    lvl[100025], sp[100025][20];
long long ans[100025], dist[100025];
bool flag[100025];
vector<int> vt[100025], wt[100025], num[100025], in;
struct data {
  long long node, val;
  bool operator<(const data &p) const { return val > p.val; }
};
priority_queue<data> q;
void dijkstra(int now) {
  data tmp;
  tmp.node = now;
  tmp.val = 0;
  q.push(tmp);
  lvl[now] = 0;
  for (int i = 1; i <= n; i++) dist[i] = 2e18;
  dist[now] = 0;
  use[now] = 0;
  par[now] = -1;
  while (!q.empty()) {
    tmp = q.top();
    q.pop();
    int u = tmp.node;
    long long z = tmp.val;
    for (int i = 0; i < vt[u].size(); i++) {
      int v = vt[u][i];
      long long zz = wt[u][i];
      if ((z + zz) < dist[v]) {
        dist[v] = z + zz;
        use[v] = num[u][i];
        par[v] = u;
        lvl[v] = lvl[u] + 1;
        data tpp;
        tpp.node = v;
        tpp.val = z + zz;
        q.push(tpp);
      }
    }
  }
}
void init() {
  for (int i = 1; i <= n; i++) sp[i][0] = par[i];
  for (int j = 1; j <= log2(n) + 1; j++) {
    for (int i = 1; i <= n; i++) {
      if (sp[i][j - 1] != -1) sp[i][j] = sp[sp[i][j - 1]][j - 1];
    }
  }
}
int lca(int x, int y) {
  if (lvl[x] < lvl[y]) swap(x, y);
  if (lvl[x] > lvl[y]) {
    for (int i = log2(n) + 1, tmp; i >= 0; i--) {
      tmp = (1 << i);
      if (lvl[x] - tmp >= lvl[y]) x = sp[x][i];
    }
  }
  if (x == y) return x;
  for (int i = log2(n) + 1; i >= 0; i--) {
    if (sp[x][i] != -1 && sp[x][i] != sp[y][i]) x = sp[x][i], y = sp[y][i];
  }
  return sp[x][0];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1, x, y, z; i <= m; i++) {
    cin >> x >> y >> z;
    vt[x].push_back(y);
    vt[y].push_back(x);
    wt[x].push_back(z);
    wt[y].push_back(z);
    num[x].push_back(i);
    num[y].push_back(i);
    edge[i] = x;
  }
  dijkstra(1);
  for (int i = 2; i <= n; i++) {
    flag[use[i]] = true;
  }
  in.push_back(1);
  for (int i = 1; i <= m; i++) {
    if (!flag[i]) in.push_back(edge[i]);
  }
  cin >> qu;
  for (int i = 1; i <= qu; i++) cin >> q1[i] >> q2[i];
  for (int i = 1; i <= qu; i++) ans[i] = 2e18;
  for (int i = 0; i < in.size(); i++) {
    memset(sp, -1, sizeof(sp));
    memset(par, -1, sizeof(par));
    memset(lvl, -1, sizeof(lvl));
    dijkstra(in[i]);
    init();
    for (int j = 1, x, y, z; j <= qu; j++) {
      x = q1[j];
      y = q2[j];
      z = lca(x, y);
      long long cur = dist[x] + dist[y] - dist[z] - dist[z];
      ans[j] = min(ans[j], cur);
    }
  }
  for (int i = 1; i <= qu; i++) cout << ans[i] << '\n';
  return 0;
}

### Prompt

Your task is to create 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 double PI = acos(-1);
const double eps = 1e-9;
const int inf = 2000000000;
const long long infLL = 9000000000000000000;
inline bool checkBit(long long n, int i) { return n & (1LL << i); }
inline long long setBit(long long n, int i) { return n | (1LL << i); }
inline long long resetBit(long long n, int i) { return n & (~(1LL << i)); }
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
inline bool isLeapYear(long long year) {
  return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0);
}
inline void normal(long long &a) {
  a %= 1000000007;
  (a < 0) && (a += 1000000007);
}
inline long long modMul(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a * b) % 1000000007;
}
inline long long modAdd(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  return (a + b) % 1000000007;
}
inline long long modSub(long long a, long long b) {
  a %= 1000000007, b %= 1000000007;
  normal(a), normal(b);
  a -= b;
  normal(a);
  return a;
}
inline long long modPow(long long b, long long p) {
  long long r = 1;
  while (p) {
    if (p & 1) r = modMul(r, b);
    b = modMul(b, b);
    p >>= 1;
  }
  return r;
}
inline long long modInverse(long long a) {
  normal(a);
  return modPow(a, 1000000007 - 2);
}
inline long long modDiv(long long a, long long b) {
  return modMul(a, modInverse(b));
}
int dx[] = {0, 0, +1, -1};
int dy[] = {+1, -1, 0, 0};
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
int n, edge[100025], m, qu, q1[100025], q2[100025], use[100025], par[100025],
    lvl[100025], sp[100025][20];
long long ans[100025], dist[100025];
bool flag[100025];
vector<int> vt[100025], wt[100025], num[100025], in;
struct data {
  long long node, val;
  bool operator<(const data &p) const { return val > p.val; }
};
priority_queue<data> q;
void dijkstra(int now) {
  data tmp;
  tmp.node = now;
  tmp.val = 0;
  q.push(tmp);
  lvl[now] = 0;
  for (int i = 1; i <= n; i++) dist[i] = 2e18;
  dist[now] = 0;
  use[now] = 0;
  par[now] = -1;
  while (!q.empty()) {
    tmp = q.top();
    q.pop();
    int u = tmp.node;
    long long z = tmp.val;
    for (int i = 0; i < vt[u].size(); i++) {
      int v = vt[u][i];
      long long zz = wt[u][i];
      if ((z + zz) < dist[v]) {
        dist[v] = z + zz;
        use[v] = num[u][i];
        par[v] = u;
        lvl[v] = lvl[u] + 1;
        data tpp;
        tpp.node = v;
        tpp.val = z + zz;
        q.push(tpp);
      }
    }
  }
}
void init() {
  for (int i = 1; i <= n; i++) sp[i][0] = par[i];
  for (int j = 1; j <= log2(n) + 1; j++) {
    for (int i = 1; i <= n; i++) {
      if (sp[i][j - 1] != -1) sp[i][j] = sp[sp[i][j - 1]][j - 1];
    }
  }
}
int lca(int x, int y) {
  if (lvl[x] < lvl[y]) swap(x, y);
  if (lvl[x] > lvl[y]) {
    for (int i = log2(n) + 1, tmp; i >= 0; i--) {
      tmp = (1 << i);
      if (lvl[x] - tmp >= lvl[y]) x = sp[x][i];
    }
  }
  if (x == y) return x;
  for (int i = log2(n) + 1; i >= 0; i--) {
    if (sp[x][i] != -1 && sp[x][i] != sp[y][i]) x = sp[x][i], y = sp[y][i];
  }
  return sp[x][0];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> n >> m;
  for (int i = 1, x, y, z; i <= m; i++) {
    cin >> x >> y >> z;
    vt[x].push_back(y);
    vt[y].push_back(x);
    wt[x].push_back(z);
    wt[y].push_back(z);
    num[x].push_back(i);
    num[y].push_back(i);
    edge[i] = x;
  }
  dijkstra(1);
  for (int i = 2; i <= n; i++) {
    flag[use[i]] = true;
  }
  in.push_back(1);
  for (int i = 1; i <= m; i++) {
    if (!flag[i]) in.push_back(edge[i]);
  }
  cin >> qu;
  for (int i = 1; i <= qu; i++) cin >> q1[i] >> q2[i];
  for (int i = 1; i <= qu; i++) ans[i] = 2e18;
  for (int i = 0; i < in.size(); i++) {
    memset(sp, -1, sizeof(sp));
    memset(par, -1, sizeof(par));
    memset(lvl, -1, sizeof(lvl));
    dijkstra(in[i]);
    init();
    for (int j = 1, x, y, z; j <= qu; j++) {
      x = q1[j];
      y = q2[j];
      z = lca(x, y);
      long long cur = dist[x] + dist[y] - dist[z] - dist[z];
      ans[j] = min(ans[j], cur);
    }
  }
  for (int i = 1; i <= qu; i++) cout << ans[i] << '\n';
  return 0;
}

```