import Control.Monad import Control.Applicative import Data.List main = do t <- readLn x <- map words <$> replicateM t getLine putStrLn . show $ solve [0..9] [0..9] (map toTuple x) get1st (a,_,_,_,_) = a solve :: [Int] -> [Int] -> [(Int,Int,Int,Int,String)] -> Int solve (x:[]) _ _ = x solve _ (x:[]) _ = x solve ns as ((a,b,c,d,r):xs) | r == "NO" = solve (ns \\ [a,b,c,d]) as xs | r == "YES" = solve [a,b,c,d] (intersect as [a,b,c,d]) xs toTuple :: [String] -> (Int,Int,Int,Int,String) toTuple (a:b:c:d:e:[]) = (read a,read b,read c,read d,e)