n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = 0
c = 0
ans = sum(a)
for i in range(q):
    b, c = map(int, input().split())
    b_num = a.count(b)
    ans += b_num*(c-b)
    for j in range(b_num):
        a.remove(b)
        a.append(c)
    print(ans)