s=set()
l = [0] * ( 10 ** 5 + 1 )
N = int(input())
A = list(map(int, input().split()))
for i in range(len(A)):
    l[A[i]] += 1
    s.add(A[i])
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    if l[B] > 0:
        s.add(C)
        l[C] += l[B]
        l[B] = 0
        s.discard(B)
    sum=0
    for i in s:
        sum += i * l[i]
    print(sum)
