Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []
for i in range(100000):
    s.append(a.count(i))
ans = sum(a)

for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []

ans = sum(a)
s = Counter(a)
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []
for i in range(100000):
    s.append(a.count(i))
ans = sum(a)

for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []

ans = sum(a)
s = Counter(a)
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
ans = sum(a)
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += a.count(b[i]) * (c[i]-b[i])
    for j in range(a.count(b[i])):
        a.append(c[i])
    a = [j for j in a if j != b[i]]
    print(ans)

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []

ans = sum(a)
s = Counter(a)
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
    print(sum(a))

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []

ans = sum(a)
s = Counter(a)
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    for j in range(n):
        if a[j] == b[i]:
            a[j] = c[i]
    print(sum(a))

Fixed submission:
from collections import Counter
n = int(input())
a = list(map(int, input().split()))
q = int(input())
b = []
c = []
s = []

ans = sum(a)
s = Counter(a)
for i in range(q):
    bi,ci = map(int, input().split())
    b.append(bi)
    c.append(ci)
for i in range(q):
    ans += s[b[i]] * (c[i]-b[i])
    s[c[i]] += s[b[i]]
    s[b[i]] = 0
    print(ans)