import sys
import collections
input = lambda: sys.stdin.readline().rstrip()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    q = int(input())
    ans = []
    a = dict(collections.Counter(a))
    for _ in range(q):
        tmp = 0
        b, c = map(int, input().split())
        if b in a.keys():
            if c in a.keys():
                a[c] = a[b] + a[c]
            else:
                a[c] = a[b]
            del a[b]
        for key, value in a.items():
            tmp += key * value
        ans.append(tmp)
    print(ans)

if __name__ == '__main__':
    main()