n=int(input())
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
    dic.setdefault(a[i],1)
    dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)