n = int(input())
aa = tuple(map(int, input().split()))
q = int(input())

tb = {}
for i in range(q):
    b, c = map(int, input().split())
    tb[b] = c
    for k, v in tb.items():
        if v == b:
            tb[k] = c

    ans = 0
    for i in range(n):
        ans += tb.get(aa[i], aa[i])
    print(ans)

