import collections 
_ = input()
a = [*map(int, input().split())]
q = int(input())
b = [[*map(int,input().split())] for _ in range(q)]
c = collections.Counter(a)
for i,j in b:
    c[j]+=c[i]
    c.pop(i)
    print(sum(k*v for k,v in c.items()))