import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

n=int(input())
a=list(map(int,input().split()))
q=int(input())

ans=sum(a)

x=[0]*(10**5)
for i in a:
    x[i]+=1


for _ in range(q):
    b,c=map(int,input().split())
    ans+=x[b]*c-x[b]*b
    print(ans)
    x[c]+=x[b]
    x[b]=0