import Numeric import Data.Char import Control.Monad intToChar :: Int -> Char intToChar x | 0 <= x && x <= 9 = chr $ ord '0' + x | 10 <= x && x <= 35 = chr $ ord 'A' - 10 + x charToInt :: Char -> Int charToInt c | '0' <= c && c <= '9' = ord c - ord '0' | 'A' <= c && c <= 'Z' = ord c - ord 'A' + 10 toDecimal :: Integral a => Int -> String -> a toDecimal fr fx = sum $ zipWith (\a b -> a * fr' ^ b) fx' $ reverse [0..length fx' - 1] where fx' = dropWhile (== 0) $ map (fromIntegral . charToInt) fx fr' = fromIntegral fr solve :: Integral a => [String] -> a solve xs = minimum $ zipWith toDecimal minrs xs where minrs = map ((+ 1) . charToInt . maximum) xs main = do n <- readLn vs <- replicateM n getLine print $ solve vs