n = int(input())
a = list(map(int,input().split()))
t = [0]*(10**5+1)
s = 0
for i in range(n):
    t[a[i]] += 1
    s += a[i]
q = int(input())
for j in range(q):
    b,c = map(int,input().split())
    s += (c-b)*t[b]
    t[c] += t[b]
    t[b] *= 0
    print(s)