Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
import copy

N = int(input())

target_list = list(map(int, input().split()))
check = [Counter(target_list)]
result_list = [sum(target_list)]


for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check[index]:
        result = copy.deepcopy(check[index])
        change = result.pop(B)
        diff = change * (C - B)
        if C in result:
            result[C] = result[C] + change
        else:
            result[C] = change

    else:
        result = copy.deepcopy(check[index])
        diff = 0
    check.append(result)
    print(result_list[index] + diff)
    result_list.append(result_list[index] + diff)


Fixed submission:
from collections import Counter

N = int(input())

target_list = list(map(int, input().split()))
check = Counter(target_list)
result = sum(target_list)


for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check:
        result = result + (check[B]) * (C - B)
        if C in check:
            check[C] = check[C] + check[B]
        else:
            check[C] = check[B]
        check[B] = 0
    print(result)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import Counter
import copy

N = int(input())

target_list = map(int, input().split())
check = [Counter(target_list)]

for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check[index]:
        result = copy.deepcopy(check[index])
        if C in result:
            result[C] = result[C] + result.pop(B)
        else:
            result[C] = result.pop(B)
    else:
        result = copy.deepcopy(check[index])
    check.append(result)
    print(sum([a * b for a, b in result.items()]))


Fixed submission:
from collections import Counter

N = int(input())

target_list = list(map(int, input().split()))
check = Counter(target_list)
result = sum(target_list)


for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check:
        result = result + (check[B]) * (C - B)
        if C in check:
            check[C] = check[C] + check[B]
        else:
            check[C] = check[B]
        check[B] = 0
    print(result)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

target_list = list(map(int, input().split()))
result = []

for i in range(int(input())):
    B, C = map(int, input().split())
    if B in target_list:
        num = target_list.count(B)
        target_list = [x for x in target_list if x != B]
        target_list.extend([C] * num)
        result.append(sum(target_list))
    else:
        result.append(sum(target_list))

for i in result:
    print(i)


Fixed submission:
from collections import Counter

N = int(input())

target_list = list(map(int, input().split()))
check = Counter(target_list)
result = sum(target_list)


for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check:
        result = result + (check[B]) * (C - B)
        if C in check:
            check[C] = check[C] + check[B]
        else:
            check[C] = check[B]
        check[B] = 0
    print(result)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())

target_list = map(int, input().split())
result = []

for i in range(int(input())):
    B, C = map(int, input().split())
    target_list = [C if i == B else i for i in target_list]
    result.append(sum(target_list))

for i in result:
    print(i)


Fixed submission:
from collections import Counter

N = int(input())

target_list = list(map(int, input().split()))
check = Counter(target_list)
result = sum(target_list)


for index in range(int(input())):
    B, C = map(int, input().split())
    if B in check:
        result = result + (check[B]) * (C - B)
        if C in check:
            check[C] = check[C] + check[B]
        else:
            check[C] = check[B]
        check[B] = 0
    print(result)