import sys
from collections import Counter
N=int(sys.stdin.readline().strip())
A=map(int, sys.stdin.readline().split())
Q=int(sys.stdin.readline().strip())

KOSUU=Counter(A)
total=sum(A)
for _ in range(Q):
    b,c=map(int, sys.stdin.readline().split())
    total=total-b*KOSUU[b]+c*KOSUU[b]
    print total
    KOSUU[c]+=KOSUU[b]
    KOSUU[b]=0
    