import sys,collections
input = sys.stdin.readline

N=input().rstrip()
A=list(map(int,input().split()))
Ac = collections.Counter(A)
Q = int(input())
for i in range(Q):
    B,C = list(map(int,input().split()))
    Ac[C] += Ac[B]
    Ac[B] = 0

    ans = 0
    for key,val in Ac.items():
        ans += key*val
    print(ans)
