N=int(input())
A=list(map(int,input().split()))
Q=int(input())
P=[]
for i in range(5):
    a,b=input().split()
    list.append((int(a), int(b))
a=len(A)
for j in range(M):
    for i in range(i):
        if P[i]==B[j][0]:
                P[i]=B[j][1]
    print(sum(P))
    