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

n = int(input())

nums = list(map(int, input().split()))

dic = defaultdict(int)
for num in nums:
    dic[num] += 1 


answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    dic[c] += dic[b]
    dic[b] = 0

    answer = 0
    for i, v in dic.items():
        answer += i * v

    answers.append(answer)

for answer in answers:
    print(answer)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

n = int(input())

nums = list(map(int, input().split()))

dic = defaultdict(int)
for num in nums:
    dic[num] += 1 


answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    dic[c] += dic[b]
    dic[b] = 0

    answer = 0
    for i, v in dic.items():
        answer += i * v

    answers.append(answer)

for answer in answers:
    print(answer)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

nums = list(map(int, input().split()))
q = int(input())

ans = []

for _ in range(q):
    b, c = map(int, input().split())
    
    nums = sorted(nums)

    if b in nums:
        cnt = 0
        is_b = True
        while is_b:
            nums.remove(b)
            cnt += 1
            if not b in nums:
                is_b = False
                nums = nums + [c] * cnt


    ans.append(sum(nums))

for a in ans:
    print(a)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

nums = list(map(int, input().split()))
q = int(input())

ans = []

for _ in range(q):
    b, c = map(int, input().split())
    
    nums = sorted(nums)

    if b in nums:
        cnt = 0
        is_b = True
        while is_b:
            nums.remove(b)
            cnt += 1
            if not b in nums:
                is_b = False
                nums = nums + [c] * cnt


    ans.append(sum(nums))

for a in ans:
    print(a)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

nums = list(map(int, input().split()))
q = int(input())

ans = []

for _ in range(q):
    b, c = map(int, input().split())
    
    nums = sorted(nums)

    if b in nums:
        nums = [(c if num == b else num) for num in nums]
    
    ans.append(sum(nums))

for a in ans:
    print(a)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

nums = input().split()

q = int(input())

ans = []

for _ in range(q):
    b, c = input().split()
    
    nums = " ".join(nums)
    nums = nums.replace(b, c)
    nums = nums.split()
    t = [int(num) for num in nums]
    ans.append(sum(t))

for a in ans:
    print(a)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

nums = list(map(int, input().split()))
q = int(input())

ans = []

for _ in range(q):
    b, c = map(int, input().split())
    nums = [(c if num == b else num) for num in nums]
    ans.append(sum(nums))

for a in ans:
    print(a)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)

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

nums = list(map(int, input().split()))
q = int(input())

ans = []

for _ in range(q):
    b, c = map(int, input().split())
    nums = [(c if num == b else num) for num in nums]
    ans.append(sum(nums))

for a in ans:
    print(a)

Fixed submission:
from collections import Counter

n = int(input())

nums = tuple(map(int, input().split()))

count = Counter(nums)
s = sum(nums)

answers = []
q = int(input())
for _ in range(q):
    b, c = map(int, input().split())
    s = s + (c - b) * count[b]
    count[c] += count[b]
    count[b] = 0
    answers.append(s)
    
for answer in answers:
    print(answer)