import collections

_ = input()
a = collections.Counter([x for x in input().rstrip().split()])
_ = input()


try:
    while True:
        l = input().rstrip()
        b, c = l.split(' ')
        a[str(c)] += a[str(b)]
        a[str(b)] = 0

        ans = 0
        for i, n in a.items():
            ans += int(i) * n
        print(ans)

except EOFError:
    pass

