Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    time = 0
    for k in range(N):
        if A[k] == B[i]:
            A[k] = C[i]
            time += 1
        
    sum += (C[i]-B[i])*time

    print(sum)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

num = [0]*100000

for i in range(N):
    num[A[i]-1] += 1

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    sum += (C[i]-B[i])*num[B[i]-1]
    num[C[i]-1] += num[B[i]-1] 
    num[B[i]-1] = 0

    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    time = 0
    A = sorted(A)
    for k in range(N):
        if A[k] == B[i]:
            A[k] = C[i]
            time += 1
        elif A[k] > B[i]:
            break
        
    sum += (C[i]-B[i])*time

    print(sum)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

num = [0]*100000

for i in range(N):
    num[A[i]-1] += 1

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    sum += (C[i]-B[i])*num[B[i]-1]
    num[C[i]-1] += num[B[i]-1] 
    num[B[i]-1] = 0

    print(sum)

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

N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
B = [0]*Q
C = [0]*Q

sum = np.sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    time = 0
    A = np.sort(A)
    for k in range(N):
        if A[k] == B[i]:
            A[k] = C[i]
            time += 1
        elif A[k] > B[i]:
            break
        
    sum += (C[i]-B[i])*time

    print(sum)

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

num = [0]*100000

for i in range(N):
    num[A[i]-1] += 1

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    sum += (C[i]-B[i])*num[B[i]-1]
    num[C[i]-1] += num[B[i]-1] 
    num[B[i]-1] = 0

    print(sum)

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

N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    A = np.sort(A)
    for k in range(N):
        if A[k] == B[i]:
            A[k] = C[i]
        elif A[k] > B[i]:
            break

    print(np.sum(A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

num = [0]*100000

for i in range(N):
    num[A[i]-1] += 1

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    sum += (C[i]-B[i])*num[B[i]-1]
    num[C[i]-1] += num[B[i]-1] 
    num[B[i]-1] = 0

    print(sum)

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

N = int(input())
A = np.array(sorted(list(map(int, input().split()))))
Q = int(input())
B = [0]*Q
C = [0]*Q
for i in range(Q):
    B[i], C[i] = map(int, input().split())
    A[A==B[i]] = C[i]
    print(np.sum(A))

Fixed submission:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = [0]*Q
C = [0]*Q

num = [0]*100000

for i in range(N):
    num[A[i]-1] += 1

sum = sum(A)

for i in range(Q):
    B[i], C[i] = map(int, input().split())
    
    sum += (C[i]-B[i])*num[B[i]-1]
    num[C[i]-1] += num[B[i]-1] 
    num[B[i]-1] = 0

    print(sum)