def replace_int(a_dict, b, c):
    if not a_dict.get(c):
        a_dict[c] = 0
    if a_dict.get(b):
        a_dict[c] = a_dict[c] + a_dict[b]
        a_dict[b] = 0



n = int(input())
a_dict = dict()
a_list = map(int, input().split())

for a in a_list:
    a_dict[a] = a_dict[a] + 1 if a_dict.get(a) else 1

q = int(input())
for i in range(q):
    b, c = map(int, input().split())
    replace_int(a_dict, b, c )
    sum = 0
    for k, v in a_dict.items():
        sum += k * v
    print(sum)