
import numpy as np 
N = int(input())
A = [int(i) for i in input().split(" ")]
Q = int(input())
list_n = np.zeros(10**5)
for a in A:
    list_n[a - 1] += 1
list_num = np.array([i for i in range(1, 10**5+1)])
for i in range(Q):
    B, C = map(int, input().split(" "))
    if list_n[B - 1] != 0:
        tmp = list_n[B - 1]
        list_n[C - 1] += tmp
        list_n[B - 1] = 0
    
    print(int(np.sum(np.dot(list_n, list_num))))
    