#include<iostream>
#include<algorithm>
#include<vector>
#include<ctime>
#define MAXNUM 83
#define SEARCHRANGEMAX1 5
#define SEARCHRANGEMAX2 5
#define SEARCHRANGEMAX3 4
#define TIMELIMIT 850
using namespace std;
int nationnum,seanum,hillnum,totnum,player;
int area[MAXNUM]={0},map[MAXNUM]={0},linknum[MAXNUM]={0},link[MAXNUM][MAXNUM]={0},nearhill[MAXNUM]={0},nearsea[MAXNUM]={0};
int value[MAXNUM]={0},value2[MAXNUM]={0},value3[MAXNUM]={0};
bool canfill[4][MAXNUM]={0},islink[MAXNUM][MAXNUM]={0};
vector<int> linknation[MAXNUM],linksea[MAXNUM],linkhill[MAXNUM],neighbor[MAXNUM];
void init();
void take(const int,const int);
void remove(const int);
inline int min(const int,const int);
bool cmp(const int,const int);
bool cmp2(const int,const int);
bool cmp3(const int,const int);
int calcvalue(int,int);
inline int calcpriorty(const int,const int,const int,const int);
inline int nextcolor(const int,const int);
inline int min(const int x,const int y)
{
if(x<y)return x;
return y;
}
inline int nextcolor(const int color,const int n)
{
return (color+4+n)%4;
}
void init()
{
int i,j,temp;
cin>>nationnum>>seanum>>hillnum>>player;
totnum=nationnum+seanum+hillnum;
for(i=1;i<=totnum;i++)
map[i]=-1;
for(i=1;i<=totnum;i++)
{
cin>>area[i]>>linknum[i];
for(j=0;j<linknum[i];j++)
{
cin>>temp;
link[i][j]=temp;
islink[i][temp]=1;
if(temp<=nationnum)
{
linknation[i].push_back(temp);
if(i>nationnum+seanum)nearhill[temp]+=area[i];
}
else if(temp<=nationnum+seanum)
{
linksea[i].push_back(temp);
if(i>nationnum&&i<=nationnum+seanum)nearsea[temp]+=area[i];
}
else linkhill[i].push_back(temp);
}
}
for(i=0;i<4;i++)
for(j=1;j<=nationnum;j++)
canfill[i][j]=1;
}
void take(const int color,const int n)
{
map[n]=color;
int i,temp;
for(i=0;i<linknation[n].size();i++)
{
temp=linknation[n][i];
canfill[color][temp]=0;
}
for(i=0;i<linkhill[n].size();i++)
{
temp=linkhill[n][i];
neighbor[temp].push_back(n);
}
}
void remove(const int n)
{
int i,j,temp,temp2,color=map[n];
map[n]=-1;
canfill[color][n]=1;
for(i=0;i<linknation[n].size();i++)
{
temp=linknation[n][i];
canfill[color][temp]=1;
for(j=0;j<linknation[temp].size();j++)
{
temp2=linknation[temp][j];
if(map[temp2]==color)
{
canfill[color][temp]=0;
break;
}
}
}
for(i=0;i<linkhill[n].size();i++)
{
temp=linkhill[n][i];
neighbor[temp].pop_back();
}
}
int calcvalue(const int color,const int n)
{
int r,i,j,empty,temp,tempa,tempb,tempt;
int seaa,seab,seac,sead,sea0,sea1,sea2,sea3,s0,s1,s2,s3,sfa,sfb,hilla,hillb,hillc,hilld,hill0,hill1,hill2,hill3,h0,h1,h2,h3;
vector<int> vn,vt;
take(color,n);
const int color1=nextcolor(color,1);
const int color2=nextcolor(color,2);
const int color3=nextcolor(color,3);
r=400*area[n];
for(i=nationnum+1;i<=nationnum+seanum;i++)
{
empty=seaa=seab=seac=sead=sea0=sea1=sea2=sea3=s0=s1=s2=s3=sfa=sfb=0;
vn.clear();
for(j=0;j<linknation[i].size();j++)
{
temp=linknation[i][j];
if(map[temp]==color||map[temp]==color2)sfa++;
else if(map[temp]==color1||map[temp]==color3)sfb++;
else
{
empty++;
bool seadup=0;
if((canfill[color][temp]||canfill[color2][temp])&&(!canfill[color1][temp])&&(!canfill[color3][temp]))seaa++;
else if((canfill[color1][temp]||canfill[color3][temp])&&(!canfill[color][temp])&&(!canfill[color2][temp]))seab++;
else if((canfill[color1][temp]||canfill[color3][temp])&&(canfill[color][temp]||canfill[color2][temp]))seac++;
else
{
seadup=1;
sead++;
}
if(!seadup)vn.push_back(temp);
if(canfill[color][temp]&&(!canfill[color1][temp])&&(!canfill[color2][temp])&&(!canfill[color3][temp]))sea0++;
else if(canfill[color1][temp]&&(!canfill[color][temp])&&(!canfill[color2][temp])&&(!canfill[color3][temp]))sea1++;
else if(canfill[color2][temp]&&(!canfill[color][temp])&&(!canfill[color1][temp])&&(!canfill[color3][temp]))sea2++;
else if(canfill[color3][temp]&&(!canfill[color][temp])&&(!canfill[color1][temp])&&(!canfill[color2][temp]))sea3++;
if(canfill[color][temp])s0++;
if(canfill[color1][temp])s1++;
if(canfill[color2][temp])s2++;
if(canfill[color3][temp])s3++;
}
}
if(empty-sead==0)
{
if(islink[i][n])
{
if(sfa-sfb==1)r+=12000*area[i];
else if(sfa-sfb==0)r+=4000*area[i];
else if(sfa-sfb==2)r+=10000*area[i];
}
else
{
if(sfa>sfb)r+=4500*area[i];
else if(sfa<sfb)r-=4500*area[i];
else r-=2500*area[i];
}
}
else if(empty-sead==1)
{
if(seaa==1)
{
if(sfa-sfb==0)r+=9000*area[i];
else if(sfa-sfb==-1)r+=2500*area[i];
else if(sfa-sfb==1)r+=7500*area[i];
}
else if(seab==1||canfill[color1][vn.back()])
{
if(sfa-sfb==2)r+=9000*area[i];
else if(sfa-sfb==1)r+=3500*area[i];
else if(sfa-sfb==3)r+=7000*area[i];
}
else if(!canfill[color1][vn.back()]&&canfill[color2][vn.back()])
{
if(sfa-sfb==0)r+=9000*area[i];
else if(sfa-sfb==-1)r+=2500*area[i];
else if(sfa-sfb==1)r+=7500*area[i];
}
else
{
if(sfa-sfb==2)r+=9000*area[i];
else if(sfa-sfb==1)r+=3500*area[i];
else if(sfa-sfb==0)r+=500*area[i];
else if(sfa-sfb==3)r+=7000*area[i];
}
}
else if(empty-sead==2)
{
if(seaa==2||(seaa==1&&!canfill[color1][vn[0]]&&!canfill[color1][vn[1]]&&((canfill[color2][vn[0]]&&!canfill[color3][vn[1]])||(canfill[color2][vn[1]]&&!canfill[color3][vn[0]]))))
{
if(sfa-sfb==-1)r+=8000*area[i];
else if(sfa-sfb==-2)r+=2000*area[i];
else if(sfa-sfb==0)r+=6500*area[i];
}
else if(seab==2||(seab==1&&((canfill[color1][vn[0]]&&!canfill[color2][vn[1]])||(canfill[color1][vn[1]]&&!canfill[color2][vn[0]]))))
{
if(sfa-sfb==3)r+=8000*area[i];
else if(sfa-sfb==2)r+=3000*area[i];
else if(sfa-sfb==1)r+=1000*area[i];
else if(sfa-sfb==4)r+=6000*area[i];
}
else if((seaa==1||(canfill[color2][vn[0]]&&canfill[color2][vn[1]]))&&(seab==1||canfill[color1][vn[0]]||canfill[color1][vn[1]]))
{
if(sfa-sfb==1)r+=8000*area[i];
else if(sfa-sfb==0)r+=2500*area[i];
else if(sfa-sfb==2)r+=6500*area[i];
}
else
{
tempa=sfa+seaa;
tempb=sfb+seab+seac%2;
if(tempa-tempb==1)r+=8000*area[i];
else if(tempa-tempb==0)r+=2500*area[i];
else if(tempa-tempb==2)r+=6500*area[i];
else if(tempa-tempb==3)r+=3000*area[i];
else if(tempa-tempb==-1)r+=500*area[i];
}
}
else
{
tempa=sfa+seaa;
tempb=sfb+seab+seac%2;
if(tempa-tempb==1)r+=7000*area[i];
else if(tempa-tempb==0)r+=3000*area[i];
else if(tempa-tempb==2)r+=6000*area[i];
else if(tempa-tempb==3)r+=2500*area[i];
else if(tempa-tempb==4)r+=500*area[i];
else if(tempa-tempb==-1)r+=1000*area[i];
}
}
for(i=nationnum+seanum+1;i<=totnum;i++)
{
empty=hilla=hillb=hillc=hilld=hill0=hill1=hill2=hill3=h0=h1=h2=h3=0;
vn.clear();
vt.clear();
for(j=0;j<linknation[i].size();j++)
{
temp=linknation[i][j];
if(map[temp]==-1)
{
empty++;
bool hilldup=0;
if((canfill[color][temp]||canfill[color2][temp])&&(!canfill[color1][temp])&&(!canfill[color3][temp]))hilla++;
else if((canfill[color1][temp]||canfill[color3][temp])&&(!canfill[color][temp])&&(!canfill[color2][temp]))hillb++;
else if((canfill[color1][temp]||canfill[color3][temp])&&(canfill[color][temp]||canfill[color2][temp]))hillc++;
else
{
hilldup=1;
hilld++;
}
if(!hilldup)vn.push_back(temp);
if(canfill[color][temp]&&(!canfill[color1][temp])&&(!canfill[color2][temp])&&(!canfill[color3][temp]))hill0++;
else if(canfill[color1][temp]&&(!canfill[color][temp])&&(!canfill[color2][temp])&&(!canfill[color3][temp]))hill1++;
else if(canfill[color2][temp]&&(!canfill[color][temp])&&(!canfill[color1][temp])&&(!canfill