#include <bits/stdc++.h>
using namespace std;
#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 1; i <= n; i++)
#define fast \
ios_base::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>
const int N = 305;
const int inf = 1e12;
const int mx = 1000;
vector<int> g[N];
bool locate[N][N];
int dis[N][N];
vector<vector<int>> recipe[N];
int dp[N][N];
int n, m, s, r;
//city i,stone j
int go(int i, int j)
{
int &ans = dp[i][j];
if (ans != -1)
return ans;
int res = inf;
if (locate[i][j] == 1)
return res = 0;
f(l, n) if (l != i) res = min(res, go(l, j) + dis[l][i]);
for (auto vv : recipe[j])
{
int temp = 0;
for (auto v : vv)
{
int temp2 = inf;
f(l, n) temp2 = min(temp2, go(l, v) + dis[i][l]);
temp += temp2;
}
res = min(res, temp);
}
return ans = res;
}
void solve()
{
cin >> n >> m >> s >> r;
int u, v;
while (m--)
{
cin >> u >> v;
g[u].pb(v);
g[v].pb(u);
}
//calculate distance
f(i, n)
{
f(j, n) dis[i][j] = mx;
vector<bool> vis(n, 0);
vis[i] = 1;
dis[i][i] = 0;
queue<int> q;
q.push(i);
while (!q.empty())
{
auto x = q.front();
q.pop();
for (auto v : g[x])
if (!vis[v])
{
vis[v] = 1;
q.push(v);
dis[i][v] = dis[i][x] + 1;
}
}
}
f(i, n)
{
cin >> u;
f(j, u)
{
cin >> v;
locate[i][v] = 1;
}
}
f(i, r)
{
vi go;
cin >> u;
f(j, u)
{
cin >> v;
go.pb(v);
}
cin >> v;
recipe[v].push_back(go);
}
f(i, n) f(j, s) dp[i][j] = -1;
int res = inf;
f(i, n) res = min(res, go(i, 1));
if (res >= 1e12)
res = -1;
cout << res << '\n';
for (int i = 0; i < N; i++)
{
g[i].clear();
for (int j = 0; j < N; j++)
locate[i][j] = 0;
recipe[i].clear();
}
}
signed main()
{
fast;
int t = 1;
cin >> t;
for (int i = 1; i <= t; i++)
{
cout << "Case #" << i << ": ";
solve();
}
}
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diA6IHZ2KQogICAgICAgIHsKICAgICAgICAgICAgaW50IHRlbXAyID0gaW5mOwoKICAgICAgICAgICAgZihsLCBuKSB0ZW1wMiA9IG1pbih0ZW1wMiwgZ28obCwgdikgKyBkaXNbaV1bbF0pOwoKICAgICAgICAgICAgdGVtcCArPSB0ZW1wMjsKICAgICAgICB9CgogICAgICAgIHJlcyA9IG1pbihyZXMsIHRlbXApOwogICAgfQoKICAgIHJldHVybiBhbnMgPSByZXM7Cn0KCnZvaWQgc29sdmUoKQp7CiAgICBjaW4gPj4gbiA+PiBtID4+IHMgPj4gcjsKCiAgICBpbnQgdSwgdjsKCiAgICB3aGlsZSAobS0tKQogICAgewogICAgICAgIGNpbiA+PiB1ID4+IHY7CiAgICAgICAgZ1t1XS5wYih2KTsKICAgICAgICBnW3ZdLnBiKHUpOwogICAgfQoKICAgIC8vY2FsY3VsYXRlIGRpc3RhbmNlCgogICAgZihpLCBuKQogICAgewogICAgICAgIGYoaiwgbikgZGlzW2ldW2pdID0gbXg7CgogICAgICAgIHZlY3Rvcjxib29sPiB2aXMobiwgMCk7CgogICAgICAgIHZpc1tpXSA9IDE7CiAgICAgICAgZGlzW2ldW2ldID0gMDsKCiAgICAgICAgcXVldWU8aW50PiBxOwogICAgICAgIHEucHVzaChpKTsKCiAgICAgICAgd2hpbGUgKCFxLmVtcHR5KCkpCiAgICAgICAgewogICAgICAgICAgICBhdXRvIHggPSBxLmZyb250KCk7CiAgICAgICAgICAgIHEucG9wKCk7CgogICAgICAgICAgICBmb3IgKGF1dG8gdiA6IGdbeF0pCiAgICAgICAgICAgICAgICBpZiAoIXZpc1t2XSkKICAgICAgICAgICAgICAgIHsKICAgICAgICAgICAgICAgICAgICB2aXNbdl0gPSAxOwogICAgICAgICAgICAgICAgICAgIHEucHVzaCh2KTsKICAgICAgICAgICAgICAgICAgICBkaXNbaV1bdl0gPSBkaXNbaV1beF0gKyAxOwogICAgICAgICAgICAgICAgfQogICAgICAgIH0KICAgIH0KCiAgICBmKGksIG4pCiAgICB7CiAgICAgICAgY2luID4+IHU7CgogICAgICAgIGYoaiwgdSkKICAgICAgICB7CiAgICAgICAgICAgIGNpbiA+PiB2OwogICAgICAgICAgICBsb2NhdGVbaV1bdl0gPSAxOwogICAgICAgIH0KICAgIH0KCiAgICBmKGksIHIpCiAgICB7CiAgICAgICAgdmkgZ287CgogICAgICAgIGNpbiA+PiB1OwoKICAgICAgICBmKGosIHUpCiAgICAgICAgewogICAgICAgICAgICBjaW4gPj4gdjsKICAgICAgICAgICAgZ28ucGIodik7CiAgICAgICAgfQoKICAgICAgICBjaW4gPj4gdjsKCiAgICAgICAgcmVjaXBlW3ZdLnB1c2hfYmFjayhnbyk7CiAgICB9CgogICAgZihpLCBuKSBmKGosIHMpIGRwW2ldW2pdID0gLTE7CgogICAgaW50IHJlcyA9IGluZjsKCiAgICBmKGksIG4pIHJlcyA9IG1pbihyZXMsIGdvKGksIDEpKTsKCiAgICBpZiAocmVzID49IDFlMTIpCiAgICAgICAgcmVzID0gLTE7CgogICAgY291dCA8PCByZXMgPDwgJ1xuJzsKCiAgICBmb3IgKGludCBpID0gMDsgaSA8IE47IGkrKykKICAgIHsKICAgICAgICBnW2ldLmNsZWFyKCk7CiAgICAgICAgZm9yIChpbnQgaiA9IDA7IGogPCBOOyBqKyspCiAgICAgICAgICAgIGxvY2F0ZVtpXVtqXSA9IDA7CiAgICAgICAgcmVjaXBlW2ldLmNsZWFyKCk7CiAgICB9Cn0KCnNpZ25lZCBtYWluKCkKewogICAgZmFzdDsKCiAgICBpbnQgdCA9IDE7CgogICAgY2luID4+IHQ7CgogICAgZm9yIChpbnQgaSA9IDE7IGkgPD0gdDsgaSsrKQogICAgewogICAgICAgIGNvdXQgPDwgIkNhc2UgIyIgPDwgaSA8PCAiOiAiOwogICAgICAgIHNvbHZlKCk7CiAgICB9Cn0K