import Data.List
import Data.Maybe
import Control.Applicative

main = interact $ unlines . map (maybe "-1" show . kakezan . read) . tail . lines

kakezan :: Int -> Maybe Int
kakezan n = kak [n] n
 where
  kak (a:acc) n'
   | n' <= 9 = Just 0
   | elem n' acc = Nothing
   | otherwise = succ <$> kak (nummax:acc) nummax
   where nummax = maximum [read s1 * read s2 | let s = show n', i <- [1 .. length s - 1], let (s1,s2) = splitAt i s]