let n = stdin.ReadLine() |> int
let st = Array.init n (fun _ -> stdin.ReadLine().Split())
let x = stdin.ReadLine()

let s = [|for i in 0..(n - 1) -> st.[i].[0]|]
let t = [|for i in 0..(n - 1) -> (st.[i].[1] |> int)|]

let rec f (arr:string[]) (ls:int list) a =
 match ls with
 |[] -> 0
 |hd :: tl -> if (arr.[hd] = a) then hd else (f arr tl a)

let m = f s [0..(n - 1)] x

[0..(n - 1)]
|>List.sumBy (fun p -> if (p > m) then t.[p] else 0)
|>stdout.WriteLine