Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
import numpy as np
readline = sys.stdin.buffer.readline

N = int(readline())
A = np.array(list(map(int, readline().split())))
Q = int(readline())

answer = [0] * Q

for i in range(Q):
    B, C = map(int, readline().split())

    A = np.where(A == B, C, A)
    print(np.sum(A))


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline


def sum_d(d):
    total = 0
    for k, v in d.items():
        total += k * v
    return total


N = int(readline())
A = map(int, readline().split())
Q = int(readline())
d = Counter(A)

answer = [0] * Q

for i in range(Q):
    B, C = map(int, readline().split())

    if B in d:
        if C in d:
            d[C] = d[C] + d.pop(B)
        else:
            d[C] = d.pop(B)

    answer[i] = sum_d(d)

print(*answer, sep='\n')


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
answer = [0] * Q

for i in range(Q):
    B, C = map(int, readline().split())

    A = [(C if num == B else num) for num in A]
    answer[i] = sum(A)

print(*answer, sep='\n')


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())

for _ in range(Q):
    B, C = map(int, readline().split())

    A = [(C if num == B else num) for num in A]
    print(sum(A))


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())

for line in readlines():
    B, C = map(int, line.split())

    A = [(C if num == B else num) for num in A]
    print(sum(A))


Fixed submission:
#!/usr/bin/env python3
import sys
from collections import Counter
readline = sys.stdin.buffer.readline

N = int(readline())
A = list(map(int, readline().split()))
Q = int(readline())
d = Counter(A)

total = sum(A)

for _ in range(Q):
    B, C = map(int, readline().split())

    total -= B * d[B]
    total += C * d[B]

    d[C] += d[B]
    d[B] = 0

    print(total)