This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define int long long
typedef long long ll;
const int M = 2e6 + 5, MOD = 1e9+7;
vector<pair<int, int>> node[M];
int dist[M], n, vis[M];
int dis[20][20];
int di[M];
void dij1() {
    for (int i = 1; i <= n; i++) dist[i] = INT_MAX;
    priority_queue<pair<int, int>> q;
    int k;
    cin >> k;
    for (int i = 1; i <= k; i++) {
        int a; cin >> a;
        dist[a] = 0;
        q.push({0, a});
    }
 
    while (!q.empty()) {
        auto [new_dist, new_node] = q.top(); q.pop();
        new_dist = -new_dist;
        if (dist[new_node] < new_dist) continue;
        for (auto [N, d]:node[new_node]) {
            if (new_dist + d < dist[N]) {
                dist[N] = new_dist + d;
                q.push({-dist[N], N});
            }
        }
    }
}
void dij2(int s) {
    for (int i = 1; i <= n; i++) di[i] = INT_MAX;
    for (int i = 1; i <= n; i++) {
        for (auto&[j, b]:node[i]) {
            b = INT_MAX-min(dist[i], dist[j]);
        }
    }
    priority_queue<pair<int, int>> q;
    di[s] = 0;
    q.push({0, s});
 
    while (!q.empty()) {
        auto [new_dist, new_node] = q.top(); q.pop();
        new_dist = -new_dist;
        if (di[new_node] < new_dist) continue;
        for (auto [N, d]:node[new_node]) {
            if (max(new_dist, d) < di[N]) {
                di[N] = max(new_dist, d);
                q.push({-di[N], N});
            }
        }
    }
}
signed main() {
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    srand(time(0));
    
    int m;
    cin >> n >> m;
    map<pair<int, int>, bool> ok;
 
    for (int i = 1; i <= m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
 
        node[a].push_back({b, c});
        node[b].push_back({a, c});
    }
 
    dij1();
 
    int t;
    cin >> t;
    if (t == 1) {
        int a, b;
        cin >> a >> b;
        dij2(a);
        cout << INT_MAX-di[b] << endl;
        return 0;
    }
    if (n <= 15) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) dis[i][j] = INT_MAX;
        }
        for (int i = 1; i <= n; i++) {
            for (auto [j, b]:node[i]) {
                dis[i][j] = INT_MAX-min(dist[i], dist[j]);
                dis[j][i] = INT_MAX-min(dist[i], dist[j]);
            }
        }
        for (int x = 1; x <= n; x++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    dis[i][j] = min(dis[i][j], max(dis[i][x], dis[x][j]));
                }
            }
        }
        while (t--) {
            int a, b;
            cin >> a >> b;
            cout << INT_MAX-dis[a][b] << endl;
        }
        return 0;
    }
    while (t--) {
        int a, b;
        cin >> a >> b;
 
        cout << min(dist[a], dist[b]) << endl;
    }
 
    return 0;
}
 
/*
9 12
1 9 4
1 2 5
2 3 7
2 4 3
4 3 6
3 6 4
8 7 10
6 7 5
5 8 1
9 5 7
5 4 12
6 8 2
2
4 7
5
1 6
5 3
4 8
5 8
1 5
*/
Compilation message (stderr)
plan.cpp: In function 'void dij1()':
plan.cpp:24:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   24 |         auto [new_dist, new_node] = q.top(); q.pop();
      |              ^
plan.cpp:27:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   27 |         for (auto [N, d]:node[new_node]) {
      |                   ^
plan.cpp: In function 'void dij2(long long int)':
plan.cpp:39:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   39 |         for (auto&[j, b]:node[i]) {
      |                   ^
plan.cpp:49:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   49 |         auto [new_dist, new_node] = q.top(); q.pop();
      |              ^
plan.cpp:52:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   52 |         for (auto [N, d]:node[new_node]) {
      |                   ^
plan.cpp: In function 'int main()':
plan.cpp:98:23: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
   98 |             for (auto [j, b]:node[i]) {
      |                       ^| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |