import collections

n=int(input())
a=map(int,input().split())
q=int(input())
b=[[int(i) for i in input().split()] for l in range(q)]

c=collections.Counter(a)

for x in b:
    c[x[0]],c[x[1]]=0,c[x[0]]+c[x[1]]
    print(sum([j*k for j,k in c.items()]))