import sys

def input():
    return sys.stdin.readline()[:-1]

N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)
Q = int(input())
BC = []
counter = [0]*10**5
for i in range(Q):
    b, c = map(int, input().split())
    b_count = A.count(b)
    c_count = A.count(c)
    BC.append([b, c])
    counter[b] = b_count
    counter[c] = c_count
    
for b, c in BC:
    A_sum += (c-b)*counter[b]
    counter[c] += counter[b]
    print(A_sum)
