n = int(input())
lis = list(map(int,input().split()))
q = int(input())
x = {}
for i in lis:
    if i not in x:
        x[i]=1
    else:
        x[i]+=1
total = sum(lis)
for i in range(q):
    a,b=map(int,input().split())
    diff = b-a
    total += diff*x[a]
    x[b]+=x[a]
    print(total)