Buggy python submission (verdict: Runtime Error):
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
ma=max(a)
a2=[0]*ma
for ii in a:
  a2[ii-1]+=1

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj]*(jj+1)
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  r+=(c-b)*a2[b-1]
  a2[c-1]+=a2[b-1]
  a2[b-1]=0
  print(r)


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ma=10**5
a2=[0]*ma
for ii in a:
  a2[ii-1]+=1

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj]*(jj+1)
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  r+=(c-b)*a2[b-1]
  a2[c-1]+=a2[b-1]
  a2[b-1]=0
  print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
a2=[]
for ii in sa:
  ca=a.count(ii)
  a2.append([ii,ca])
  for jj in range(ca):
    a.remove(ii)
#import numpy as np
#a2=np.array(a2)
#print(a2)

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj][0]*a2[jj][1]
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  for jj in range(len(a2)):
    if a2[jj][0]==b:
      r+=(c-b)*a2[jj][1]
      a2[jj][0]=c
  print(r)


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ma=10**5
a2=[0]*ma
for ii in a:
  a2[ii-1]+=1

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj]*(jj+1)
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  r+=(c-b)*a2[b-1]
  a2[c-1]+=a2[b-1]
  a2[b-1]=0
  print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
a2=[]
for ii in sa:
  ca=a.count(ii)
  a2.append([ii,ca])
  for jj in range(ca):
    a.remove(ii)
#import numpy as np
#a2=np.array(a2)
#print(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  s=0
  idx=[]
  for jj in range(len(a2)):
    if a2[jj][0]==b or a2[jj][0]==c:
      idx.append(jj)
      s+=a2[jj][1]
      a2[jj][0]=0
      a2[jj][1]=0
  a2.append([c,s])
  #print(a2)
  r=0
  for jj in range(len(a2)):
     r+= a2[jj][0]*a2[jj][1]
  print(r)


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ma=10**5
a2=[0]*ma
for ii in a:
  a2[ii-1]+=1

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj]*(jj+1)
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  r+=(c-b)*a2[b-1]
  a2[c-1]+=a2[b-1]
  a2[b-1]=0
  print(r)

Buggy python submission (verdict: Time Limit Exceeded):
n=int(input())
a=list(map(int,input().split()))
sa=set(a)
a2=[]
for ii in sa:
  ca=a.count(ii)
  a2.append([ii,ca])
  for jj in range(ca):
    a.remove(ii)
import numpy as np
a2=np.array(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  idx=np.where(a2[:,0]==b)
  idx=np.append(idx,np.where(a2[:,0]==c))
  s=0
  for jj in idx:
    s+=a2[jj,1]
  a2=np.delete(a2,idx,axis=0)
  a2=np.append(a2,[[c,s]],axis=0)
  print(sum(a2[:,0]*a2[:,1]))


Fixed submission:
n=int(input())
a=list(map(int,input().split()))
ma=10**5
a2=[0]*ma
for ii in a:
  a2[ii-1]+=1

def func(a2):
  r=0
  for jj in range(len(a2)):
     r+= a2[jj]*(jj+1)
  return r

r=func(a2)
q=int(input())
for ii in range(q):
  b,c=map(int,input().split())
  r+=(c-b)*a2[b-1]
  a2[c-1]+=a2[b-1]
  a2[b-1]=0
  print(r)