import Control.Monad

main :: IO ()
main = do
  n <- readLn
  loop 1 n
loop :: Int -> Int -> IO ()
loop count lim = do
  if count > lim
    then return ()
    else do
      ls <- getLine
      let ans = randList' ls
      putStrLn $ "Case " ++ (show count) ++ ":"
      mapM_  putStrLn ans
      loop (count + 1) lim

strToInt :: String -> Int
strToInt str = read str

format :: Int -> String
format i = (replicate (8 - (length str)) '0') ++ str
  where str = show i

takeRange :: Int -> Int -> [a] -> [a]
takeRange 0 j ss = take j ss
takeRange i j (s:ss)
  |(i >= 0 && j >= 0 && i <= j) = takeRange (i-1) (j-1) ss
  | otherwise = []

randNex :: String -> String
randNex s = show (strToInt (takeRange 2 6 (format (i*i))))
  where i = strToInt s

randList :: String -> [String] -> [String]
randList s ss
  | length ss == 10 = ss
  | otherwise = randList (randNex s) (ss ++ [randNex s])
randList' :: String -> [String]
randList' s = randList s []