import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
q = int(input())
info = []
for i in range(q):
    info.append(list(map(int, input().split())))

a_dict = dict()
for num in a:
    if num in a_dict.keys():
        a_dict[num] += 1
    else:
        a_dict[num] = 1

for j in range(q):
    if info[j][0] in a_dict.keys():
        if info[j][1] in a_dict.keys():
            a_dict[info[j][1]] += a_dict[info[j][0]]
            del a_dict[info[j][0]]
        else:
            a_dict[info[j][1]] = a_dict[info[j][0]]
            del a_dict[info[j][0]]
    ans = 0
    a_key = list(a_dict.keys())
    a_value = list(a_dict.values())
    for k in range(len(a_dict)):
        ans += a_key[k] * a_value[k]

    print(ans)