n=int(input())
a=list(map(int,input().split()))
b=[0 for i in range(10**5)]
s=0
for i in range(n):
    b[a[i]-1]+=1
    s+=a[i]
q=int(input())
for i in range(q):
    c,d=map(int,input().split())
    s+=(d-c)*b[c-1]
    b[d-1]+=b[c-1]
    b[c-1]=0
    print(s)