#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 87;
const ll inf = 1e18;
int c[N];
vector<int> g[N];
bool vis[N];
int pa[N];
void dfs(int u)
{
vis[u] = 1;
for (int v : g[u])
if (!vis[v]) {
pa[v] = u;
dfs(v);
}
}
void solve()
{
int n, m, a, b;
cin >> n >> m >> a >> b;
for (int i = 1; i <= n; ++i)
g[i].clear();
for (int i = 1; i <= n; ++i) {
int pi;
cin >> pi >> c[i];
if (pi)
g[pi].push_back(i), g[i].push_back(pi);
}
fill_n(vis, n + 1, 0);
dfs(a);
vector<int> path;
for (int i = b; i != a; i = pa[i])
path.push_back(i);
reverse(path.begin(), path.end());
fill_n(vis, n + 1, 0);
vis[a] = 1;
for (int u : path)
vis[u] = 1;
int delta = 0;
deque<pair<int, ll>> dk = {{m, 0}};
for (int u : path) {
delta -= 1;
while (dk.size() && dk[0].first + delta < 0)
dk.pop_front();
vector<int> qu = {u}, nq, co;
for (int k = 0; qu.size(); ++k) {
co.push_back(0);
nq.clear();
for (int x : qu) {
if (co.back() == 0)
co.back() = c[x];
else if (c[x])
co.back() = min(co.back(), c[x]);
for (int y : g[x])
if (!vis[y]) {
vis[y] = 1;
nq.push_back(y);
}
}
swap(qu, nq);
}
co.resize(min(m + 1, (int)co.size()));
vector<pair<int, ll>> v1, v2;
for (int k = 0, i = 0; k < co.size(); ++k) {
while (i < dk.size() && dk[i].first + delta < k)
++i;
if (i == dk.size())
break;
if (co[k])
v1.emplace_back(m - k - delta, co[k] + dk[i].second);
}
if (v1.empty())
continue;
while (dk.size() && dk.back().first >= v1.back().first) {
v2.push_back(dk.back());
dk.pop_back();
}
auto pb = [&] (pair<int, ll> p) {
while (dk.size() && dk.back().second >= p.second)
dk.pop_back();
if (dk.empty() || dk.back().first < p.first)
dk.push_back(p);
};
while (v1.size() && v2.size()) {
if (v1.back().first < v2.back().first)
pb(v1.back()), v1.pop_back();
else
pb(v2.back()), v2.pop_back();
}
while (v1.size())
pb(v1.back()), v1.pop_back();
while (v2.size())
pb(v2.back()), v2.pop_back();
}
ll ans = dk.size() ? dk[0].second : inf;
if (ans >= inf)
cout << "-1\n";
else
cout << ans << '\n';
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
int T;
cin >> T;
for (int i = 1; i <= T; ++i) {
cout << "Case #" << i << ": ";
solve();
}
}
I2luY2x1ZGU8Yml0cy9zdGRjKysuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKdHlwZWRlZiBsb25nIGxvbmcgbGw7CmNvbnN0IGludCBOID0gMWU2ICsgODc7CmNvbnN0IGxsIGluZiA9IDFlMTg7CmludCBjW05dOwp2ZWN0b3I8aW50PiBnW05dOwpib29sIHZpc1tOXTsKaW50IHBhW05dOwp2b2lkIGRmcyhpbnQgdSkKewogICAgdmlzW3VdID0gMTsKICAgIGZvciAoaW50IHYgOiBnW3VdKQogICAgICAgIGlmICghdmlzW3ZdKSB7CiAgICAgICAgICAgIHBhW3ZdID0gdTsKICAgICAgICAgICAgZGZzKHYpOwogICAgICAgIH0KfQp2b2lkIHNvbHZlKCkKewogICAgaW50IG4sIG0sIGEsIGI7CiAgICBjaW4gPj4gbiA+PiBtID4+IGEgPj4gYjsKICAgIGZvciAoaW50IGkgPSAxOyBpIDw9IG47ICsraSkKICAgICAgICBnW2ldLmNsZWFyKCk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyArK2kpIHsKICAgICAgICBpbnQgcGk7CiAgICAgICAgY2luID4+IHBpID4+IGNbaV07CiAgICAgICAgaWYgKHBpKQogICAgICAgICAgICBnW3BpXS5wdXNoX2JhY2soaSksIGdbaV0ucHVzaF9iYWNrKHBpKTsKICAgIH0KICAgIGZpbGxfbih2aXMsIG4gKyAxLCAwKTsKICAgIGRmcyhhKTsKICAgIHZlY3RvcjxpbnQ+IHBhdGg7CiAgICBmb3IgKGludCBpID0gYjsgaSAhPSBhOyBpID0gcGFbaV0pCiAgICAgICAgcGF0aC5wdXNoX2JhY2soaSk7CiAgICByZXZlcnNlKHBhdGguYmVnaW4oKSwgcGF0aC5lbmQoKSk7CiAgICBmaWxsX24odmlzLCBuICsgMSwgMCk7CiAgICB2aXNbYV0gPSAxOwogICAgZm9yIChpbnQgdSA6IHBhdGgpCiAgICAgICAgdmlzW3VdID0gMTsKICAgIGludCBkZWx0YSA9IDA7CiAgICBkZXF1ZTxwYWlyPGludCwgbGw+PiBkayA9IHt7bSwgMH19OwogICAgZm9yIChpbnQgdSA6IHBhdGgpIHsKICAgICAgICBkZWx0YSAtPSAxOwogICAgICAgIHdoaWxlIChkay5zaXplKCkgJiYgZGtbMF0uZmlyc3QgKyBkZWx0YSA8IDApCiAgICAgICAgICAgIGRrLnBvcF9mcm9udCgpOwogICAgICAgIHZlY3RvcjxpbnQ+IHF1ID0ge3V9LCBucSwgY287CiAgICAgICAgZm9yIChpbnQgayA9IDA7IHF1LnNpemUoKTsgKytrKSB7CiAgICAgICAgICAgIGNvLnB1c2hfYmFjaygwKTsKICAgICAgICAgICAgbnEuY2xlYXIoKTsKICAgICAgICAgICAgZm9yIChpbnQgeCA6IHF1KSB7CiAgICAgICAgICAgICAgICBpZiAoY28uYmFjaygpID09IDApCiAgICAgICAgICAgICAgICAgICAgY28uYmFjaygpID0gY1t4XTsKICAgICAgICAgICAgICAgIGVsc2UgaWYgKGNbeF0pCiAgICAgICAgICAgICAgICAgICAgY28uYmFjaygpID0gbWluKGNvLmJhY2soKSwgY1t4XSk7CiAgICAgICAgICAgICAgICBmb3IgKGludCB5IDogZ1t4XSkKICAgICAgICAgICAgICAgICAgICBpZiAoIXZpc1t5XSkgewogICAgICAgICAgICAgICAgICAgICAgICB2aXNbeV0gPSAxOwogICAgICAgICAgICAgICAgICAgICAgICBucS5wdXNoX2JhY2soeSk7CiAgICAgICAgICAgICAgICAgICAgfQogICAgICAgICAgICB9CiAgICAgICAgICAgIHN3YXAocXUsIG5xKTsKICAgICAgICB9CiAgICAgICAgY28ucmVzaXplKG1pbihtICsgMSwgKGludCljby5zaXplKCkpKTsKICAgICAgICB2ZWN0b3I8cGFpcjxpbnQsIGxsPj4gdjEsIHYyOwogICAgICAgIGZvciAoaW50IGsgPSAwLCBpID0gMDsgayA8IGNvLnNpemUoKTsgKytrKSB7CiAgICAgICAgICAgIHdoaWxlIChpIDwgZGsuc2l6ZSgpICYmIGRrW2ldLmZpcnN0ICsgZGVsdGEgPCBrKQogICAgICAgICAgICAgICAgKytpOwogICAgICAgICAgICBpZiAoaSA9PSBkay5zaXplKCkpCiAgICAgICAgICAgICAgICBicmVhazsKICAgICAgICAgICAgaWYgKGNvW2tdKQogICAgICAgICAgICAgICAgdjEuZW1wbGFjZV9iYWNrKG0gLSBrIC0gZGVsdGEsIGNvW2tdICsgZGtbaV0uc2Vjb25kKTsKICAgICAgICB9CiAgICAgICAgaWYgKHYxLmVtcHR5KCkpCiAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgIHdoaWxlIChkay5zaXplKCkgJiYgZGsuYmFjaygpLmZpcnN0ID49IHYxLmJhY2soKS5maXJzdCkgewogICAgICAgICAgICB2Mi5wdXNoX2JhY2soZGsuYmFjaygpKTsKICAgICAgICAgICAgZGsucG9wX2JhY2soKTsKICAgICAgICB9CiAgICAgICAgYXV0byBwYiA9IFsmXSAocGFpcjxpbnQsIGxsPiBwKSB7CiAgICAgICAgICAgIHdoaWxlIChkay5zaXplKCkgJiYgZGsuYmFjaygpLnNlY29uZCA+PSBwLnNlY29uZCkKICAgICAgICAgICAgICAgIGRrLnBvcF9iYWNrKCk7CiAgICAgICAgICAgIGlmIChkay5lbXB0eSgpIHx8IGRrLmJhY2soKS5maXJzdCA8IHAuZmlyc3QpCiAgICAgICAgICAgICAgICBkay5wdXNoX2JhY2socCk7CiAgICAgICAgfTsKICAgICAgICB3aGlsZSAodjEuc2l6ZSgpICYmIHYyLnNpemUoKSkgewogICAgICAgICAgICBpZiAodjEuYmFjaygpLmZpcnN0IDwgdjIuYmFjaygpLmZpcnN0KQogICAgICAgICAgICAgIC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