n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l = [0] * (10 ** 5)
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    l[i]+=1
for (f,t) in move:
    l[t]+=l[f]
    l[f]=0
    sum = 0
    for i in range(10**5):
        sum += i*l[i]
    print(sum)