from collections import Counter
N = int(input())
A_list = list(map(int, input().split()))
Q = int(input())
BC_list = [list(map(int, input().split())) for i in range(Q)]

count_dict = dict(Counter(A_list))

for b, c in BC_list:
    if b in count_dict:
        if c in count_dict:
            count_dict[c] += count_dict[b]
        else:
            count_dict[c] = count_dict[b]
        count_dict.pop(b)
    _sum = 0
    for i, a in count_dict.items():
        _sum += i * a
    print(_sum)