import collections
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = [0]*q
c = [0]*q
for i in range(q):
    b[i], c[i] = map(int, input().split())

counter = dict(collections.Counter(a))
for i in range(q):
    s = 0
    try:
        counter[c[i]] += counter.get(b[i])
    except:
        counter.update({c[i]: int(counter.get(b[i])or 0)} )
    counter[b[i]] = 0    
    for key, val in counter.items():
        s += key * val
    print(s)