Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())
for i in range(q):
    bc = list(map(int, input().split()))
    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    print(sum(list_in))

Fixed submission:
import collections
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())

counter = collections.Counter(list_in)
sum = sum(list_in)

for i in range(q):
    bc = list(map(int, input().split()))
#    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    sum_diff = (bc[1] - bc[0]) * counter[bc[0]]
    counter[bc[1]] = counter[bc[1]] + counter[bc[0]]
    counter[bc[0]] = 0
    print(sum+sum_diff)
    sum = sum + sum_diff

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())
for i in range(q):
    bc = list(map(int, input().split()))
    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    print(sum(list_in))

Fixed submission:
import collections
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())

counter = collections.Counter(list_in)
sum = sum(list_in)

for i in range(q):
    bc = list(map(int, input().split()))
#    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    sum_diff = (bc[1] - bc[0]) * counter[bc[0]]
    counter[bc[1]] = counter[bc[1]] + counter[bc[0]]
    counter[bc[0]] = 0
    print(sum+sum_diff)
    sum = sum + sum_diff

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())
for i in range(q):
    bc = list(map(int, input().split()))
    for j in range(n):
        if list_in[j] == bc[0]:
            list_in[j] = bc[1]
    print(sum(list_in))

Fixed submission:
import collections
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())

counter = collections.Counter(list_in)
sum = sum(list_in)

for i in range(q):
    bc = list(map(int, input().split()))
#    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    sum_diff = (bc[1] - bc[0]) * counter[bc[0]]
    counter[bc[1]] = counter[bc[1]] + counter[bc[0]]
    counter[bc[0]] = 0
    print(sum+sum_diff)
    sum = sum + sum_diff

Buggy python submission (verdict: Runtime Error):
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())
for i in range(q+1):
    bc = list(map(int, input().split()))
    for j in range(n):
        if list_in[j] == bc[0]:
            list_in[j] = bc[1]
    print(sum(list_in))


Fixed submission:
import collections
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())

counter = collections.Counter(list_in)
sum = sum(list_in)

for i in range(q):
    bc = list(map(int, input().split()))
#    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    sum_diff = (bc[1] - bc[0]) * counter[bc[0]]
    counter[bc[1]] = counter[bc[1]] + counter[bc[0]]
    counter[bc[0]] = 0
    print(sum+sum_diff)
    sum = sum + sum_diff

Buggy python submission (verdict: Runtime Error):
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())
for i in range(q+1):
    b,c = input().split()
    b = int(b)
    c = int(c)
    for j in range(n):
        if list_in[j]==b:
            list_in[j]=c
    print(sum(list_in))

Fixed submission:
import collections
n = int(input())
list_in = list(map(int, input().split()))
q = int(input())

counter = collections.Counter(list_in)
sum = sum(list_in)

for i in range(q):
    bc = list(map(int, input().split()))
#    list_in = [bc[1] if list_in[l] == bc[0] else list_in[l] for l in range(n)]
    sum_diff = (bc[1] - bc[0]) * counter[bc[0]]
    counter[bc[1]] = counter[bc[1]] + counter[bc[0]]
    counter[bc[0]] = 0
    print(sum+sum_diff)
    sum = sum + sum_diff