n = int(input())

a_list = [int(i) for i in input().split()]
q = int(input())

ary = [0 for _ in range((10 ** 5)+1)]

for i in a_list:
    ary[i] += i


for _ in range(q):
    b, c = map(int, input().split())
    print('BC', b, c)
    ary[c] += (ary[b]/b)*c
    ary[b] = 0
    ans = int(sum(ary))
    print(ans)
