import Data.List
import Data.Maybe
import Control.Applicative ((<$>))
data DataBase = DB { pid :: Int
                   , ans :: Int }

main = do
    dbs <- getData
    let rank = zip (nub.reverse.sort.map ans $ dbs) [1..]
    rankChecker rank dbs

getData :: IO [DataBase]
getData = do
    [p,s] <- map read.splitByComma<$>getLine
    if p == 0 && s == 0 then return []
                        else do
                            dbs <- getData
                            return $ (DB p s) : dbs

splitByComma :: String -> [String]
splitByComma xs
    | rest == "" = pre : []
    | otherwise = pre : splitByComma (tail rest)
    where
        (pre, rest) = break (== ',') xs

rankChecker :: [(Int, Int)] -> [DataBase] -> IO ()
rankChecker ranks dbs = do
    ids <- fmap ((map read).lines) getContents
    let pts = concat.map (\pID -> map ans.filter (\x -> pid x == pID) $ dbs) $ ids
        rank = map (fromMaybe 0.(\x -> lookup x ranks)) pts
    mapM_ print rank