import Data.List

readPS :: [(Int,Int)]->IO [(Int,Int)]
readPS lst = do
    line <- getLine
    let (p,s) = read ("("++line++")")::(Int,Int)
    if (p,s)==(0,0) then return lst else return =<< readPS ((p,s):lst)

readQ :: IO [Int]
readQ = do
    str <- getContents
    return $ map (\x->read x::Int) $ lines $ str

score :: [(Int,Int)]->Int->Int
score ps p = snd $ head $ filter (\x->(fst x)==p) ps

index :: [(Int,Int)]->Int->Int
index ps s = (length $ takeWhile (/=s) s') + 1
    where s' = reverse $ sort $ nub $ map snd ps 

main :: IO ()
main = do
    ps <- readPS []
    q <- readQ
    mapM_ print $ map (index ps . score ps) q