n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = [0] * q
c = [0] * q
for i in range(q):
    b[i], c[i] = map(int, input().split())

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

for i in range(q):
    ans = 0
    if b[i] in num_dict:
        if c[i] in num_dict:
            num_dict[c[i]] += num_dict.pop(b[i])
        else:
            num_dict[c[i]] = num_dict.pop(b[i])
    for k, v in num_dict.items():
        ans += k*v
    print(ans)