/* Add suits to a list of unsuited hands in such a way that the equity of the
   worst hand must be more than 1/3-EPS */
/* A.P.Selby Oct 1999 */
#include <stdio.h>
#include <string.h>
#define EPS 0.0005
/* EPS 0.0005 guarantees not missing any triple with difference <0.075% */
/* EPS -0.02 makes a shorter list, by hoping that flushes don't make as much difference as they might */
/* EPS -0.03 even shorter ... */
#define EPS1 -100
#define swap(x,y) {int zz=x; x=y; y=zz;}
static char *cc="23456789TJQKA",*su="cdhs";
static int c2n(char c){return strchr(cc,c)-cc;}
main(){
int i,j,k,r,s,t,v,n0,n1,pp,no[8],mr[4096],pe[4],nn,or[8][4096][6],
  tt[52],cn[6],sm[8][4096][4],hf,ta[6],isc[3];
char hd[3][10];
double w,x,y,z,ub0[5],ub1[5],ub2[5],sc[3],bs,cs;
 
/* pp says which hands of the triple are pairs */
/* This calculates all the essentially different ways to make a triple suited */
for(pp=0;pp<8;pp++){
 for(s=0;s<4096;s++)mr[s]=1;
 for(s=0,nn=0;s<4096;s++){
  if(mr[s]==0)goto nl0;
  for(j=0;j<3;j++)if((pp>>j&1)&&(s>>4*j&3)>=(s>>4*j+2&3))goto nl0;
  /* for(j=5;j>=0;j--)printf("%c",su[s>>2*j&3]);printf("\n"); */
  for(j=0;j<6;j++)or[pp][nn][j]=s>>2*j&3;
  for(j=0;j<4;j++)sm[pp][nn][j]=0;
  for(j=0;j<6;j++)sm[pp][nn][or[pp][nn][j]]++;
  nn++;
  for(i=0;i<24;i++){
   for(j=0;j<4;j++)pe[j]=j;
   for(j=0,k=i;j<4;j++){swap(pe[j],pe[j+k%(4-j)]);k/=4-j;}
   for(j=0;j<6;j++)ta[j]=pe[(s>>2*j)&3];
   for(j=0;j<3;j++)if((pp>>j&1)&&ta[2*j]>ta[2*j+1])swap(ta[2*j],ta[2*j+1]);
   for(j=0,t=0;j<6;j++)t|=ta[j]<<2*j;
   if(t<s)exit(printf("Bad\n"));
   if(t>s)mr[t]=0;
  }
 nl0:
 }
 no[pp]=nn;
fprintf(stderr,"Pair pattern %d%d%d    %d orbits\n",pp&1,pp>>1&1,pp>>2&1,nn);
}

/* This calculates an upper bound for the probability of a flush in the
   various situations (whether or not you are suited, whether or not you
   are high-carded, how many of your outs are dead) */
z=46*45*44*43*42/120.;
for(r=0;r<=4;r++){
 x=(12-r)*(11-r)*(10-r)*(9-r);
 y=x*(8-r);
 ub0[r]=(x/24*(34+r)+y/120)/z;
 ub1[r]=(5*(34+r)+y/120/2)/z;
 x=(11-r)*(10-r)*(9-r);
 y=x*(8-r);
 ub2[r]=(x/6*(35+r)*(34+r)/2.+y/24*(35+r)+y*(7-r)/120.)/z;
}

/* This adds suits to the hand triples in all ways which could possibly make the
   equity of the worst hand >= 1/3-EPS */
n0=0;n1=0;
while(scanf("%s %s %s %d %d %d %d\n",hd[0],hd[1],hd[2],&t,&isc[0],&isc[1],&isc[2])==7){
 n0++;
 for(i=0,t=0;i<3;i++)t+=isc[i];if(t!=6*1370754)exit(printf("Zog %d\n",t));
 for(i=0;i<3;i++)sc[i]=isc[i]/(6*1370754.);
 for(i=0;i<3;i++)if(sc[i]+ub2[0]<1/3.-EPS)goto nl1;
 for(i=0,pp=0;i<3;i++){cn[i*2]=c2n(hd[i][0]);cn[i*2+1]=c2n(hd[i][1]);pp|=(cn[i*2]==cn[i*2+1])<<i;}
 for(i=0;i<no[pp];i++){
  bs=-99;
  for(j=0;j<52;j++)tt[j]=0;
  for(j=0;j<6;j++){k=cn[j]<<2|or[pp][i][j];if(tt[k])goto nl2;tt[k]=1;}
  for(j=0;j<3;j++){
   if(or[pp][i][2*j]==or[pp][i][2*j+1]){r=sm[pp][i][or[pp][i][2*j]]-2;x=ub2[r];}
   else{
    for(k=0,x=0;k<2;k++){
     s=or[pp][i][2*j+k];v=cn[2*j+k];
     r=sm[pp][i][s]-1;
     for(t=0,hf=1;t<6&&hf;t++)if(or[pp][i][t]==s&&cn[t]>v)hf=0;
     if(hf)x+=ub0[r]; else x+=ub1[r];
    }
   }
   cs=1/3.-(sc[j]+x);
   if(EPS<cs)goto nl2;
   if(cs>bs)bs=cs;
  }
  if(bs<EPS1)goto nl2;
  for(j=0;j<3;j++){
   printf("%c%c%c%c",hd[j][0],su[or[pp][i][j*2]],hd[j][1],su[or[pp][i][j*2+1]]);
   if(j<2)printf(" ");
  }
  printf("\n");
  n1++;
 nl2:
 }/* for i */
 nl1:
}/* while */

fprintf(stderr,"Read %d lines\nWrote %d lines\n",n0,n1);
}
