n=int(input())
a=list(map(int,input().split()))
q=int(input())
bc = [list(map(int,input().split())) for _ in range(q)]
N=[0]*(10**5+1)
for i in a:
    N[i]+=1
for b,c in bc:
    N[c] += N[b]
    N[b] = 0
    print(sum([i*x for i,x in enumerate(N) if x != 0]))
