import Control.Applicative ((<$>)) import Control.Monad (replicateM) import Data.List (foldl') import Data.Char (digitToInt, ord) main :: IO () main = do n <- readLn solve <$> replicateM n getLine >>= print solve :: [String] -> Int solve ss = minimum . map f $ ss where f s = foldl' (\x y -> n * x + y) 0 ls where ls = map toNum s n = 1 + maximum ls toNum c | c <= '9' = digitToInt c | otherwise = ord c - 55