from collections import Counter
import numpy as np

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
s = Counter(A)
t=[0]*(10**5+1)
for i in range(10**5+1):
    t[i] = s[i]*i
    
for i in range(Q):
    B, C = map(int, input().split())
    num = t[B]//(B)
    t[B] = 0
    t[C] += num*(C)
    print(sum(t))
