
from collections import deque

number = int(input())
aaa = list(map(int, input().split()))
dq = deque(aaa)
cntQ = int(input())
for _ in range(cntQ):
    num1, num2 = map(int, input().split())
    if dq.count(num1) != 0:
        for _ in range(dq.count(num1)):
            dq.appendleft(-1 * num1)
            dq.appendleft(num2)
    ans = sum(list(dq))
    print(ans)
