def MI():
    return map(int, input().split())

def LI():
    return list(map(int, input().split()))

# N = int(input())
# group = 0
# tmp = 0

# while tmp < N: 
#     tmp += 26**group
#     group += 1
    
# print(group)

from collections import Counter

N = int(input())
A = LI()
Q = int(input())
A_c = Counter(A)

for _ in range(Q):
    B, C = MI()
    tmp = A_c[B]
    A_c[B] = 0
    
    if B in A_c.keys():
        A_c[C] += tmp
    else:
        A_c[C] = tmp
    
    ans = 0
    
    
    for k, v in A_c.items():
        ans += k * v

    print(ans)