Buggy python submission (verdict: Runtime Error):
import collections
#入力
N = int(input())
n = 100000
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = dict(collections.Counter(Alist))
numlist = [0]*n

for j in range(n):
  if j in Adic:
    numlist[j] = Adic[j]
sum = sum(Alist)
for k in range(Q):
  sum += (Clist[k]-Blist[k])*numlist[Blist[k]]
  numlist[Clist[k]] += numlist[Blist[k]] 
  numlist[Blist[k]] = 0

  print(sum)

Fixed submission:
import collections
#入力
N = int(input())
n = 100001
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = dict(collections.Counter(Alist))
numlist = [0]*n

for j in range(n):
  if j in Adic:
    numlist[j] = Adic[j]
sum = sum(Alist)
for k in range(Q):
  sum += (Clist[k]-Blist[k])*numlist[Blist[k]]
  numlist[Clist[k]] += numlist[Blist[k]] 
  numlist[Blist[k]] = 0

  print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
import collections
#入力
N = int(input())
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = collections.Counter(Alist)

def dicsum(dic):
  sum = 0
  for i in range(len(dic)):
    sum += list(dic.keys())[i]*list(dic.values())[i]
  return sum


for j in range(Q):
  if (Blist[j] in Adic) == True:
    Adic[Clist[j]] += Adic[Blist[j]] 
    Adic.pop(Blist[j])
    print(dicsum(Adic))
  else:
    print(dicsum(Adic))

Fixed submission:
import collections
#入力
N = int(input())
n = 100001
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = dict(collections.Counter(Alist))
numlist = [0]*n

for j in range(n):
  if j in Adic:
    numlist[j] = Adic[j]
sum = sum(Alist)
for k in range(Q):
  sum += (Clist[k]-Blist[k])*numlist[Blist[k]]
  numlist[Clist[k]] += numlist[Blist[k]] 
  numlist[Blist[k]] = 0

  print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
#入力
N = int(input())
Alist = list(map(int,input().split()))
Alist.sort()
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

for j in range(Q):
  num = Alist.count(Blist[j])
  if num == 0:
    pass
  else:
    firstindex = Alist.index(Blist[j])
    for l in range(num):
      Alist[firstindex+l] = Clist[j]
  print(sum(Alist))
  Alist.sort()

Fixed submission:
import collections
#入力
N = int(input())
n = 100001
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = dict(collections.Counter(Alist))
numlist = [0]*n

for j in range(n):
  if j in Adic:
    numlist[j] = Adic[j]
sum = sum(Alist)
for k in range(Q):
  sum += (Clist[k]-Blist[k])*numlist[Blist[k]]
  numlist[Clist[k]] += numlist[Blist[k]] 
  numlist[Blist[k]] = 0

  print(sum)

Buggy python submission (verdict: Time Limit Exceeded):
#入力
N = int(input())
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

for j in range(Q):
  for k in range(len(Alist)):
    if Alist[k] == Blist[j]:
      Alist[k] = Clist[j]
  print(sum(Alist))

Fixed submission:
import collections
#入力
N = int(input())
n = 100001
Alist = list(map(int,input().split()))
Q = int(input())
Blist = []
Clist = []
for i in range(Q):
  B, C = map(int,input().split())
  Blist.append(B)
  Clist.append(C)

Adic = dict(collections.Counter(Alist))
numlist = [0]*n

for j in range(n):
  if j in Adic:
    numlist[j] = Adic[j]
sum = sum(Alist)
for k in range(Q):
  sum += (Clist[k]-Blist[k])*numlist[Blist[k]]
  numlist[Clist[k]] += numlist[Blist[k]] 
  numlist[Blist[k]] = 0

  print(sum)