Problem: Problem Statement

Texas hold 'em is one of the standard poker games, originated in Texas, United States. It is played with a standard deck of 52 cards, which has 4 suits (spades, hearts, diamonds and clubs) and 13 ranks (A, K, Q, J and 10-2), without jokers.

With betting aside, a game goes as described below.

At the beginning each player is dealt with two cards face down. These cards are called hole cards or pocket cards, and do not have to be revealed until the showdown. Then the dealer deals three cards face up as community cards, i.e. cards shared by all players. These three cards are called the flop. The flop is followed by another community card called the turn then one more community card called the river.

After the river, the game goes on to the showdown. All players reveal their hole cards at this point. Then each player chooses five out of the seven cards, i.e. their two hole cards and the five community cards, to form a hand. The player forming the strongest hand wins the game. There are ten possible kinds of hands, listed from the strongest to the weakest:

* Royal straight flush: A, K, Q, J and 10 of the same suit. This is a special case of straight flush.
* Straight flush: Five cards in sequence (e.g. 7, 6, 5, 4 and 3) and of the same suit.
* Four of a kind: Four cards of the same rank.
* Full house: Three cards of the same rank, plus a pair of another rank.
* Flush: Five cards of the same suit, but not in sequence.
* Straight: Five cards in sequence, but not of the same suit.
* Three of a kind: Just three cards of the same rank.
* Two pairs: Two cards of the same rank, and two other cards of another same rank.
* One pair: Just a pair of cards (two cards) of the same rank.
* High card: Any other hand.



For the purpose of a sequence, J, Q and K are treated as 11, 12 and 13 respectively. A can be seen as a rank either next above K or next below 2, thus both A-K-Q-J-10 and 5-4-3-2-A are possible (but not 3-2-A-K-Q or the likes).

If more than one player has the same kind of hand, ties are broken by comparing the ranks of the cards. The basic idea is to compare first those forming sets (pairs, triples or quads) then the rest cards one by one from the highest-ranked to the lowest-ranked, until ties are broken. More specifically:

* Royal straight flush: (ties are not broken)
* Straight flush: Compare the highest-ranked card.
* Four of a kind: Compare the four cards, then the remaining one.
* Full house: Compare the three cards, then the pair.
* Flush: Compare all cards one by one.
* Straight: Compare the highest-ranked card.
* Three of a kind: Compare the three cards, then the remaining two.
* Two pairs: Compare the higher-ranked pair, then the lower-ranked, then the last one.
* One pair: Compare the pair, then the remaining three.
* High card: Compare all cards one by one.



The order of the ranks is A, K, Q, J, 10, 9, ..., 2, from the highest to the lowest, except for A next to 2 in a straight regarded as lower than 2. Note that there are exceptional cases where ties remain. Also note that the suits are not considered at all in tie-breaking.

Here are a few examples of comparison (note these are only intended for explanatory purpose; some combinations cannot happen in Texas Hold 'em):

* J-J-J-6-3 and K-K-Q-Q-8.




The former beats the latter since three of a kind is stronger than two pairs.

* J-J-J-6-3 and K-Q-8-8-8.




Since both are three of a kind, the triples are considered first, J and 8 in this case. J is higher, hence the former is a stronger hand. The remaining cards, 6-3 and K-Q, are not considered as the tie is already broken.

* Q-J-8-6-3 and Q-J-8-5-3.




Both are high cards, assuming hands not of a single suit (i.e. flush). The three highest-ranked cards Q-J-8 are the same, so the fourth highest are compared. The former is stronger since 6 is higher than 5.

* 9-9-Q-7-2 and 9-9-J-8-5.




Both are one pair, with the pair of the same rank (9). So the remaining cards, Q-7-2 and J-8-5, are compared from the highest to the lowest, and the former wins as Q is higher than J.

Now suppose you are playing a game of Texas Hold 'em with one opponent, and the hole cards and the flop have already been dealt. You are surprisingly telepathic and able to know the cards the opponent has. Your ability is not, however, as strong as you can predict which the turn and the river will be.

Your task is to write a program that calculates the probability of your winning the game, assuming the turn and the river are chosen uniformly randomly from the remaining cards. You and the opponent always have to choose the hand strongest possible. Ties should be included in the calculation, i.e. should be counted as losses.



Input

The input consists of multiple datasets, each of which has the following format:


YourCard_1 YourCard_2
OpponentCard_1 OpponentCard_2
CommunityCard_1 CommunityCard_2 CommunityCard_3


Each dataset consists of three lines. The first and second lines contain the hole cards of yours and the opponent's respectively. The third line contains the flop, i.e. the first three community cards. These cards are separated by spaces.

Each card is represented by two characters. The first one indicates the suit: `S` (spades), `H` (hearts), `D` (diamonds) or `C` (clubs). The second one indicates the rank: `A`, `K`, `Q`, `J`, `T` (10) or `9`-`2`.

The end of the input is indicated by a line with `#`. This should not be processed.

Output

Print the probability in a line. The number may contain an arbitrary number of digits after the decimal point, but should not contain an absolute error greater than 10^{-6}.

Examples

Input

SA SK
DA CA
SQ SJ ST
SA HA
D2 C3
H4 S5 DA
HA D9
H6 C9
H3 H4 H5
#


Output

1.00000000000000000000
0.34444444444444444198
0.63030303030303025391


Input

SA SK
DA CA
SQ SJ ST
SA HA
D2 C3
H4 S5 DA
HA D9
H6 C9
H3 H4 H5


Output

1.00000000000000000000
0.34444444444444444198
0.63030303030303025391

Solution (in CPP):

Solution:
#include <cstdio>
#include <vector>
#include <cassert>
#include <cstring>
#include <algorithm>

#define rep(i,n) for(int i=0;i<(n);i++)

using namespace std;

const char *SUIT="SHDC";

struct card{
	int num;
	char suit;
	void parse(const char *s){
		suit=s[0];
		if     (s[1]=='A') num=1;
		else if(s[1]=='T') num=10;
		else if(s[1]=='J') num=11;
		else if(s[1]=='Q') num=12;
		else if(s[1]=='K') num=13;
		else               num=s[1]-'0';
	}
	bool operator==(const card &C)const{ return num==C.num && suit==C.suit; }
	bool operator<(const card &C)const{ return (num==1?100:num)>(C.num==1?100:C.num); }
};

struct hand{
	int rank;
	card c[5];
	vector<int> seq;
	bool operator<(const hand &h)const{
		if(rank!=h.rank) return rank>h.rank;
		else{
			rep(i,5) if(seq[i]!=h.seq[i]) {
				return (seq[i]==1?100:seq[i])>(h.seq[i]==1?100:h.seq[i]);
			}
			return false;
		}
	}
};

void parse(int n,const char *s,card *res){
	int len=strlen(s);
	int m=0,pre=0;
	rep(i,len+1) if(i==len || s[i]==' ') {
		res[m++].parse(s+pre);
		pre=i+1;
	}
}

bool isflush(const card *C){
	return C[0].suit==C[1].suit
		&& C[0].suit==C[2].suit
		&& C[0].suit==C[3].suit
		&& C[0].suit==C[4].suit;
}

bool isstraight(const card *C){
	if(C[0].num==1){
		if(C[1].num==13 && C[2].num==12 && C[3].num==11 && C[4].num==10) return true;
		if(C[1].num== 5 && C[2].num== 4 && C[3].num== 3 && C[4].num== 2) return true;
		return false;
	}
	else{
		return C[0].num==C[1].num+1 && C[1].num==C[2].num+1 && C[2].num==C[3].num+1 && C[3].num==C[4].num+1;
	}
}

int maxfreq(const card *C){
	int freq[14]={};
	rep(i,5){
		freq[C[i].num]++;
	}
	return *max_element(freq,freq+14);
}

hand calc_hand(card *C){
	sort(C,C+5);
	hand h;
	rep(i,5) h.seq.push_back(C[i].num);

	// royal straight flush
	if(isflush(C) && isstraight(C) && C[0].num==1 && C[1].num==13){
		h.rank=10;
	}
	// straight flush
	else if(isflush(C) && isstraight(C)){
		h.rank=9;
		if(C[0].num==1){
			h.seq[0]=5;
			h.seq[1]=4;
			h.seq[2]=3;
			h.seq[3]=2;
			h.seq[4]=1;
		}
	}
	// four a kind
	else if(maxfreq(C)==4){
		h.rank=8;
		if(C[0].num!=C[1].num){
			swap(h.seq[0],h.seq[4]);
		}
	}
	// full house
	else if(C[0].num==C[1].num && C[3].num==C[4].num && (C[2].num==C[0].num || C[2].num==C[3].num)){
		h.rank=7;
		if(C[2].num==C[3].num){
			swap(h.seq[0],h.seq[3]);
			swap(h.seq[1],h.seq[4]);
		}
	}
	// flush
	else if(isflush(C)){
		h.rank=6;
	}
	// straight
	else if(isstraight(C)){
		h.rank=5;
		if(C[0].num==1 && C[1].num!=13){
			h.seq[0]=5;
			h.seq[1]=4;
			h.seq[2]=3;
			h.seq[3]=2;
			h.seq[4]=1;
		}
	}
	// three of a kind
	else if(maxfreq(C)==3){
		h.rank=4;
		if     (C[0].num==C[1].num && C[0].num==C[2].num);
		else if(C[1].num==C[2].num && C[1].num==C[3].num) swap(h.seq[0],h.seq[3]);
		else if(C[2].num==C[3].num && C[2].num==C[4].num) swap(h.seq[0],h.seq[3]), swap(h.seq[1],h.seq[4]);
		else assert(0);
	}
	// two pairs
	else if(C[0].num==C[1].num && C[2].num==C[3].num
	     || C[0].num==C[1].num && C[3].num==C[4].num
	     || C[1].num==C[2].num && C[3].num==C[4].num){
		h.rank=3;
		if(C[0].num==C[1].num && C[2].num==C[3].num);
		else if(C[0].num==C[1].num && C[3].num==C[4].num){
			swap(h.seq[2],h.seq[4]);
		}
		else{
			swap(h.seq[0],h.seq[2]);
			swap(h.seq[2],h.seq[4]);
		}
	}
	// one pair
	else if(maxfreq(C)==2){
		h.rank=2;
		if     (C[0].num==C[1].num);
		else if(C[1].num==C[2].num) swap(h.seq[0],h.seq[2]);
		else if(C[2].num==C[3].num) swap(h.seq[0],h.seq[2]), swap(h.seq[1],h.seq[3]);
		else if(C[3].num==C[4].num) swap(h.seq[0],h.seq[3]), swap(h.seq[1],h.seq[4]), swap(h.seq[2],h.seq[3]), swap(h.seq[3],h.seq[4]);
		else assert(0);
	}
	// high card
	else{
		h.rank=1;
	}

	return h;
}

hand get_best_hand(const card *C,const card *flop,card turn,card river){
	hand res; res.rank=-1;

	card c[7]={C[0],C[1],flop[0],flop[1],flop[2],turn,river};

	for(int S=(1<<5)-1,x,y;S&&S<(1<<7);x=S&-S,y=S+x,S=((S&~y)/x>>1)|y){
		int m=0;
		card chosen[5];
		rep(k,7) if(S>>k&1) chosen[m++]=c[k];
		res=min(res,calc_hand(chosen));

		if(S==0) break;
	}

	return res;
}

int main(){
	while(1){
		char s1[10],s2[10],s3[10];
		fgets(s1,10,stdin);
		fgets(s2,10,stdin);
		fgets(s3,10,stdin);
		if(s1[0]=='#') break;

		card C1[2],C2[2],flop[3];
		parse(2,s1,C1);
		parse(2,s2,C2);
		parse(3,s3,flop);

		vector<card> yama;
		for(int i=1;i<=13;i++) rep(j,4) {
			card c={i,SUIT[j]};

			bool ok=true;
			if(C1[0]==c || C1[1]==c) ok=false;
			if(C2[0]==c || C2[1]==c) ok=false;
			if(flop[0]==c || flop[1]==c || flop[2]==c) ok=false;

			if(ok) yama.push_back(c);
		}

		int win=0;
		rep(i,yama.size()) rep(j,i) {
			hand h1=get_best_hand(C1,flop,yama[i],yama[j]);
			hand h2=get_best_hand(C2,flop,yama[i],yama[j]);
			if(h1<h2) win++;
		}
		printf("%.15f\n",(double)win/(yama.size()*(yama.size()-1)/2));
	}

	return 0;
}