n=int(input())
a=list(map(int, input().split()))
q=int(input())
num=[0]*(10**5-1)
tot=0
for i in range(n):
    num[a[i]-1]+=1
    tot+=a[i]

for i in range(q):
    b,c=map(int, input().split())
    tot+=num[b-1]*(c-b)
    print(tot)
    num[c-1]+=num[b-1]
    num[b-1]=0