Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

N = int(sys.stdin.readline())
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1

for search, replace in replace_map:
    if search in nums:
        nums[replace] = nums[replace] + nums[search] if replace in nums else nums[search]
        del nums[search]
    print(np.dot(list(nums.keys()), list(nums.values())))


Fixed submission:
import sys
import numpy as np

_ = sys.stdin.readline()
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1

previous_sum = 0
for search, replace in replace_map:
    replaced_count = 0
    if search in nums:
        replaced_count = nums[search]
        nums[replace] = nums[replace] + replaced_count if replace in nums else replaced_count
        del nums[search]
    if previous_sum == 0:
        previous_sum = np.dot(list(nums.keys()), list(nums.values()))
    else:
        previous_sum += replaced_count * (replace - search)
    print(previous_sum)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

N = int(sys.stdin.readline())
A = np.array(list(map(int, sys.stdin.readline().split())), dtype=np.uint32)
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for i in range(Q)]

for search, replace in replace_map:
    A[A == search] = replace
    print(np.sum(A))


Fixed submission:
import sys
import numpy as np

_ = sys.stdin.readline()
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1

previous_sum = 0
for search, replace in replace_map:
    replaced_count = 0
    if search in nums:
        replaced_count = nums[search]
        nums[replace] = nums[replace] + replaced_count if replace in nums else replaced_count
        del nums[search]
    if previous_sum == 0:
        previous_sum = np.dot(list(nums.keys()), list(nums.values()))
    else:
        previous_sum += replaced_count * (replace - search)
    print(previous_sum)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

N = int(sys.stdin.readline())
A = np.array(list(map(int, sys.stdin.readline().split())))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for i in range(Q)]

for search, replace in replace_map:
    A = np.where(A==search, replace, A)
    print(np.sum(A))


Fixed submission:
import sys
import numpy as np

_ = sys.stdin.readline()
A = list(map(int, sys.stdin.readline().split()))
Q = int(sys.stdin.readline())
replace_map = [list(map(int, sys.stdin.readline().split())) for _ in range(Q)]

nums = {}
for a in A:
    nums[a] = nums[a] + 1 if a in nums else 1

previous_sum = 0
for search, replace in replace_map:
    replaced_count = 0
    if search in nums:
        replaced_count = nums[search]
        nums[replace] = nums[replace] + replaced_count if replace in nums else replaced_count
        del nums[search]
    if previous_sum == 0:
        previous_sum = np.dot(list(nums.keys()), list(nums.values()))
    else:
        previous_sum += replaced_count * (replace - search)
    print(previous_sum)