n = int(input())
a = [int(x) for x in input().split()]
q = int(input())
b = [0]*q
c = [0]*q
for i in range(q):
    bc = input().split()
    b[i] = int(bc[0])
    c[i] = int(bc[1])
a_max = max(a+c)
cnt = [0]*a_max
sum = 0
for i in range(a_max):
    m = i + 1
    cnt[i] = a.count(m)
    sum += m*cnt[i]
for i in range(q):
    sum += (c[i]-b[i])*cnt[b[i]-1] 
    cnt[c[i]-1] += cnt[b[i]-1]
    cnt[b[i]-1] = 0

    print(sum)