n = int(eval(input()))

d = {}

sum_ = 0

for i in range(n):

  s, t = list(map(str, input().split()))

  t = int(t)

  sum_ += t

  d[s] = sum_

x = str(eval(input()))

print((d[s]-d[x]))
n = int(eval(input()))

S = []

T = []



for i in range(n):

    s, t = list(map(str, input().split()))

    t = int(t)

    S.append(s)

    T.append(t)



x = str(eval(input()))



for i in range(n):

    if S[i] == x:

        j = i



if j == n-1:

    print((0))

else:

    print((sum(T[j+1:])))

n = int(eval(input()))
d = {}
sum_ = 0
for i in range(n):
    s, t = list(map(str, input().split()))
    t = int(t)
    sum_ += t
    d[s] = sum_
x = str(eval(input()))
print((d[s] - d[x]))

n = int(eval(input()))
S = []
T = []
for i in range(n):
    s, t = list(map(str, input().split()))
    t = int(t)
    S.append(s)
    T.append(t)
x = str(eval(input()))
for i in range(n):
    if S[i] == x:
        j = i
if j == n - 1:
    print((0))
else:
    print((sum(T[j + 1 :])))

-d = {} -sum_ = 0 +S = [] +T = [] -    sum_ += t -    d[s] = sum_ +    S.append(s) +    T.append(t) -print((d[s] - d[x])) +for i in range(n): +    if S[i] == x: +        j = i +if j == n - 1: +    print((0)) +else: +    print((sum(T[j + 1 :])))