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>
#define ll long long
#define ull unsigned long long
#define ld long double
#define pb push_back
#define pf push_front
#define vi vector<ll>
#define vii vector<vi>
#define pll pair<ll, ll>
#define vpll vector<pll>
#define all(a) a.begin(), a.end()
#define fi first
#define se second
using namespace std;
const ll mod = 1e9+7; 
const ll inf = 2e18;
const ll B = 320;
const ll N = 1e5+8;
ll n,s,q,e;
vector<pair<ll,ll>> g[N];
vector<pair<ll,ll>> edge = {{0,0}};
bool mark[N];
ll tin[N],tout[N],tdfs,d[N],h[N];
void dfs_shop(ll u, ll p){
    tin[u] = ++tdfs;
    for(pair<ll,ll> ed:g[u]){
        ll v = ed.fi;
        ll w = ed.se;
        if(v == p) continue;
        d[v] = d[u]+w;
        h[v] = h[u]+1;
        dfs_shop(v,u);
    }
    tout[u] = tdfs;
}
ll a[N];
void dfs_a(ll u, ll p){
    a[u] = inf;
    if(mark[u]) a[u] = d[u];
    for(pair<ll,ll> ed:g[u]){
        ll v = ed.fi;
        ll w = ed.se;
        if(v == p) continue;
        dfs_a(v,u);
        a[u] = min(a[u],a[v]);
    }
}
struct node{
    ll pr;
    ll mn = inf;
} up[N][20];
void dfs_pre(ll u, ll p){
    for(pair<ll,ll> ed:g[u]){
        ll v = ed.fi;
        if(v == p) continue;
        up[v][0].pr = u;
        up[v][0].mn = min(a[u],a[v]);
        for(ll i = 1; i <= 19; i++){
            up[v][i].pr = up[up[v][i-1].pr][i-1].pr;
            up[v][i].mn = min(up[v][i-1].mn,up[up[v][i-1].pr][i-1].mn);
        }
        dfs_pre(v,u);
    }
}
bool is_anc(ll u, ll v){
    return tin[u] <= tin[v] && tout[v] <= tout[u];
}
ll query(ll u, ll p){
    ll k = h[u]-h[p];
    ll ans = inf;
    for(ll i = 19; i >= 0; i--){
        if(k>>i&1){
            ans = min(ans,up[u][i].mn);
            u = up[u][i].pr;
        }
    }
    ans = min(ans,a[u]);
    return ans;
}
void solve(){
    cin >> n >> s >> q >> e;
    for(ll i = 1; i < n; i++){
        ll u,v;
        ll w;cin >> u >> v >> w;
        g[u].pb({v,w});
        g[v].pb({u,w});
        edge.pb({u,v});
    }
    for(ll i = 1; i <= s; i++){
        ll x;cin >> x;
        mark[x] = 1;
    }
    dfs_shop(e,e);
    dfs_a(e,e);
    for(ll i = 1; i <= n; i++) a[i] -= 2*d[i];
    dfs_pre(e,e);
    while(q--){
        ll id,r;cin >> id >> r;
        ll u;
        if(h[edge[id].fi] > h[edge[id].se]) u = edge[id].fi;
        else u = edge[id].se;
        if(!is_anc(u,r)) cout << "escaped\n";
        else{
            ll ans = query(r,u)+d[r];
            if(ans >= 1e18) cout << "oo\n";
            else cout << ans << '\n';
        }
    }
}
signed main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    if (fopen("test.inp", "r")) {
        freopen("test.inp", "r", stdin);
        freopen("test.out", "w", stdout);
    }   
    ll T = 1;
    // cin >> T;
    for (ll i = 1; i <= T; i++) {
        solve();
        // cout << '\n';
    }
}
Compilation message (stderr)
valley.cpp: In function 'void dfs_a(long long int, long long int)':
valley.cpp:42:12: warning: unused variable 'w' [-Wunused-variable]
   42 |         ll w = ed.se;
      |            ^
valley.cpp: In function 'int main()':
valley.cpp:113:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  113 |         freopen("test.inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
valley.cpp:114:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
  114 |         freopen("test.out", "w", stdout);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |