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" -- >>> isPossible "WGWR" -- "impossible" -- >>> isPossible "WGRW" -- "impossible" isPossible :: String -> String isPossible s = if take1RGW s then "possible" else "impossible" where take1RGW xs@(_:_:_:_) | dropWhile (=='W') xs == "GR" = True | otherwise = takeW xs take1RGW _ = False takeW ('W':xs) = takeG xs takeW _ = False takeX c ls f | c `elem` ls = let (as, bs) = break (==c) ls in if null bs then False else f (as ++ tail bs) | otherwise = False takeG xs = takeX 'G' xs takeR takeR xs = takeX 'R' xs take1RGW