for q in range(Q):
    B, C = map(int, input().split())
    if B in A:
        A2 = [i for i, x in enumerate(A) if x == B]
        A = [C if i == B else i for i in A]
        print(sum(A))
    else:
        print(sum(A))