n = int(input())
m=list(map(int,input().split()))
d={}
k=0
for i in range(n):
    k+=m[i]
    if m[i] in d:
        d[m[i]]+=1
    else:
        d[m[i]]=1
for x in range(int(input())):
    
    b,c=list(map(int,input().split()))

    k+=d[b]*(c-b)
    
    if c in d:
        d[c]+=d[b]
    else:
        d[c] = d[b]
    del d[b] 
    print(k)