module Main where main :: IO () main = do i <- readLn s <- getContents mapM_ (putStrLn . isPossible) . take i . lines $ s -- | tests -- >>> isPossible "WWWWWWWWWGR" -- "possible" -- >>> isPossible "WWWWGWGRR" -- "possible" -- >>> isPossible "WWWWWWWWWWWWWWGRRG" -- "impossible" -- >>> isPossible "WGRWGR" -- "possible" -- >>> isPossible "WWWWWWWWWWWWWWWWWGGWGWGGWGWGGGWGG" -- "impossible" -- >>> WGWR -- "impossible" -- >>> WGRW -- "impossible" isPossible :: String -> String isPossible s = if take1RGW s then "possible" else "impossible" where take1RGW = takeR . reverse takeR :: String -> Bool takeR ('R':'G':ls) = length ls > 0 && all (=='W') ls || takeG ('G':ls) takeR ('R':xs) = takeG xs takeR _ = False takeG xs = if elem 'R' xs then case break (=='G') xs of (_, "") -> False (ys, (_:zs)) -> takeW (ys ++ zs) else head xs == 'G' && length xs >= 2 && all (=='W') xs takeW xs | elem 'W' xs = let (as, (_:bs)) = break (=='W') xs in takeR $ as ++ bs | otherwise = False