n=int(input())
a=list(map(int,input().split()))
d={}
for i in a:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
q=int(input())
s=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    if c in d and b in d:
        d[c]+=d[b]
    elif b in d:
        d[c]=d[b]
    if b in d:
        s=s+(c-b)*d[b]
    print(s)
