N = int(input())
A = [int(n) for n in input().split()]
Q = int(input())

D = dict()
for a in A:
    if a not in D.keys():
        D[a] = 0
    D[a] += 1

for _ in range(Q):
    B, C = [int(n) for n in input().split()]
    
    if B in D.keys():
        if C not in D.keys():
            D[C] = 0
        D[C] += D[B]
        del D[B]

    ans = 0
    for k, v in D.items():
        ans += k * v
    print(ans)

