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

N=int(input())
A=list(map(int, input().split()))
A=sorted(A)
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=np.cumsum(A)
print(ans[-1])

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans[-1]+=(C-B)*len(rep)
    print(ans[-1])

Fixed submission:
mod=pow(10,5)+2
N=int(input())
num=[0]*mod
ans=0
A=list(map(int, input().split()))
for i in range(N):
    num[A[i]]+=1
    ans+=A[i]
Q=int(input())
for i in range(Q):
    B,C=map(int, input().split())
    cnt=num[B]
    num[C]+=num[B]
    num[B]=0
    ans+=(C-B)*cnt
    print(ans)

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

N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=np.cumsum(A)
print(ans[-1])

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans[-1]+=(C-B)*len(rep)
    print(ans[-1])

Fixed submission:
mod=pow(10,5)+2
N=int(input())
num=[0]*mod
ans=0
A=list(map(int, input().split()))
for i in range(N):
    num[A[i]]+=1
    ans+=A[i]
Q=int(input())
for i in range(Q):
    B,C=map(int, input().split())
    cnt=num[B]
    num[C]+=num[B]
    num[B]=0
    ans+=(C-B)*cnt
    print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy as np

N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=np.cumsum(A)
print(ans[1])

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans+=(C-B)*len(rep)
    print(ans[1])

Fixed submission:
mod=pow(10,5)+2
N=int(input())
num=[0]*mod
ans=0
A=list(map(int, input().split()))
for i in range(N):
    num[A[i]]+=1
    ans+=A[i]
Q=int(input())
for i in range(Q):
    B,C=map(int, input().split())
    cnt=num[B]
    num[C]+=num[B]
    num[B]=0
    ans+=(C-B)*cnt
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=sum(A)
print(ans)

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans+=(C-B)*len(rep)
    print(ans)

Fixed submission:
mod=pow(10,5)+2
N=int(input())
num=[0]*mod
ans=0
A=list(map(int, input().split()))
for i in range(N):
    num[A[i]]+=1
    ans+=A[i]
Q=int(input())
for i in range(Q):
    B,C=map(int, input().split())
    cnt=num[B]
    num[C]+=num[B]
    num[B]=0
    ans+=(C-B)*cnt
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=0
for i in range(N):
    ans+=A[i]
print(ans)

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans+=(C-B)*len(rep)
    print(ans)

Fixed submission:
mod=pow(10,5)+2
N=int(input())
num=[0]*mod
ans=0
A=list(map(int, input().split()))
for i in range(N):
    num[A[i]]+=1
    ans+=A[i]
Q=int(input())
for i in range(Q):
    B,C=map(int, input().split())
    cnt=num[B]
    num[C]+=num[B]
    num[B]=0
    ans+=(C-B)*cnt
    print(ans)