Problem:


Score : 400 points

Problem Statement

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.

Constraints

All values in input are integers.

 1 \leq N, Q, A_{i}, B_{i}, C_{i} \leq 10^{5} 

 B_{i} \neq C_{i} 

Input

Input is given from Standard Input in the following format:

N
A_{1} A_{2} \cdots A_{N}
Q
B_{1} C_{1}
B_{2} C_{2}
\vdots
B_{Q} C_{Q}

Output

Print Q integers S_{i} to Standard Output in the following format:

S_{1}
S_{2}
\vdots
S_{Q}

Note that S_{i} may not fit into a 32-bit integer.

Sample Input 1

4
1 2 3 4
3
1 2
3 4
2 4

Sample Output 1

11
12
16

Initially, the sequence A is 1,2,3,4.

After each operation, it becomes the following:

2, 2, 3, 4

2, 2, 4, 4

4, 4, 4, 4

Sample Input 2

4
1 1 1 1
3
1 2
2 1
3 5

Sample Output 2

8
4
4

Note that the sequence A may not contain an element whose value is B_{i}.

Sample Input 3

2
1 2
3
1 100
2 100
100 1000

Sample Output 3

102
200
2000



Sample input:
4
1 2 3 4
3
1 2
3 4
2 4


Sample output:
11
12
16

Submission status: Runtime Error

Solution code:
N,=list(map(int, input().split()))
A = list(map(int, input().split()))
Q, = list(map(int, input().split()))
B = [0 for i in range(Q)]
C = [0 for i in range(Q)]
for q in range(Q):
  B[q],C[q]=list(map(int, input().split())) 
li=[0 for i in range(1,100001)]
for a in A:
  li[a]+=1
su=sum(A)
for q in range(Q):
  su += li[B[q]] * (C[q]-B[q])
  li[C[q]]+=li[B[q]]
  li[B[q]]=0
  print(su)

Submission status: Runtime Error

Solution code:
N=int(input())
listA=list(map(int, input().split()))
Q=int(input())

listB=[] #appendのために宣言が必要
while True:
    try:
        listB.append(list(map(int,input().split())))

    except:
        break;
        
sum=sum(listA)
numlist=[0]*(4+1)
for i in range(4+1):
  numlist[i]=listA.count(i)
  
for i in range(Q):
  B=listB[i][0]
  C=listB[i][1]
  sum=sum+(C-B)*numlist[B]
  numlist[C]+=numlist[B]
  numlist[B]=0
  # print(numlist)
  print(sum)

Submission status: Runtime Error

Solution code:
N,=list(map(int, input().split()))
A = list(map(int, input().split()))
Q, = list(map(int, input().split()))
B = [0 for i in range(Q)]
C = [0 for i in range(Q)]
for q in range(Q):
  B[q],C[q]=list(map(int, input().split())) 
li=[0 for i in range(100000)]
for a in A:
  li[a]+=1
su=sum(A)
for q in range(Q):
  su += li[B[q]] * (C[q]-B[q])
  li[C[q]]+=li[B[q]]
  li[B[q]]=0
  print(su)

Submission status: Runtime Error

Solution code:
N,=list(map(int, input().split()))
A = list(map(int, input().split()))
Q, = list(map(int, input().split()))
B = [0 for i in range(Q)]
C = [0 for i in range(Q)]
for q in range(Q):
  B[q],C[q]=list(map(int, input().split())) 
li=[0 for i in range(100000)]
for a in A:
  li[a]+=1
su=sum(A)
for q in range(Q):
  su = su + li[B[q]] * (C[q]-B[q])
  li[C[q]]+=li[B[q]]
  li[B[q]]=0
  print(su)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())

dp = [0] *( 10 ** 5)

bc = [input() for _ in range(q)]

for ai in a:
  dp[ai] += 1
ans = 0
for i in range(1,len(dp)):
  ans += dp[i] * i
  
for bci in bc:
  b,c = map(int,bci.split())
  ans += (dp[b] * c) -(dp[b] * b)
  print(ans)
  dp[c] += dp[b] 
  dp[b] = 0

Submission status: Runtime Error

Solution code:
#D
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BandC = [list(map(int, input().split())) for _ in range(Q)]

temp = [0] * 10 ** 5
for i in range(N):
    temp[A[i]] += 1
    
s = sum(A)

for i in range(Q):
    num = BandC[i][0]
    num_temp = temp[num]
    temp[num] = 0
    temp[BandC[i][1]] += num_temp
    s = s - BandC[i][0] * num_temp + BandC[i][1] * num_temp
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
# w = list(map(int, input().split()))
#n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
# data = [list(map(int, input().split())) for i in range(n)]
q = int(input())
#p = list(map(int, input().split()))
#a = list(map(int, input().split()))
# ab_sorted = sorted(ab, key=lambda x: x[0])
bc = [list(map(int, input().split())) for i in range(q)]
# print(min(15*n, 100*(n//10+1), 100*(n//10)+15*(n % 10)))
a_count = collections.Counter(a)
a_list = dict(list(a_count.items()))
#print(a_list)
total = sum(a)
for b, c in bc:
    if b in a_list:
        total += (c-b)*a_list[b]
        num = a_list[b]
        a_list[b] = 0
        if c in a_list:
            a_list[c] += num
        else:
            a_list[c] = num
            
    print(total)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

table = [0] * 100001

c = 0
s = 0

for i in A:
    table[i] += 1
    s += i

print(su,s)

for i in range(Q):
    B, C = map(int,input().split())
    c = table[B]
    s += (C - B) * c
    table[C] += table[B]
    table[B] = 0
    print(s)

Submission status: Runtime Error

Solution code:
import sys
readline = sys.stdin.buffer.readline

from collections import Counter


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

S = sum(A)
C = Counter(A)

ans = []
for _ in range(Q):
    b, c = map(int, input().split())
    count = C[b]
    S += (c - b) * count
    C[b], C[c] = 0, C[c] + count
    ans.append(S)

print('\n'.join(map(str, ans)))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

table = [0] * 100001

c = 0
s = 0
 
for i in A:
    table[i] += 1
    sum += i
    
for i in range(Q):
    B, C = map(int,input().split())
    c = table[B]
    s += (C - B) * c
    table[C] += table[B]
    table[B] = 0
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A_cnt = [0] * 100000
A_sum = 0

for a in A:
    A_cnt[a]+=1

for _ in range(Q):
    B, C = map(int, input().split())
    A_cnt[C] = A_cnt[C] + A_cnt[B]
    A_cnt[B] = 0

    for i, x in enumerate(A_cnt):
        if A_cnt[i] > 0:
            A_sum = A_sum + (i * A_cnt[i])
    print(A_sum)
    A_sum = 0

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(10**8)

N = int(input())
A = list(map(int,(input().split())))
Q = int(input())
num_list = [0]*(N+1)
s = []
for i in range(N):
    num_list[A[i]] += 1
for _ in range(Q):
    s_s = 0
    B,C = map(int,(input().split()))
    num_list[C] += num_list[B]
    num_list[B] = 0
    for j in range(N+1):
        s_s += (j)*num_list[j]
    s.append(s_s)
for i in range(Q):
    print(s[i])

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())
results=[]

S=sum(a)

table=[0]*(10**6)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))
        results.append(ans)
        table[b]=0
        S=ans
    else:
        results.appned(S)

for result in results:
    print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A_cnt = [0] * 10000
A_sum = 0

for a in A:
    A_cnt[a]+=1

for _ in range(Q):
    B, C = map(int, input().split())
    A_cnt[C] = A_cnt[C] + A_cnt[B]
    A_cnt[B] = 0

    for i, x in enumerate(A_cnt):
        if A_cnt[i] > 0:
            A_sum = A_sum + (i * A_cnt[i])
    print(A_sum)
    A_sum = 0

Submission status: Runtime Error

Solution code:
a=list(map(int,input().split()))
q=int(input())
dic={}
for i in range(len(a)):
  dic.setdefault(a[i],1)
  dic[a[i]]+=1
answer=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    dic.setdefault(b,0)
    dic.setdefault(c,0)
    dic[c]+=dic[b]
    answer=answer+dic[b]*c-dic[b]*b
    dic[b] = 0
    print(answer)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
n = int(input())
a = list(map(int,input().split()))
mp = defaultdict(int)
for i in a:
    mp[i] += 1
q = int(input())
for _ in range(q):
    #print(mp)
    s = 0   
    b,c=map(int,input().split())
    if c in mp:
        mp[c] = mp[c]+mp[b]
        mp.pop(b)
    else:
        mp[c] = mp.pop(b)
    for i in mp:
        s += (mp[i])*i
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
num = list(map(int, input().split()))
q = int(input())
change = [0 for i in range(q)]
for i in range(q):
    change[i] = list(map(int, input().split()))

dic = {}
for i in num:
    if i in dic:
        dic[i] += 1
    else:
        dic[i] = 1
ans = 0
for i, j in enumerate(dic):
    ans += j*dic[j]

for i in range(q):
    ans += dic[change[i][0]] * (change[i][1] - change[i][0])
    if change[i][0] in dic:
        if change[i][1] in dic:
            dic[change[i][1]] += dic.pop(change[i][0])
        else:
            dic[change[i][1]] = dic.pop(change[i][0])
    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
hako = [0]*(10^6+1)
ind = list(range(10^6+1))
S = sum(a)
s = []

for i in a:
    hako[i] += 1

for i in range(q):
    b, c = [int(i) for i in input().split()]
    S += (ind[c] - ind[b])*hako[b]
    s.append(S)
    hako[c] += hako[b]
    hako[b] = 0
for i in range(q):
    print(s[i])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
LIST = [0]*(10001)
Q = int(input())


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

for i in A:
    LIST[i]+=1

s = sum(A)

for i in range(Q):
    s += LIST[b[i]]*(c[i]-b[i])
    LIST[c[i]]+=LIST[b[i]]
    LIST[b[i]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    A.replace(B,C,A.count(B))
    print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
LIST = [0]*(10001)
Q = int(input())


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

for i in A:
    LIST[i]+=1

s = sum(A)

for i in range(Q):
    LIST[c[i]]+=LIST[b[i]]
    s += LIST[b[i]]*(c[i]-b[i])
    LIST[b[i]] = 0
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
q = int(input())
hako = [0]*(max(a)+1)
ind = list(range(max(a)+1))
S = sum(a)
s = []

for i in a:
    hako[i] += 1

for i in range(q):
    b, c = [int(i) for i in input().split()]
    S += (ind[c] - ind[b])*hako[b]
    s.append(S)
    hako[c] += hako[b]
    hako[b] = 0
for i in range(q):
    print(s[i])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
for i in range(Q):
    B,C = map(int,input())
    A.replace(B,C,A.count(B))
    print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
S = []
c = 0
s = sum(A)
table = [0] * 100000
 
for i in A:
    table[i] += 1
 
for i in range(Q):
    B, C = map(int,input().split())
    c = table[B]
    s += (C - B) * c
    table[C] += table[B]
    table[B] = 0
    S.append(s)

for s in S:
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
for i in range(Q):
    B,C = map(int,input())
    A.replace(B,C,A.count(B))
    print(sum((A)))

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
summary = sum(A)
d = {}
for a in A:
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
    
Q = int(input())
for _ in range(Q):
  B, C = list(map(int,input().split()))
  if B not in d:
    b = 0
  else:
    b = d[B]
  	d[B] = 0
  if C in d:
    d[C] += b
  else:
    d[C] = b
  summary = summary + b * (C - B)
  print(summary)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
L = [0] * 100001
for i in A:
    L[i] += 1
Q = int(input())
for i in range(Q):
    B, C = map(int, input().split())
    S += (C - B) * L[B]
    L[C] += Ct[B]
    L[B] = 0
    print(S)

Submission status: Runtime Error

Solution code:
 = int(input())
A = list(map(int,(input().split())))
Q = int(input())
num_list = [0]*(N+1)
s = []
for i in range(N):
    num_list[A[i]] += 1
for _ in range(Q):
    s_s = 0
    B,C = map(int,(input().split()))
    num_list[C] += num_list[B]
    num_list[B] = 0
    for j in range(N+1):
        s_s += (j)*num_list[j]
    s.append(s_s)
for i in range(Q):
    print(s[i])

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
summary = sum(A)
d = {}
for a in A:
  if a in d:
    d[a] += 1
  else:
    d[a] = 1
    
Q = int(input())
for _ in range(Q):
  B, C = list(map(int,input().split()))
  if b in d:
    b = 0
  else:
    b = d[B]
  	d[B] = 0
  if C in d:
    d[C] += b
  else:
    d[C] = b
  summary = summary + b * (C - B)
  print(summary)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
n = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1

for i in range(q):
    b, c = map(int, input().split())
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)

Submission status: Runtime Error

Solution code:
from collections import defaultdict
 
n = int(input())
raw_numbers = list(map(int, input().split()))
Q = int(input())
 
total = sum(raw_numbers)
 
numbers = defaultdict(int)
 
for num in raw_numbers:
    numbers[num] += 1
 
for i in range(q):
    b, c = map(int, input().split())
    taken = numbers[b]
    total -= numbers[b]*b
    numbers[c] += numbers[b]
    numbers[b] = 0
    total += taken*c
    print(total)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())
results=[]

S=sum(a)

table=[0]*(10**5)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))
        results.append(ans)
        table[b]=0
        S=ans
    else:
        results.append(S)

for result in results:
    print(result)

Submission status: Runtime Error

Solution code:
import collections

n = int(input())
lis = list(map(int,input().split()))
q = int(input())
total = sum(lis)

nlis = [0] * 10001

for i in lis:
    nlis[i-1] += 1

for i in range(q):
    b, c = map(int,input().split())
    total = total -(b*nlis[b-1]) + (c*nlis[b-1])
    nlis[c-1] += nlis[b-1]
    nlis[b-1] = 0
    print(total)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    answer = sum(A)
    numNumberList = [0] * 100000

    for Ai in A:
        numNumberList[Ai] = A.count(Ai)

    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,list(input().split())))
Q = int(input())
num_list = [0]*(N+1)
s = []
for i in range(N):
    num_list[A[i]] += 1
for _ in range(Q):
    s_s = 0
    B,C = map(int,(input().split()))
    num_list[C] += num_list[B]
    num_list[B] = 0
    for j in range(N+1):
        s_s += (j)*num_list[j]
    s.append(s_s)
for i in range(Q):
    print(s[i])

Submission status: Runtime Error

Solution code:
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)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())
results=[]

S=sum(a)

table=[0]*(10**5)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))
        results.append(ans)
        table[b]=0
        S=ans
    else:
        results.appned(S)

for result in results:
    print(result)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
LIST = [0]*(10001)
sum = sum(A)
for i in A:
    LIST[i]+=1
Q = int(input())
b =[0]*Q
c =[0]*Q
for i in range(Q):
    b[i], c[i] = map(int, input().split())
for i in range(Q):
    LIST[c[i]]+=LIST[b[i]]
    sum+=LIST[b[i]]*(c[i]-b[i])
    LIST[b[i]]= 0
    print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,list(input().split())))
Q = int(input())
num_list = [0]*(N+1)
s = []
for i in range(N):
    num_list[A[i]] += 1
for i in range(Q):
    s_s = 0
    B,C = map(int,(input().split()))
    num_list[C] += num_list[B]
    num_list[B] = 0
    for j in range(N+1):
        s_s += (j)*num_list[j]
    s.append(s_s)
for i in range(Q):
    print(s[i])

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[0]*(10**5+1)
ans=sum(a)
for i in a:
    num[i]+=1
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
S = sum(A)
count = [0 for _ in range(int(1e5))]
for a in A:
    count[a] += 1    

Q = int(input())
for i in range(Q):
    b, c = map(int, input().split())
    S += (c-b) * count[b]
    count[c] += count[b]
    count[b] = 0
    print(S)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    answer = sum(A)
    numNumberList = [0] * 100000
        
    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())

S=sum(a)

table=[0]*(10**5)

for i in range(n):
    table[a[i]]+=1

for i in range(q):
    b, c=map(int, input().split())
    if table[b]!=0:
        cnt=table[b]
        table[c]+=cnt
        ans=S+(cnt*(c-b))
        print(ans)
        table[b]=0
        S=ans
    else:
        print(S)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
LIST = [0]*(10001)
sum = sum(A)
for i in A:
    LIST[i]+=1
Q = int(input())
b =[]
c =[]
for _ in range(Q):
    bs, cs = map(int, input().split())
    b.append(bs)
    c.append(cs)
for i in range(Q):
    LIST[c[i]]+=LIST[b[i]]
    sum+=LIST[b[i]]*(c[i]-b[i])
    LIST[b[i]]= 0
    print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

count = [0]*(10**5)
for i in range(len(A)):
  count[A[i]] += 1

A = list(set(A))
for i in range(Q):
  B, C = map(int, input().split())
  count[C] += count[B]
  count[B] = 0
  ans = 0
  for i in range(len(A)):
    ans += count[A[i]] * A[i]
  print(ans)

Submission status: Runtime Error

Solution code:
k = [i for i in range(1, 10 ** 5 + 1)]
v = [0] * 10 * 5
dic = {key: val for key, val in zip(k, v)}
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  dic[a[i]] += 1
q = int(input())
ans = [sum(a)]
for i in range(q):
  b, c = map(int, input().split())
  ans.append(ans[-1] + (c - b) * dic[b])
  dic[c] += dic[b]
  dic[b] = 0
for i in range(1, q + 1):
  print(ans[i])

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[list(map(int,input().split())) for i in range(Q)]

dp=[0 for i in range(1,10**5+1)]
for i in A:
    dp[i]+=1
S=sum(A)
for i in B:
    S+=(i[1]-i[0])*dp[i[0]]
    print(S)
    dp[i[1]]+=dp[i[0]]
    dp[i[0]]=0

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
for i in range(n):
  dic[a[i]] += 1
q = int(input())
ans = [sum(a)]
for i in range(q):
  b, c = map(int, input().split())
  ans.append(ans[-1] + (c - b) * dic[b])
  dic[c] += dic[b]
  dic[b] = 0
for i in range(1, q + 1):
  print(ans[i])

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    answer = sum(A)
    numNumberDict = {}
    Aset = set(A)
    
    for Ai in Aset:
        numNumberDict[Ai] = A.count(Ai)
        
    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberDict[B]

        numNumberDict[C] = numNumberDict[C] + numNumberDict[B]
        numNumberDict[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B = [[int(i) for i in input().split()] for Q in range(Q)]

p = [0]*100000
for i in range(N):
    p[A[i]] += 1

cnt = 0
for j in range(100000):
    cnt += p[j]*j

for i in range(Q):
    cnt = cnt + (B[i][1]- B[i][0]) * p[B[i][0]]
    
    p[B[i][1]] += p[B[i][0]]
    p[B[i][0]] = 0

    print(cnt)

Submission status: Runtime Error

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())

    C = Counter(A)
    ANS = 0
    for i in C:
        ANS += i * C[i]

    for _ in range(Q):
        B, X = map(int, input().split())
        ANS += X * C[B]
        C[X] += C[B]

        ANS -= B * C[B]
        del(C[B])
        print(ANS)

resolve()

Submission status: Runtime Error

Solution code:
N = int(input())
num_list = list(map(int,input().split()))
q = int(input())

for i in range(len(num_list)):
  ans += num_list[i]

for j in range(q):
  query = list(map(int,input().split()))
  cnt = num_list.count(query)
  ans += (query[1] - query[0]) * cnt
  print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
AList = list(map(int, input().split()))
Q = int(input())
dp = [0]*(10**5)
total = 0
for i in AList:
  dp[i] += 1
  total += i
  
for i in range(Q):
  B,C = map(int, input().split())
  bNum = dp[B]
  dp[B] = 0
  dp[C] += bNum
  cNum = dp[C]
  total = total - B*bNum + C*bNum
  print(total)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
answer = []
counter = [0] * (10**5)
for a in A:
    counter[a] += 1
for _ in range(Q):
    B, C = map(int, input().split())
    counter[C] += counter[B]
    counter[B] = 0
    hoge = [idx*num for idx, num in enumerate(counter)]
    answer.append(sum(hoge))
for res in answer:
    print(res)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
L = []
for i in range(Q):
    B,C = map(int,input().split())
    L.append([B,C])
c = {}
for i in range(1,10002):
    c[i] = 0
for k in A:
    c[k] += 1
s = sum(A)
for i in range(Q):
    if c[L[i][0]] == 0:
        print(s)
    else:
        n = c[L[i][0]]
        c[L[i][1]] += n
        c[L[i][0]] = 0
        s += (L[i][1] - L[i][0])*n
        print(s)

Submission status: Runtime Error

Solution code:
from collections import Counter


def resolve():
    N = int(input())
    A = list(input().split())
    Q = int(input())

    C = Counter(A)
    ANS = 0
    for i in C:
        ANS += i * C[i]

    for _ in range(Q):
        B, X = map(int, input().split())
        ANS += X * C[B]
        C[X] += C[B]

        ANS -= B * C[B]
        del(C[B])
        print(ANS)

resolve()

Submission status: Runtime Error

Solution code:
def resolve():
    N = int(input())
    A = list(map(int, input().split()))
    Q = int(input())
    B = []
    a = sum(A)
    for i in range(1, 10**5):
        B.append(A.count(i))
    for j in range(Q):
        b, c = map(int, input().split())
        a += B[b-1]*(c-b)
        print(a)
        B[c-1] += B[b-1]
        B[b-1] = 0
resolve()

Submission status: Runtime Error

Solution code:
import collections
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
LIST = [0]*(10000+1)
for i in A:
    LIST[i]+=1

sum = sum(A)
for _ in range(Q):
    b, c = map(int, input().split())
    LIST[c]+=LIST[b]
    sum+=LIST[b]*(c-b)
    LIST[b]= 0
    print(sum)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[list(map(int,input().split())) for i in range(Q)]

dp=[0 for i in range(1,10**5+1)]
for i in A:
    dp[i]+=1
S=sum(A)
for i in B:
    S+=(i[1]-i[0])*dp[i[0]]
    print(S)
    dp[i[1]]+=dp[i[0]]
    dp[i[0]]=0

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()));
q = int(input())
aa = 0;
for i in range(n):
    aa += a[i];

b = [input().split() for l in range(q)]

bu = [0]*(100000);

for i in range(100000):
    bu[i] = a.count(i);
    
for x in range(q):
    c = int(b[x][0])
    d = int(b[x][1])
    aa += (d-c)*(bu[c]);
    bu[d] += bu[c];
    bu[c] = 0;
    print(aa);

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

ans = sum(A)
unko = [0]*10000
for i in A:
    unko[i-1] += 1

for i in range(Q):
    B, C = map(int, input().split())

    a = unko[B-1]

    ans = ans + (C-B)*a

    unko[B-1] = 0
    unko[C-1] += a

    print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()));
q = int(input())
aa = 0;
for i in range(n):
    aa += a[i];

b = [input().split() for l in range(q)]

bu = [None]*(100000);

for i in range(100000):
    bu[i] = a.count(i);
    
for x in range(q):
    c = int(b[x][0])
    d = int(b[x][1])
    aa += (d-c)*(bu[c]);
    bu[d] += bu[c];
    bu[c] = 0;
    print(aa);

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int, input().split()))
q=int(input())

S=sum(a)

table=[0]*(10**5)

for i in range(n):
    table[i]+=1

for i in range(q):
    b, c=map(int, input().split())

    cnt=table[b]
    table[c]+=cnt
    ans=S+(cnt*(c-b))
    print(ans)
    table[b]=0
    S=ans

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
num=[[a.count(i+1)] for i in range(n)]
ans=sum(a)
for i in range(q):
    b,c=map(int,input().split())
    ans=ans+(c-b)*num[b-1][0]
    num[c-1][0]+=num[b-1][0]
    num[b-1][0]=0
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
B = []
C = []
for _ in range(Q):
    b, c = map(int, input().split())
    B.append(b)
    C.append(c)

counter = Counter(A)
ans = 0
for num, count in counter.items():
    ans += num * count

for i in range(Q):
    if B[i] in counter:
        ans += counter[B[i]] * C[i] - counter[B[i]] * B[i]
        counter[C[i]] = counter[C[i]] + counter.pop([B[i]])
    print(ans)

Submission status: Runtime Error

Solution code:
import collections
n = int(input())
li = list(map(int,input().split()))
q = int(input())
num_li=[list(map(int,input().split())) for i in range(q)]

c = collections.Counter(li)

li_sum = sum(li)

for i,j in num_li:
    s = 0
    if i in c:
        cnt = c.pop(i)
        c[j] += cnt
    
    li_sum = li_sum - (i*cnt) + (j*cnt)
    print(li_sum)

Submission status: Runtime Error

Solution code:
def main():
    N = int(input())
    A = input()
    Q = int(input())

    answer = sum(list(map(int, A.split())))
    numNumberList = [0] * 10
    
    for i in range(10):
        numNumberList[i] = A.count(str(i))
        
    for i in range(Q):
        B, C = list(map(int, input().split()))
        answer = answer + (int(C) - int(B)) * numNumberList[B]

        numNumberList[C] = numNumberList[C] + numNumberList[B]
        numNumberList[B] = 0

        print(answer)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
a =list(map(int, input().split()))
q = int(input())
b = [0]*q
c = [0]*q
for i in range(q):
    b[i],c[i]=map(int, input().split())

at = [0]*10**5+100
sum = 0

for i in a:
    at[i]= at[i]+1
    sum = sum+i


for  i in range(q):
    m= at[b[i]]
    at[b[i]]=0
    at[c[i]]=at[c[i]]+m

    sum = sum + (c[i]-b[i])*m
    print(sum)

Submission status: Runtime Error

Solution code:
import numpy as np

n = int(input())
a = np.array([int(i) for i in input().split()])
q = int(input())
hako = np.array([0]*(a.max()+1))
ind = np.array(list(range(a.max()+1)))
S = []

for i in a:
    hako[i] += 1

for i in range(q):
    b, c = [int(i) for i in input().split()]
    hako[c] += hako[b]
    hako[b] = 0
    S.append(np.sum(ind*hako))
for i in range(q):
    print(S[i])

Submission status: Runtime Error

Solution code:
n = int(input())
a =list(map(int, input().split()))
q = int(input())
b = [0]*q
c = [0]*q
for i in range(q):
    b[i],c[i]=map(int, input().split())

at = [0]*10**5+2
sum = 0

for i in a:
    at[i]= at[i]+1
    sum = sum+i


for  i in range(q):
    m= at[b[i]]
    at[b[i]]=0
    at[c[i]]=at[c[i]]+m

    sum = sum + (c[i]-b[i])*m
    print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
Q = int(input())
B = [[int(i) for i in input().split()] for Q in range(Q)]

p = [0]*100000
for i in range(N):
    p[A[i]] += 1

for i in range(Q):
    p[B[i][1]] += p[B[i][0]]
    p[B[i][0]] = 0

    cnt = 0
    for j in range(100000):
        cnt += p[j]*j
    print(cnt)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
Q = int(input())

ans = [0]*100000
for a in A:
    ans[a] += a

for i in range(Q):
    b, c = map(int, input().split())
    ans[c] += (ans[b]/b) * c
    ans[b] = 0
    print(int(sum(ans)))

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(1000000000)
input = sys.stdin.readline

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

ans=sum(a)

x=[0]*(10**5)
for i in a:
    x[i]+=1


for _ in range(q):
    b,c=map(int,input().split())
    ans+=x[b]*c-x[b]*b
    print(ans)
    x[c]+=x[b]
    x[b]=0

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split())
q = int(input())
table = [[int(i) for i in input().split()] for q in range(q)]
list2 = []
for item in a:
  mod =item.replace(table[0][0],table[0][1])
  list2.append(mod)
print(sum(list2))
list3 =[]
for item2 in list2:
  mod1 = item2.replace(table[1][0],table[1][0])
  list3.append(mod1)
print(sum(list3))
list4 = []
for item3 in list3:
  mod2 = item3.replace(table[2][0],table[2][1])
  list4.append(mod2)
print(sum(list4))