n = int(input())
a_list = list(map(int, input().split()))
a_dict = {}
a_sum = 0
for a in a_list:
    a_sum += a
    if a in a_dict:
        a_dict[a] += 1
    else:
        a_dict[a] = 1

q = int(input())

for i in range(q):
    b, c = map(int,input().split())
    convert_num = a_dict.get(b)
    diff = 0
    if convert_num:
        diff = convert_num * (c - b)
        if a_dict.get(c):
            a_dict[c] = a_dict[c] + a_dict[b] 
            a_dict.pop(b)
        else:
            a_dict[c] = a_dict[b]
    a_sum += diff
    print(a_sum)