n = int(input())
als = list(map(int,input().split()))
ans = sorted(als)
q = int(input())
for i in range(q):
    b, c = map(int,input().split())
    if b in ans:
        for j in range(n):
            if ans[j] == b:
                ans[j] = c
    print(sum(ans))