import Control.Arrow
import Control.Monad
import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Char
import Data.Ord
import Data.Function
import Control.Monad.State (State, evalState, runState, put, gets, modify)
import Data.Map.Strict (Map, lookup, insert, empty)

main :: IO ()
main = readContents 2 >>= putStrLn . solve

solve :: [[Int]] -> String
solve [[n, m], as] = build . calc n m $ candidates as

build :: [Int] -> String
build = map (\x -> chr $ zero + x)
    where
        zero = ord '0'

calc :: Int -> Int -> [(Int, Int)] -> [Int]
calc n m xs = evalState (choose n xs) empty
    where
        choose i (x@(a, c) : ys) = check >>= next
            where
                check = (==) <$> res1 <*> res2
                res1 = f (i - c, x : ys)
                res2 = pred <$> f (i, x : ys)
                next b 
                    | b = (a :) <$> choose (i - c) (x : ys)
                    | otherwise = choose i ys
        choose _ _ = return []

        f = memoizeFix memo 

type Key = (Int, [(Int, Int)])

memo :: Memoizer Key Int -> Memoizer Key Int
memo f (i, x@(_, c) : xs)
    | i > 0 = max <$> next1 <*> next2
    | i < 0 = return ninf
    | otherwise = return 0
    where
        next1 = succ <$> f (i - c, x : xs)
        next2 = f (i, xs)
memo _ _ = return ninf

ninf :: Int
ninf = -100000

candidates :: [Int] -> [(Int, Int)]
candidates = sortBy (flip cmpFst) . map head . groupBy ((==) `on` snd) . sortBy cmpf . map acf
    where
        acf n 
            | n == 1 = (n, 2)
            | n `elem` [2, 3, 5] = (n, 5)
            | n == 4 = (n, 4)
            | n `elem` [6, 9] = (n, 6)
            | n == 7 = (n, 3)
            | otherwise = (n, 7)

        cmpf lhs rhs = comparing snd lhs rhs `mappend` cmpFst rhs lhs

        cmpFst = comparing fst

type Memoizer a b = a -> State (Map a b) b

evalMemo :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> b
evalMemo f = flip evalState empty . memoizeFix f

runMemo :: (Ord a) => (Memoizer a b -> Memoizer a b) -> a -> (b, Map a b)
runMemo f = flip runState empty . memoizeFix f

memoizeFix :: (Ord a) => (Memoizer a b -> Memoizer a b) -> Memoizer a b
memoizeFix f x = gets (Data.Map.Strict.lookup x) >>= maybe (f (memoizeFix f) x >>= ((<$) <*> (modify . Data.Map.Strict.insert x))) return

readLine :: IO [Int]
readLine = unfoldr (fmap (second (maybe B.empty snd . B.uncons)) . B.readInt) <$> B.getLine

readContents :: Int -> IO [[Int]]
readContents = flip replicateM readLine