#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") // codeforces
// #pragma GCC target("avx,avx2,fma")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native") // yandex
#include "bits/stdc++.h"
using namespace std;
#ifndef LOCAL
#define endl '\n'
#endif
#define fr(i, a, b) for(int i = a; i <= b; i++)
#define rf(i, a, b) for(int i = a; i >= b; i--)
#define pf push_front
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define sz(x) (int)x.size()
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
typedef long double f80;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
int pct(int x) { return __builtin_popcount(x); }
int pct(ll x) { return __builtin_popcountll(x); }
int bt(int x) { return 31 - __builtin_clz(x); } // floor(log2(x))
int bt(ll x) { return 63 - __builtin_clzll(x); } // floor(log2(x))
int cdiv(int a, int b) { return a / b + !(a < 0 || a % b == 0); }
ll cdiv(ll a, ll b) { return a / b + !(a < 0 || a % b == 0); }
int nxt_C(int x) { int c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }
ll nxt_C(ll x) { ll c = x & -x, r = x + c; return (((r ^ x) >> 2) / c) | r; }
vector<int> get_bits(int mask) {
vector<int> bb;
while(mask) { int b = bt(mask); bb.pb(b); mask ^= (1 << b); }
reverse(all(bb));
return bb;
}
int get_mask(vector<int> v) {
int mask = 0;
for(int x : v) { mask ^= (1 << x); }
return mask;
}
template<typename T>
void uniq(vector<T> &v) { sort(all(v)); v.resize(unique(all(v)) - v.begin()); }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll rand(ll l, ll r){
uniform_int_distribution<ll> uid(l, r);
return uid(rng);
}
void sc() {}
template <typename Head, typename... Tail>
void sc(Head &H, Tail &... T) { cin >> H; sc(T...); }
#ifdef LOCAL
#define debug(...) cerr << "[L:" << __LINE__ << "][" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
// #ifndef LOCAL
// string to_string(__int128 x) {
// string s = "";
// bool neg = 0;
// if(x < 0) { s += "-"; neg = 1; x = -x; }
// if(!x) s += '0';
// while(x) {
// int rem = x % 10;
// s += to_string(rem);
// x /= 10;
// }
// reverse(s.begin() + neg, s.end());
// return s;
// }
// #endif
const int mod = 1e9 + 7; // 998244353;
int pwr(int a,ll b) {
int ans = 1;
while(b) {
if(b & 1) ans = (ans * 1LL * a) % mod;
a = (a * 1LL * a) % mod;
b >>= 1;
}
return ans;
}
/*
Lookout for overflows!!
Check array sizes!!
Clear before test cases!!
Use the correct modulo!!
Check for corner cases!!
Are you forgetting something?!
Read problem statement carefully!!!
*/
const int N = 1e6 + 5;
const int LOGN = 20;
vector<int> g[N];
bool centroid[N];
int depth[N], par[N], sz[N];
int dist_par[N][LOGN], dsz[N];
vector<pii> dis_child[N];
bool tmp[N];
pii qq[N];
void dfs2(int u,int p){
tmp[u] = 0;
sz[u] = 1;
for(int v : g[u])
if(v != p && !centroid[v]){
dfs2(v, u);
sz[u] += sz[v];
}
}
int get(int u,int p,int S){
for(int v : g[u])
if(v != p && !centroid[v] && sz[v] > S / 2)
return get(v, u, S);
return u;
}
int solve(int u){
dfs2(u, 0);
int c = get(u, 0, sz[u]);
centroid[c] = 1;
int l = 0, r = 0;
qq[r++] = {0, c};
tmp[c] = 1;
pii x;
while(l < r) {
x = qq[l++];
dis_child[c].eb(x);
dist_par[x.se][dsz[x.se]++] = x.fi;
int u = x.se;
for(int v : g[u]) {
if(!tmp[v] && !centroid[v]) {
tmp[v] = 1;
qq[r++] = {x.fi + 1, v};
}
}
}
for(int v : g[c])
if(!centroid[v]) {
par[solve(v)] = c;
}
return c;
}
void dfs1(int u,int p) {
depth[u] = depth[p] + 1;
for(int v : g[u])
if(v != p)
dfs1(v, u);
}
ll c[N];
bool vis[N];
set<pair<ll,int>> q;
void solve() {
int n, m, a, b;
sc(n, m, a, b);
q.clear();
fr(i, 1, n) {
dis_child[i].clear();
dsz[i] = 0;
g[i].clear();
centroid[i] = 0;
vis[i] = 0;
par[i] = 0;
}
fr(i, 1, n) {
int p;
sc(p, c[i]);
// p = i - 1, c[i] = 1;
if(!c[i]) c[i] = 1e18;
if(p) {
g[p].eb(i);
g[i].eb(p);
}
}
c[a] = c[b] = 0;
solve(1);
fr(i, 1, n) {
reverse(dist_par[i], dist_par[i] + dsz[i]);
reverse(all(dis_child[i]));
}
q.emplace(0, a);
vis[a] = 1;
ll ans = -1;
while(!q.empty()) {
auto x = *q.begin();
q.erase(q.begin());
int u = x.se;
ll val = x.fi;
if(val >= 1e18) break;
if(u == b) { ans = val; break; }
int p = u, k = 0;
while(p) {
int d = dist_par[u][k];
while(!dis_child[p].empty() && dis_child[p].back().fi <= m - d) {
int v = dis_child[p].back().se;
dis_child[p].pop_back();
if(!vis[v]) {
vis[v] = 1;
q.emplace(c[v] + val, v);
}
}
p = par[p], k++;
}
}
cout << ans << endl;
}
int main() {
ios :: sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
for(int tt = 1; tt <= t; tt++) {
cout << "Case #" << tt << ": ";
solve();
}
return 0;
}
I3ByYWdtYSBjb21tZW50KGxpbmtlciwgIi9zdGFjazoyMDAwMDAwMDAiKQojcHJhZ21hIEdDQyBvcHRpbWl6ZSgiT2Zhc3QiKQovLyAjcHJhZ21hIEdDQyBvcHRpbWl6ZSAoInVucm9sbC1sb29wcyIpCi8vICNwcmFnbWEgR0NDIHRhcmdldCgic3NlLHNzZTIsc3NlMyxzc3NlMyxzc2U0LHBvcGNudCxhYm0sbW14LGF2eCx0dW5lPW5hdGl2ZSIpIC8vIGNvZGVmb3JjZXMKLy8gI3ByYWdtYSBHQ0MgdGFyZ2V0KCJhdngsYXZ4MixmbWEiKQovLyNwcmFnbWEgR0ND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CiAgICBwaWkgeDsKICAgIHdoaWxlKGwgPCByKSB7CiAgICAgICAgeCA9IHFxW2wrK107CiAgICAgICAgZGlzX2NoaWxkW2NdLmViKHgpOwogICAgICAgIGRpc3RfcGFyW3guc2VdW2Rzelt4LnNlXSsrXSA9IHguZmk7CiAgICAgICAgaW50IHUgPSB4LnNlOwogICAgICAgIGZvcihpbnQgdiA6IGdbdV0pIHsKICAgICAgICAgICAgaWYoIXRtcFt2XSAmJiAhY2VudHJvaWRbdl0pIHsKICAgICAgICAgICAgICAgIHRtcFt2XSA9IDE7CiAgICAgICAgICAgICAgICBxcVtyKytdID0ge3guZmkgKyAxLCB2fTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KICAgIGZvcihpbnQgdiA6IGdbY10pCiAgICAgICAgaWYoIWNlbnRyb2lkW3ZdKSB7CiAgICAgICAgICAgIHBhcltzb2x2ZSh2KV0gPSBjOwogICAgICAgIH0KICAgIHJldHVybiBjOwp9CnZvaWQgZGZzMShpbnQgdSxpbnQgcCkgewogICAgZGVwdGhbdV0gPSBkZXB0aFtwXSArIDE7CiAgICBmb3IoaW50IHYgOiBnW3VdKQogICAgICAgIGlmKHYgIT0gcCkKICAgICAgICAgICAgZGZzMSh2LCB1KTsKfQoKbGwgY1tOXTsKYm9vbCB2aXNbTl07CnNldDxwYWlyPGxsLGludD4+IHE7Cgp2b2lkIHNvbHZlKCkgewogICAgaW50IG4sIG0sIGEsIGI7CiAgICBzYyhuLCBtLCBhLCBiKTsKICAgIHEuY2xlYXIoKTsKICAgIGZyKGksIDEsIG4pIHsKICAgICAgICBkaXNfY2hpbGRbaV0uY2xlYXIoKTsKICAgICAgICBkc3pbaV0gPSAwOwogICAgICAgIGdbaV0uY2xlYXIoKTsKICAgICAgICBjZW50cm9pZFtpXSA9IDA7CiAgICAgICAgdmlzW2ldID0gMDsKICAgICAgICBwYXJbaV0gPSAwOwogICAgfQogICAgZnIoaSwgMSwgbikgewogICAgICAgIGludCBwOwogICAgICAgIHNjKHAsIGNbaV0pOwogICAgICAgIC8vIHAgPSBpIC0gMSwgY1tpXSA9IDE7CiAgICAgICAgaWYoIWNbaV0pIGNbaV0gPSAxZTE4OwogICAgICAgIGlmKHApIHsKICAgICAgICAgICAgZ1twXS5lYihpKTsKICAgICAgICAgICAgZ1tpXS5lYihwKTsKICAgICAgICB9CiAgICB9CiAgICBjW2FdID0gY1tiXSA9IDA7CiAgICBzb2x2ZSgxKTsKICAgIGZyKGksIDEsIG4pIHsKICAgICAgICByZXZlcnNlKGRpc3RfcGFyW2ldLCBkaXN0X3BhcltpXSArIGRzeltpXSk7CiAgICAgICAgcmV2ZXJzZShhbGwoZGlzX2NoaWxkW2ldKSk7CiAgICB9CiAgICBxLmVtcGxhY2UoMCwgYSk7CiAgICB2aXNbYV0gPSAxOwogICAgbGwgYW5zID0gLTE7CiAgICB3aGlsZSghcS5lbXB0eSgpKSB7CiAgICAgICAgYXV0byB4ID0gKnEuYmVnaW4oKTsKICAgICAgICBxLmVyYXNlKHEuYmVnaW4oKSk7CiAgICAgICAgaW50IHUgPSB4LnNlOwogICAgICAgIGxsIHZhbCA9IHguZmk7CiAgICAgICAgaWYodmFsID49IDFlMTgpIGJyZWFrOwogICAgICAgIGlmKHUgPT0gYikgeyBhbnMgPSB2YWw7IGJyZWFrOyB9CiAgICAgICAgaW50IHAgPSB1LCBrID0gMDsKICAgICAgICB3aGlsZShwKSB7CiAgICAgICAgICAgIGludCBkID0gZGlzdF9wYXJbdV1ba107CiAgICAgICAgICAgIHdoaWxlKCFkaXNfY2hpbGRbcF0uZW1wdHkoKSAmJiBkaXNfY2hpbGRbcF0uYmFjaygpLmZpIDw9IG0gLSBkKSB7CiAgICAgICAgICAgICAgICBpbnQgdiA9IGRpc19jaGlsZFtwXS5iYWNrKCkuc2U7CiAgICAgICAgICAgICAgICBkaXNfY2hpbGRbcF0ucG9wX2JhY2soKTsKICAgICAgICAgICAgICAgIGlmKCF2aXNbdl0pIHsKICAgICAgICAgICAgICAgICAgICB2aXNbdl0gPSAxOwogICAgICAgICAgICAgICAgICAgIHEuZW1wbGFjZShjW3ZdICsgdmFsLCB2KTsKICAgICAgICAgICAgICAgIH0KICAgICAgICAgICAgfQogICAgICAgICAgICBwID0gcGFyW3BdLCBrKys7CiAgICAgICAgfQogICAgfQogICAgY291dCA8PCBhbnMgPDwgZW5kbDsKfQoKaW50IG1haW4oKSB7Cglpb3MgOjogc3luY193aXRoX3N0ZGlvKDApOwoJY2luLnRpZSgwKTsKCWNvdXQudGllKDApOwoJaW50IHQgPSAxOwoJY2luID4+IHQ7Cglmb3IoaW50IHR0ID0gMTsgdHQgPD0gdDsgdHQrKykgewogICAgICAgIGNvdXQgPDwgIkNhc2UgIyIgPDwgdHQgPDwgIjogIjsKCQlzb2x2ZSgpOwoJfQoJcmV0dXJuIDA7Cn0=