Problem: You have a sequence A composed of N positive integers: A_{1}, A_{2}, \cdots, A_{N}.  You will now successively do the following Q operations:    * In the i-th operation, you replace every element whose value is B_{i} with C_{i}.  For each i (1 \leq i \leq Q), find S_{i}: the sum of all elements in A just after the i-th operation.

Submission attempts (in order):
import collections
n = map(int, input().split())
A = list(map(int, input().split()))
q = map(int, input().split())
 
cnt = collections.Counter(A)
ANS = []
base = sum(A)

for i in range(q):
  b,c = map(int, input().split())
  ans = base + (c-b) * cnt[b]
  ANS.append(ans)
  cnt[c] += cnt[b]
  cnt[b] = 0
  base = ans

for i in ANS:
  print(i)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
Ans = []
 
for i in range(N):
    base += A[i]

for i in range(Q):
  b ,c = map(int, input().split())
  Ans.append(Ans[i] + (c-b) * cnt[b])
  cnt[c] += cnt[b]
  cnt[b] = 0

print(Ans)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
Ans = []
 
for i in range(N):
    base += A[i]

for i in range(Q):
  b,c = map(int, input().split())
  ans = base + (c-b) * cnt[b]
  Ans.append(ans)
  cnt[c] += cnt[b]
  cnt[b] = 0
  base = ans

print(Ans)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
Ans = []
 
for i in range(N):
    base += A[i]

for i in range(Q):
  b ,c = map(int, input().split())
  Ans.append(base + (c-b) * cnt[b])
  cnt[c] += cnt[b]
  cnt[b] = 0
  base = Ans[i]

print(Ans)

import collections

n = map(int, input().split())
A = list(map(int, input().split()))
q = map(int, input().split())

CNT = collections.Counter(A)
ANS = []
ans = sum(A)

for i in range(q):
    b, c = map(int, input().split())
    t = CNT[b]
    ans +=(c - b) * t
    ANS.append(ans)
    CNT[c] += t
    CNT[b] = 0

for i in ANS:
    print(i)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
ANS = []
 
for i in range(N):
    base += A[i]

for i in range(Q):
  b,c = map(int, input().split())
  ans = base + (c-b) * cnt[b]
  ANS.append(ans)
  cnt[c] += cnt[b]
  cnt[b] = 0
  base = ans

print(ANS)

import collections

n = map(int, input().split())
A = list(map(int, input().split()))
q = map(int, input().split())

cnt = collections.Counter(A)
ANS = []
base = sum(A)

for i in range(q):
    b, c = map(int, input().split())
    t = CNT[b]
    ans = base + (c - b) * t
    ANS.append(ans)
    CNT[c] += t
    CNT[b] = 0
    base = ans

for i in ANS:
    print(i)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
B, C = list(map(int, input().split())),list(map(int, input().split()))

base = 0
cnt = collections.Counter(A)

for i in range(N):
    base += A[i]

Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])

for i in range(Q-1):
    if cnt[B[i+1]] != 0:
        Ans.append(Ans[i] + (C[i+1]-B[i+1]) * cnt[B[i+1]])
        for j in range(i+1):
            Ans[i+1] += B.count(C[j]) * (C[i+1] - C[j])
    else:
        Ans.append(Ans[i])

print(Ans)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)    
ANS = []
 
for i in range(N):
    base += A[i]

for i in range(Q):
  b,c = map(int, input().split())
  ans = base + (c-b) * cnt[b]
  ANS.append(ans)
  cnt[c] += cnt[b]
  cnt[b] = 0
  base = ans

for i in ANS:
  print(i)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
 
base = 0
cnt = collections.Counter(A)
 
for i in range(N):
    base += A[i]
    
Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])
 
for i in range(Q-1):
  b ,c = map(int, input().split())
  Ans.append(Ans[i] + (c-b) * cnt[b])
  cnt[c] += cnt[b]
  cnt[b] = 0

print(Ans)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
B = list(map(int, input().split()))
C = list(map(int, input().split()))
 
base = 0
cnt = collections.Counter(A)
 
for i in range(N):
    base += A[i]

Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])
 
for i in range(Q-1):
    if cnt[B[i+1]] != 0:
        Ans.append(Ans[i] + (C[i+1]-B[i+1]) * cnt[B[i+1]])
        for j in range(i+1):
            Ans[i+1] += B.count(C[j]) * (C[i+1] - C[j])
    else:
        Ans.append(Ans[i])

print(Ans)

import collections
N = map(int, input().split())
A = list(map(int, input().split()))
Q = map(int, input().split())
B = list(map(int, input().split()))
C = list(map(int, input().split()))

base = 0
cnt = collections.Counter(A)

for i in range(N):
    base += A[i]

Ans = []
Ans.append(base + (C[0]-B[0]) * cnt[B[0]])

for i in range(Q-1):
    if cnt[B[i+1]] != 0:
        Ans.append(Ans[i] + (C[i+1]-B[i+1]) * cnt[B[i+1]])
        for j in range(i+1):
            Ans[i+1] += B.count(C[j]) * (C[i+1] - C[j])
    else:
        Ans.append(Ans[i])

print(Ans)

import collections

n = int(input())
A = list(map(int, input().split()))
q = int(input())

CNT = collections.Counter(A)
ANS = []
ans = sum(A)

for i in range(q):
    b, c = map(int, input().split())
    t = CNT[b]
    ans +=(c - b) * t
    ANS.append(ans)
    CNT[c] += t
    CNT[b] = 0

for i in ANS:
    print(i)

Verdicts: Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Runtime Error

Accepted