Buggy python submission (verdict: Runtime Error):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
#for i in range(100001):
#    AA[i]=A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
    AA[(B[i][0])]=A.count(int((B[i][0])))
    A.replace(B[i][0],B[i][1])
#    AA[(B[i][1])]=A.count(int((B[i][1])))
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
#    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
#    AA[(B[i][0])] = 0
    print(sum)

Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in A:
    AA[i]+=1 #A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
#    AA[(B[i][0])]=A.count(int((B[i][0])))
#    A.replace(B[i][0],B[i][1])
#    AA[(B[i][1])]=A.count(int((B[i][1])))
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in range(100001):
    AA[i]=A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in A:
    AA[i]+=1 #A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
#    AA[(B[i][0])]=A.count(int((B[i][0])))
#    A.replace(B[i][0],B[i][1])
#    AA[(B[i][1])]=A.count(int((B[i][1])))
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
sum=int(0)
for i in range(Q):
    B.append(list(map(int,input().split())))
for i in range(Q):
    for j in range(N):
        if A[j]==B[i][0]:
            A[j]=B[i][1]
    for k in range(N):
        sum += int(A[k])
    print(sum)
    sum=int(0)


Fixed submission:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
for i in A:
    AA[i]+=1 #A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
#    AA[(B[i][0])]=A.count(int((B[i][0])))
#    A.replace(B[i][0],B[i][1])
#    AA[(B[i][1])]=A.count(int((B[i][1])))
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
    AA[(B[i][0])] = 0
    print(sum)