Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a.sort()
s = set(a)
c = collections.Counter(a)
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    if ca in c:
        c[ca] += c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    total = 0
    for key, val in c.items():
#         print(key, val)
        total += key * val
    print(total)



Fixed submission:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
c_n = np.array([(k, v) for k, v in c.items()])
total = np.dot(c_n.T[0], c_n.T[1])
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    num = c[cf]
    if ca in c:
        c[ca] += c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    total += num * (ca - cf)
    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a.sort()
s = set(a)
c = collections.Counter(a)
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    if ca in c:
        c[ca] += c[cf]
#         del sd[cf]
        del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
#         del sd[cf]
        del c[cf]
    total = 0
    for key, val in c.items():
#        print(key, val)
        total += key * val
    print(total)




Fixed submission:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
c_n = np.array([(k, v) for k, v in c.items()])
total = np.dot(c_n.T[0], c_n.T[1])
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    num = c[cf]
    if ca in c:
        c[ca] += c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    total += num * (ca - cf)
    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np
import math
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

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

for _ in range(q):
    cf, ca = map(int, input().split())
    na[na == cf] = ca
    print(na.sum())



Fixed submission:
import sys
import numpy as np
import math
import collections
from collections import deque 
from functools import reduce

# input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
c = collections.Counter(a)
c_n = np.array([(k, v) for k, v in c.items()])
total = np.dot(c_n.T[0], c_n.T[1])
q = int(input())
for _ in range(q):
    cf, ca = map(int, input().split())
    num = c[cf]
    if ca in c:
        c[ca] += c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    else:
#         sd[ca] = sd[cf]
        c[ca] = c[cf]
        c[cf] = 0
#         del sd[cf]
#         del c[cf]
    total += num * (ca - cf)
    print(total)