N=int(input())
A=list(map(int,input().split()))
Q=int(input())
L=[0]*(10**5)
for i in A:L[i-1]+=i
for i in range(Q):
    B,C=map(int,input().split())
    L[C-1]+=(L[B-1]//B)*C;L[B-1]=0
    print(sum(L))