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

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0

        summ = 0
        keys = list(dic.keys())
        values = list(dic.values())
        print(np.array(keys).dot(np.array(values)))

Fixed submission:
import sys
import numpy as np

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    # keys = list(dic.keys())
    # values = list(dic.values())
    # summ = np.array(keys).dot(np.array(values))
    summ = sum([k*v for k, v in dic.items()])
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        diff = c*dic.get(b,0) - b*dic.get(b,0)
        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0
        summ += diff
        print(summ)

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

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0

        summ = 0
        for k, v in dic.items():
            summ += k*v
        print(summ)

Fixed submission:
import sys
import numpy as np

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    # keys = list(dic.keys())
    # values = list(dic.values())
    # summ = np.array(keys).dot(np.array(values))
    summ = sum([k*v for k, v in dic.items()])
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        diff = c*dic.get(b,0) - b*dic.get(b,0)
        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0
        summ += diff
        print(summ)

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

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    B, C = [], []
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))
        B.append(b)
        C.append(c)

    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    for i in range(q):
        dic[C[i]] = dic.get(C[i], 0) + dic.get(B[i], 0)
        dic[B[i]] = 0

        summ = 0
        for k, v in dic.items():
            summ += k*v
        print(summ)



Fixed submission:
import sys
import numpy as np

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    # keys = list(dic.keys())
    # values = list(dic.values())
    # summ = np.array(keys).dot(np.array(values))
    summ = sum([k*v for k, v in dic.items()])
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        diff = c*dic.get(b,0) - b*dic.get(b,0)
        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0
        summ += diff
        print(summ)

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

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    B, C = [], []
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))
        B.append(b)
        C.append(c)

    for i in range(q):
        summ = 0
        for j in range(n):
            if A[j] == B[i]:
                A[j] = C[i]
            summ += A[j]
        print(summ)

Fixed submission:
import sys
import numpy as np

if __name__ == '__main__':
    n = int(sys.stdin.readline().rstrip())
    A = list(map(lambda x: int(x), sys.stdin.readline().split()))
    q = int(sys.stdin.readline().rstrip())
    dic = {}
    for a in A:
        dic[a] = dic.get(a, 0) + 1

    # keys = list(dic.keys())
    # values = list(dic.values())
    # summ = np.array(keys).dot(np.array(values))
    summ = sum([k*v for k, v in dic.items()])
    for i in range(q):
        b, c = list(map(lambda x: int(x), sys.stdin.readline().split()))

        diff = c*dic.get(b,0) - b*dic.get(b,0)
        dic[c] = dic.get(c, 0) + dic.get(b, 0)
        dic[b] = 0
        summ += diff
        print(summ)