import numpy as np
n = int(input())
a = list(map(int,input().split()))
a = np.array(a)
q = int(input())
import collections
co = collections.Counter(a)
# print(co)
for i in range(q):
    b,c = map(int,input().split())
    if b in co.keys():
        co[c]+=co[b]
        co.pop(b)
    s=0
    for k,v in co.items():
        s+=k*v
    print(s)