N = int(input())
A = list(map(int, input().split()))
Q = int(input())

dic = {}
for num in A:
    if num in dic:
        dic[num] += 1
    else:
        dic[num] = 1

for _ in range(Q):
    total = 0
    B, C = map(int, input().split())
    if B in dic and C in dic:
        dic[C] += dic[B]
        del dic[B]
    elif B in dic and C not in dic:
        dic[C] = dic[B]
        del dic[B]
    for mykey, myvalue in dic.items():
        total += mykey * myvalue
    print(total)
