N = int(input())
A = list(map(int, input().split()))
A_set = set(A)
Q = int(input())
dis_list = []
count = {}
temp_sum = 0
for a in A_set:
    count[a] = A.count(a)
    temp_sum += a * count[a]
for i in range(Q):
    s = list(map(int, input().split()))
    dis_list.append(s)
for j in dis_list:
    if j[0] in count:
        temp_sum += (j[1] - j[0])* count[j[0]]
        if j[1]in count:
            count[j[1]] += count[j[0]]
        else:
            count[j[1]] = count[j[0]]
        count[j[0]] = 0
    print(temp_sum)