N=int(eval(input()))

S=[]

T=[]

for _ in range(N):

    s,t=input().split()

    S.append(s)

    T.append(int(t))

X=eval(input())



ans=0

sleep=False

for s,t in zip(S,T):

    ans+=t if sleep else 0

    if s==X:

        sleep=True

print(ans)
N = int(eval(input()))

st = [tuple(input().split()) for _ in range(N)]

X = eval(input())



ans = 0

for s, t in st:

    ans += int(t)

for s, t in st:

    ans -= int(t)

    if s == X: break

print(ans)
N = int(eval(input()))
S = []
T = []
for _ in range(N):
    s, t = input().split()
    S.append(s)
    T.append(int(t))
X = eval(input())
ans = 0
sleep = False
for s, t in zip(S, T):
    ans += t if sleep else 0
    if s == X:
        sleep = True
print(ans)

N = int(eval(input()))
st = [tuple(input().split()) for _ in range(N)]
X = eval(input())
ans = 0
for s, t in st:
    ans += int(t)
for s, t in st:
    ans -= int(t)
    if s == X:
        break
print(ans)

-S = [] -T = [] -for _ in range(N): -    s, t = input().split() -    S.append(s) -    T.append(int(t)) +st = [tuple(input().split()) for _ in range(N)] -sleep = False -for s, t in zip(S, T): -    ans += t if sleep else 0 +for s, t in st: +    ans += int(t) +for s, t in st: +    ans -= int(t) -        sleep = True +        break