#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18 + 10;
const int inf = 1e9 + 10;
const int N = 1e5 + 10;
struct Dsu {
  vector<int> parent;
  void Build(int n) {
    for (int i = 0; i < n; i++) parent.push_back(i);
  }
  int Root(int now) {
    if (parent[now] == now)
      return now;
    else
      return parent[now] = Root(parent[now]);
  }
  bool Merge(int a, int b) {
    a = Root(a);
    b = Root(b);
    if (a != b) parent[a] = b;
    return a != b;
  }
};
struct osu {
  int a, b, cost;
};
long long pref[N], timer;
int tin[N], tout[N], up[N][18], kek[N];
vector<pair<int, int> > g[N], tree[N];
vector<pair<int, long long> > naruto[N];
bool cmp(pair<int, long long> a, pair<int, long long> b) {
  return (a.first < b.first) or (a.first == b.first and a.second < b.second);
}
void dfs(int now, int from, long long sum) {
  kek[now] = 1;
  pref[now] = sum;
  tin[now] = timer++;
  up[now][0] = from;
  for (int i = 1; i < 17; i++) up[now][i] = up[up[now][i - 1]][i - 1];
  for (auto to : tree[now])
    if (to.first != from) dfs(to.first, now, sum + to.second);
  tout[now] = timer++;
}
bool upper(int a, int b) { return tin[a] <= tin[b] and tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (a == b) return a;
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = 17; i >= 0; i--)
    if (!upper(up[a][i], b)) a = up[a][i];
  return up[a][0];
}
vector<long long> dp;
set<pair<long long, int> > que;
void algos(int n, int now) {
  dp.assign(n, INF);
  dp[now] = 0;
  que.insert(make_pair(0, now));
  while (que.size()) {
    auto it = que.begin();
    int now = it->second;
    for (auto to : g[now])
      if (dp[now] + to.second < dp[to.first]) {
        que.erase(make_pair(dp[to.first], to.first));
        dp[to.first] = dp[now] + to.second;
        que.insert(make_pair(dp[to.first], to.first));
      }
    que.erase(it);
  }
  for (int i = 0; i < n; i++) naruto[now].push_back(make_pair(i, dp[i]));
  sort(naruto[now].begin(), naruto[now].end());
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vector<osu> edge(m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
    edge[i] = {a, b, c};
  }
  Dsu dsu;
  dsu.Build(n);
  vector<int> used(m);
  for (int i = 0; i < m; i++) {
    int a = edge[i].a, b = edge[i].b;
    if (dsu.Merge(a, b)) {
      tree[a].push_back(make_pair(b, edge[i].cost));
      tree[b].push_back(make_pair(a, edge[i].cost));
      used[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (tree[i].size()) {
      dfs(i, i, 0);
      break;
    }
  set<int> _border;
  for (int i = 0; i < m; i++)
    if (!used[i]) {
      int a = edge[i].a;
      if (kek[a]) _border.insert(a);
      a = edge[i].b;
      if (kek[a]) _border.insert(a);
    }
  vector<int> border;
  for (auto i : _border) border.push_back(i);
  set<int> _lol;
  for (int i = 0; i < m; i++)
    if (!used[i]) {
      _lol.insert(edge[i].a);
      _lol.insert(edge[i].b);
    }
  for (auto i : _lol) algos(n, i);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    long long ans = INF;
    if (kek[a] and kek[b]) {
      int c = lca(a, b);
      ans = min(ans, pref[a] - pref[c] + pref[b] - pref[c]);
    }
    auto it = lower_bound(naruto[a].begin(), naruto[a].end(),
                          make_pair(b, -inf), cmp);
    if (it != naruto[a].end() and it->first == b)
      ans = min(ans, 1LL * it->second);
    int flag = 0;
    if (kek[a]) {
      int now = a;
      for (auto v : border) {
        int c = lca(now, v);
        long long res = pref[now] - pref[c] + pref[v] - pref[c];
        auto it1 = lower_bound(naruto[v].begin(), naruto[v].end(),
                               make_pair(b, -INF), cmp);
        if (it1 != naruto[v].end()) {
          res += it1->second;
          ans = min(ans, res);
        }
      }
      flag = 1;
    }
    swap(a, b);
    if (kek[a] and !flag) {
      int now = a;
      for (auto v : border) {
        int c = lca(now, v);
        long long res = pref[now] - pref[c] + pref[v] - pref[c];
        auto it1 = lower_bound(naruto[v].begin(), naruto[v].end(),
                               make_pair(b, -INF), cmp);
        if (it1 != naruto[v].end()) {
          res += it1->second;
          ans = min(ans, res);
        }
      }
    }
    cout << ans << "\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 long long INF = 1e18 + 10;
const int inf = 1e9 + 10;
const int N = 1e5 + 10;
struct Dsu {
  vector<int> parent;
  void Build(int n) {
    for (int i = 0; i < n; i++) parent.push_back(i);
  }
  int Root(int now) {
    if (parent[now] == now)
      return now;
    else
      return parent[now] = Root(parent[now]);
  }
  bool Merge(int a, int b) {
    a = Root(a);
    b = Root(b);
    if (a != b) parent[a] = b;
    return a != b;
  }
};
struct osu {
  int a, b, cost;
};
long long pref[N], timer;
int tin[N], tout[N], up[N][18], kek[N];
vector<pair<int, int> > g[N], tree[N];
vector<pair<int, long long> > naruto[N];
bool cmp(pair<int, long long> a, pair<int, long long> b) {
  return (a.first < b.first) or (a.first == b.first and a.second < b.second);
}
void dfs(int now, int from, long long sum) {
  kek[now] = 1;
  pref[now] = sum;
  tin[now] = timer++;
  up[now][0] = from;
  for (int i = 1; i < 17; i++) up[now][i] = up[up[now][i - 1]][i - 1];
  for (auto to : tree[now])
    if (to.first != from) dfs(to.first, now, sum + to.second);
  tout[now] = timer++;
}
bool upper(int a, int b) { return tin[a] <= tin[b] and tout[a] >= tout[b]; }
int lca(int a, int b) {
  if (a == b) return a;
  if (upper(a, b)) return a;
  if (upper(b, a)) return b;
  for (int i = 17; i >= 0; i--)
    if (!upper(up[a][i], b)) a = up[a][i];
  return up[a][0];
}
vector<long long> dp;
set<pair<long long, int> > que;
void algos(int n, int now) {
  dp.assign(n, INF);
  dp[now] = 0;
  que.insert(make_pair(0, now));
  while (que.size()) {
    auto it = que.begin();
    int now = it->second;
    for (auto to : g[now])
      if (dp[now] + to.second < dp[to.first]) {
        que.erase(make_pair(dp[to.first], to.first));
        dp[to.first] = dp[now] + to.second;
        que.insert(make_pair(dp[to.first], to.first));
      }
    que.erase(it);
  }
  for (int i = 0; i < n; i++) naruto[now].push_back(make_pair(i, dp[i]));
  sort(naruto[now].begin(), naruto[now].end());
}
int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n, m;
  cin >> n >> m;
  vector<osu> edge(m);
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    g[a].push_back(make_pair(b, c));
    g[b].push_back(make_pair(a, c));
    edge[i] = {a, b, c};
  }
  Dsu dsu;
  dsu.Build(n);
  vector<int> used(m);
  for (int i = 0; i < m; i++) {
    int a = edge[i].a, b = edge[i].b;
    if (dsu.Merge(a, b)) {
      tree[a].push_back(make_pair(b, edge[i].cost));
      tree[b].push_back(make_pair(a, edge[i].cost));
      used[i] = 1;
    }
  }
  for (int i = 0; i < n; i++)
    if (tree[i].size()) {
      dfs(i, i, 0);
      break;
    }
  set<int> _border;
  for (int i = 0; i < m; i++)
    if (!used[i]) {
      int a = edge[i].a;
      if (kek[a]) _border.insert(a);
      a = edge[i].b;
      if (kek[a]) _border.insert(a);
    }
  vector<int> border;
  for (auto i : _border) border.push_back(i);
  set<int> _lol;
  for (int i = 0; i < m; i++)
    if (!used[i]) {
      _lol.insert(edge[i].a);
      _lol.insert(edge[i].b);
    }
  for (auto i : _lol) algos(n, i);
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    long long ans = INF;
    if (kek[a] and kek[b]) {
      int c = lca(a, b);
      ans = min(ans, pref[a] - pref[c] + pref[b] - pref[c]);
    }
    auto it = lower_bound(naruto[a].begin(), naruto[a].end(),
                          make_pair(b, -inf), cmp);
    if (it != naruto[a].end() and it->first == b)
      ans = min(ans, 1LL * it->second);
    int flag = 0;
    if (kek[a]) {
      int now = a;
      for (auto v : border) {
        int c = lca(now, v);
        long long res = pref[now] - pref[c] + pref[v] - pref[c];
        auto it1 = lower_bound(naruto[v].begin(), naruto[v].end(),
                               make_pair(b, -INF), cmp);
        if (it1 != naruto[v].end()) {
          res += it1->second;
          ans = min(ans, res);
        }
      }
      flag = 1;
    }
    swap(a, b);
    if (kek[a] and !flag) {
      int now = a;
      for (auto v : border) {
        int c = lca(now, v);
        long long res = pref[now] - pref[c] + pref[v] - pref[c];
        auto it1 = lower_bound(naruto[v].begin(), naruto[v].end(),
                               make_pair(b, -INF), cmp);
        if (it1 != naruto[v].end()) {
          res += it1->second;
          ans = min(ans, res);
        }
      }
    }
    cout << ans << "\n";
  }
  return 0;
}

```