#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5+11;
const int oo = 0x3f3f3f3f;
typedef long long ll;
ll cost[maxn<<1];
int to[maxn<<1],cap[maxn<<1],flow[maxn<<1],nxt[maxn<<1];
int head[maxn],tot;
void init(){
memset(head,-1,sizeof head);
tot=0;
}
void add(int u,int v,int c,int w){
to[tot]=v;
cap[tot]=c;
flow[tot]=0;
cost[tot]=w;
nxt[tot]=head[u];
head[u]=tot++;
swap(u,v);
to[tot]=v;
cap[tot]=0;
flow[tot]=0;
cost[tot]=-w;
nxt[tot]=head[u];
head[u]=tot++;
}
struct QUEUE{
int que[maxn];
int front,rear;
void init(){front=rear=0;}
void push(int x){que[rear++]=x;}
int pop(){return que[front++];}
bool empty(){return front==rear;}
}que;
int n,m,s,t;
bool vis[maxn];
int pre[maxn],dis[maxn];
bool spfa(){
que.init();
memset(vis,0,sizeof vis);
memset(pre,-1,sizeof pre);
memset(dis,oo,sizeof dis);
que.push(s);vis[s]=1;dis[s]=0;
while(!que.empty()){
int u=que.pop(); vis[u]=0;
for(int i = head[u]; ~i; i = nxt[i]){
int v=to[i],c=cap[i],f=flow[i],w=cost[i];
if(c>f&&dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
pre[v]=i;
if(!vis[v]){
que.push(v);
vis[v]=1;
}
}
}
}
if(dis[t]==oo) return 0;
else return 1;
}
ll mcmf(){
ll mc=0,mf=0;
while(spfa()){
ll tf=oo+1;
for(int i = pre[t]; ~i; i = pre[to[i^1]]){
tf=min(tf,1ll*cap[i]-flow[i]);
}
mf+=tf;
for(int i = pre[t]; ~i; i = pre[to[i^1]]){
flow[i]+=tf;
flow[i^1]-=tf;
}
mc+=dis[t]*tf;
}
return mc;
}
#define rep(i,j,k) for(int i = j; i <= k; i++)
#define repp(i,j,k) for(int i = j; i < k; i++)
#define repe(i,u) for(int i = head[u]; ~i; i = nxt[i])
#define scan(a) scanf("%d",&a)
#define scann(a,b) scanf("%d%d",&a,&b)
#define scannn(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define println(a) printf("%d\n",a)
#define printbk(a) printf("%d ",a)
#define print(a) printf("%d",a)
vector<int> G[maxn],G2[maxn];
int haxi[maxn];
int main(){
int T,kase=0,m1,m2,a[maxn],b[maxn]; scan(T);
while(T--){
init();
memset(G,0,sizeof G);
memset(G2,0,sizeof G2);
memset(haxi,0,sizeof haxi);
int cnt=0;
scan(m1);
rep(i,1,m1){
scan(a[i]);
int x=0;
char ch;
while((ch=getchar())){
if(ch==' '||ch=='\n'){
if(x){
G[i].push_back(x);
if(!haxi[x]) haxi[x]=++cnt;
}
x=0;
if(ch=='\n')break;
}
if(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
}
}
}
scan(m2);
rep(i,1,m2){
scan(b[i]);
int x=0;
char ch;getchar();
while((ch=getchar())){
if(ch==' '||ch=='\n'){
if(x){
G2[i].push_back(x);
if(!haxi[x]) haxi[x]=++cnt;
}
x=0;
if(ch=='\n')break;
}
else if(ch>='0'&&ch<='9'){
x=x*10+ch-'0';
}
}
}
// rep(i,1,m1){
// repp(j,0,G[i].size()){
// cout<<i<<" x:"<<G[i][j]<<endl;
// }
// }
// rep(i,1,m2){
// repp(j,0,G2[i].size()){
// cout<<i<<" x:"<<G2[i][j]<<endl;
// }
// }
s=m1+m2+cnt+1;t=s+1;n=t;
#define duimian(x) ((x)+m1)
#define channel(x) ((x)+m1+m2)
rep(i,1,m1){
add(s,i,G[i].size(),-a[i]);
repp(j,0,G[i].size()){
add(i,channel(haxi[G[i][j]]),1,0);
//
// cout<<"channel"<<channel(G[i][j])<<endl;
}
}
rep(i,1,m2){
add(s,duimian(i),G2[i].size(),-b[i]);
repp(j,0,G2[i].size()){
add(duimian(i),channel(haxi[G2[i][j]]),1,0);
//
// cout<<"channel"<<channel(G2[i][j])<<endl;
}
}
rep(i,1,cnt){
add(channel(i),t,1,0);
}
ll ans=mcmf();
printf("Case %d:\n%lld\n",++kase,-ans);
if(T) printf("\n");
}
return 0;
}