import Data.Bool import Data.List last' s = if null s then "" else last s main = getLine >> interact (unlines . map (bool "impossible" "possible" . citybus) . lines) citybus s = and [num, restR, restG, pairGR] where num = let [g,r,w] = map (pred.length) (group (sort ("WGR"++s))) in g == r && w >= g restR = (=="R") $ last' $ filter (\(b:_) -> b=='R') $ delete [] $ tails s restG = (notElem 'W') $ last' $ filter (\(b:_) -> b=='G') $ delete [] $ tails s pairGR = foldr pairGRgo (Just 0) s == Just 0 pairGRgo _ Nothing = Nothing pairGRgo 'R' (Just n) = Just (n + 1) pairGRgo 'G' (Just n) | n == 0 = Nothing | otherwise = Just (n - 1) pairGRgo _ m = m