import collections
n=int(input())
a=[int(i) for i in input().split()]

c = collections.Counter(a)
suma=sum(a)
q=int(input())
for i in range(q):
    x,y=map(int,input().split())
    print(suma+c[x]*(y-x))
    suma+=c[x]*(y-x)
    c[y]+=c[x]
    c[x]=0