n = int(input())
A = list(map(int, input().split())) 
q = int(input())

arr = [0] * (10**5+1)
# arr = [i for i in range(1, 10**5+1)]

for i in A:
    arr[i] += i

for _ in range(q):
    b, c = map(int, input().split())
    arr[c] += (arr[b]//b*c)
    arr[b] = 0

    print(sum(arr))
