import Data.List
import Data.Ord
import Data.Maybe

main = interact $ unlines . show' . rank_checker . read' . lines
 where
  read' xs = let (ranks,(_:reqs)) = span (/= "0,0") xs in (map parse_rank ranks, map read reqs)
   where parse_rank r = let (n,(_:p)) = span (/= ',') r in (read n, read p)
  show' = map (show . succ . fromJust)

rank_checker :: ([(Int,Int)],[Int]) -> [Maybe Int]
rank_checker (ranks,reqs) = map (query_rank (groupBy equal_snd $ reverse $ sortBy (comparing snd) ranks)) reqs
 where
  equal_snd (x1,x2) (y1,y2) = x2 == y2
  query_rank ranklist req = findIndex (\samerank -> elem req (map fst samerank)) ranklist