n = int(input())
target = list(map(int, input().split()))
lst = [list(map(int, input().split())) for i in range(int(input()))]
a = []
for l in lst:
    target = [l[1] if t == l[0] else t for t in target]
    print(sum(target))