Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [map(int,input().split()) for _ in range(Q)]
B, C = [i for i in zip(*BC)]
#print(B,C)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1

ans = sum(A)
for q in range(Q):
  bc = C(q) - B(q)
  ans = ans + bc * dic.get(B(q),0)
  dic[C(q)] = dic.get(C(q),0) + dic.get(B(q),0)
  dic[B(q)] = 0
  print(ans)


Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  del dic[B[q]]
  print(ans)



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Runtime Error):
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  del dic[B[q]]
  print(ans)



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Runtime Error):
import bisect

N = int(input())
A = list(map(int,input().split()))

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b #B[i]の要素数
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())

ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  jg = False
  for j in range(len(B)):
    if X[j][0] == y:
      jg = True
      jgx = j
  for j in range(len(B)):
    if X[j][0] == x:
      X[j][0] = y
      if jg = True:
        X[j][1] += X[jgx][1]
        X[jgx][0] = 0
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)
  



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int,input().split()))

Q = int(input())

A.sort()
B = list(set(A))
X = [[0 for j in range(2)] for i in range(len(B))]
for i in range(len(B)):
  b=bisect.bisect_left(A,B[i])
  c=bisect.bisect_right(A,B[i])
  num = c - b #B[i]の要素数
  X[i][0] = B[i]
  X[i][1] = num

M = [0 for j in range(Q)]
N = [0 for j in range(Q)]
for i in range(Q):
  M[i],N[i] = map(int,input().split())

ans = int()
for i in range(Q):
  x = M[i]
  y = N[i]
  ans = 0
  for j in range(len(B)):
    if X[j][0] == x:
      X[j][0] = y
    ans += X[j][0] * X[j][1]
  print(ans)
  #print(X)
  



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int,input().split()))

Q = int(input())

for i in range(Q):
  x,y = map(int,input().split())
  search = x
  A.sort()
  b=bisect.bisect_left(A,search)
  c=bisect.bisect_right(A,search)
  for j in range(b,c):
    A[j] = y
  print(sum(A))



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int,input().split()))

Q = int(input())
s = [list(map(int,input().split())) for i in range(Q)]
for i in range(Q):
  search = s[i][0]
  A.sort()
  b=bisect.bisect_left(A,search)
  c=bisect.bisect_right(A,search)
  for j in range(b,c):
    A[j] = s[i][1]
  print(sum(A))



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import bisect

N = int(input())
A = list(map(int,input().split()))
A.sort()
Q = int(input())
s = [list(map(int,input().split())) for i in range(Q)]
for i in range(Q):
  search = s[i][0]
  b=bisect.bisect_left(A,search)
  c=bisect.bisect_right(A,search)
  for j in range(b,c):
    A[j] = s[i][1]
  print(sum(A))



Fixed submission:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
BC = [list(map(int,input().split())) for i in range(Q)]
B, C = [list(i) for i in zip(*BC)]
#print(B,C,BC)

dic = {}
for a in A:
  dic[a] = dic.get(a,0) + 1
#print(dic)

ans = sum(A)
for q in range(Q):
  ans = ans + (C[q]-B[q]) * dic.get(B[q],0)
  dic[C[q]] = dic.get(B[q],0) + dic.get(C[q],0)
  dic[B[q]] = 0
  print(ans)