Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
 
n = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)

Fixed submission:
class Counter(dict):
    def __missing__(self, i):
        return 0

n = int(input())
a = list(map(int, input().split()))
q = int(input())

total = sum(a)

bucket = Counter()

for item in a:
    bucket[item] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = bucket[b]
    total -= bucket[b]*b
    bucket[c] += bucket[b]
    bucket[b] = 0
    total += taken*c
    print(total)

Buggy python submission (verdict: Runtime Error):
from collections import defaultdict
 
n = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1
 
for i in range(q):
    b, c = map(int, input().split())
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)

Fixed submission:
class Counter(dict):
    def __missing__(self, i):
        return 0

n = int(input())
a = list(map(int, input().split()))
q = int(input())

total = sum(a)

bucket = Counter()

for item in a:
    bucket[item] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = bucket[b]
    total -= bucket[b]*b
    bucket[c] += bucket[b]
    bucket[b] = 0
    total += taken*c
    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    raw_number = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_number:
        numbers[num] += 1

    Q = int(input())

    for _ in range(Q):
        a, b = list(map(int, input().split()))
        numbers[b] += numbers[a]
        numbers.pop(a)
        print(sum([v*k for (k, v) in numbers.items()]))
    
main()

Fixed submission:
class Counter(dict):
    def __missing__(self, i):
        return 0

n = int(input())
a = list(map(int, input().split()))
q = int(input())

total = sum(a)

bucket = Counter()

for item in a:
    bucket[item] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = bucket[b]
    total -= bucket[b]*b
    bucket[c] += bucket[b]
    bucket[b] = 0
    total += taken*c
    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys

input = sys.stdin.readline

def main():
    N = int(input())
    raw_number = list(map(int, input().split()))
    numbers = defaultdict(int)

    for num in raw_number:
        numbers[num] += 1

    Q = int(input())

    for _ in range(Q):
        a, b = list(map(int, input().split()))
        numbers[b] += numbers[a]
        numbers[a] = 0
        print(sum([v*k for (k, v) in numbers.items()]))
    
main()

Fixed submission:
class Counter(dict):
    def __missing__(self, i):
        return 0

n = int(input())
a = list(map(int, input().split()))
q = int(input())

total = sum(a)

bucket = Counter()

for item in a:
    bucket[item] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = bucket[b]
    total -= bucket[b]*b
    bucket[c] += bucket[b]
    bucket[b] = 0
    total += taken*c
    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
N = int(input())
raw_number = list(map(int, input().split()))
numbers = defaultdict(int)

for num in raw_number:
    numbers[num] += 1
    
Q = int(input())

for _ in range(Q):
    a, b = list(map(int, input().split()))
    numbers[b] += numbers[a]
    numbers[a] = 0
    print(sum([v*k for (k, v) in numbers.items()]))

Fixed submission:
class Counter(dict):
    def __missing__(self, i):
        return 0

n = int(input())
a = list(map(int, input().split()))
q = int(input())

total = sum(a)

bucket = Counter()

for item in a:
    bucket[item] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = bucket[b]
    total -= bucket[b]*b
    bucket[c] += bucket[b]
    bucket[b] = 0
    total += taken*c
    print(total)