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 = int(input())
A = list(map(int, input().split()))
Q = int(input())

ans = [0]*10000
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 numpy as np

n = int(input())
a = np.array([int(i) for i in input().split()])
q = int(input())
hako = np.array([0]*(10^6))
ind = np.array(list(range(10^6)))
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())
elements= input().split()
q=int(input())
actions=[]
for i in range(q):
    actions.append(input().split())
 
dict={}
for e in elements:
    count=dict.get(e,0)
    count+=1
    dict[e]=count
 
sum=0
for key in dict.keys():
    sum+=int(key)* dict[key]

for act in actions:
    count=dict.get(act[0],0)
    sum+=(int(act[1])-int(act[0]))*count
    count+=dict.get(act[1],0)
    dict[act[1]]=count
    del dict[act[0]]
 
    print(str(sum))

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(10000):
    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:
g

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
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

hyou = [0]*100001
keisuu = np.array(list(range(0,100001)))

N =  int(input())
a_data = [int(x) for x in input().split()]
Q = int(input())
b_data = [[int(x) for x in input().split()] for _ in range(Q)]

# 数列を分類
for x in a_data:
    hyou[x] += 1
# 操作と合計
for before_value,after_value in b_data:
    # 数値の入れ替え
    hyou[after_value] = hyou[before_value] + hyou[after_value] 
    hyou[before_value] = 0

    # 合計を求めて出力
    hyou_array = np.array(hyou)
    total = np.dot(hyou_array,keisuu)
    print(total)

Submission status: Runtime Error

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

sum = 0
for i in range(N):
    sum += A[i]
dic = cl.Counter(A)

l = []
for _ in range(Q):
    b, c = map(int, input().split())
    x = dic[b]
    if x == None:
        continue
    if b < c:
        sum += (c - b) * x
    else:
        sum -= (b- c) * x
    l.append(sum)
    if c not in dic:
        dic[c] = dic.pop(b)
    else:
        dic[c] = dic.pop(b) + dic[c]

for s in l:
    print(s)

Submission status: Runtime Error

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

sum = 0
kazu = [0] * (10**5)
for i in a:
    kazu[i] += 1
    sum += i
for i in range(q):
    cnt = kazu[b[i]]
    kazu[b[i]] = 0
    kazu[c[i]] += cnt
    sa = c[i] - b[i]
    sum += (sa * cnt)
    print(sum)

Submission status: Runtime Error

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

table = [0] * 100010


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

  sum = 0

  for i in A:
    sum += i

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

Submission status: Runtime Error

Solution code:
_ = int(input())
A = dict()
for a in map(int, input().split()):
  A[a] = A.get(a, 0) + a
  Q = int(input())
  for _ in range(Q):
    b, c = map(int, input().split())
    A[c] = A.get(c, 0) + A.pop(b, 0) // b * c
    print(sum(A.values()))

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(10000):
    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())
lst = list(map(int, input().split()))
q = int(input())
for _ in range(q):
    a, b = map(int, input().split())
    if a in lst:
        summ = 0
        for i in range(n):
            if lst[i] == a:
                lst[i] = b
                summ += lst[i]
            else:
                summ += lst[i]
    print(summ)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
import numpy as np
import numba
sys.setrecursionlimit(1000000000)
MOD = 10 ** 9 + 7


@numba.jit('void (i4[:], i4[:,:])', nopython=True)
def calc(A, BC):
    for i in range(len(BC)):
        A = np.where(A == BC[i][0], BC[i][1], A)
        print(np.sum(A))


def main():
    input()
    A = np.fromstring(input(), 'int', sep=' ')
    Q = int(input())
    BC = np.array([list(map(int, input().split())) for _ in range(Q)])
    calc(A, BC)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

stuck_b = []
stuck_c = []
stuck_sum = []

sum = 0

for cnt_q in range(q):
    sum = 0
    if stuck_b[cnt_q] == b[cnt_q] and stuck_c[cnt_q] == c[cnt_q]:
        print(stuck_sum[cnt_q])
    else:
        for cnt_n in range(n):
            if a[cnt_n] == b[cnt_q]:
                a[cnt_n] = c[cnt_q]
        for num in range(n):
            sum += a[num]
        print(sum)

Submission status: Runtime Error

Solution code:
import collections 
_ = input()
a = [*map(int, input().split())]
q = int(input())
b = [[*map(int,input().split())] for _ in range(q)]
c = collections.Counter(a)
for i,j in b:
    c[j]+=c[i]
    c.pop(i)
    print(sum(k*v for k,v in c.items()))

Submission status: Runtime Error

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

for i in range(Q):
    b, c = map(int, input().split())
    s = 0
    e = N
    h = int((s+e)/2)
    while A[h] != b and s != h :
        if A[h] < b:
            s = h
            h = int((s+e)/2)
        elif A[h] > b:
            e = h
            h = int((s+e)/2)
    X = []
    for j in range(s, e):
        if A[j] == b:
            X.append(j)
            A[j] = c
    print(sum(A))
    if len(X) > 0:
        A = A[:X[0]] + A[X[-1]:]
        s = 0
        e = len(A)
        h = int((s+e)/2)
        while A[h] != c and s != h :
            if A[h] < c:
                s = h
                h = int((s+e)/2)
            elif A[h] > c:
                e = h
                h = int((s+e)/2)
        A = A[:h] + [c]*len(X) + A[h+1:]

Submission status: Runtime Error

Solution code:
N=int(input())
List=list(map(int,input().split()))
Q=int(input())
sum=0
Number=[]
Alphabet=[]
for i in range(Q):
    n,a=input().split()
    Number.append(int(n))
    Alphabet.append(int(a))
for i in range(N):
    sum+=List[i]
for i in range(1,Q+1):
    sum+=(List.count(Number[i]))*(Alphabet[i]-Number[i])
    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)]
from bisect import bisect_left,bisect_right
# def 二分探索(リスト, 要素)
def index(lst, e):
    # Step 1. bisect_left して
    i = bisect_left(lst, e)
    
    # Step 2. 変な値を ValueError で弾いて
    if i == len(lst):
        return -1
    if lst[i] != e:
        return -1
    # Step 3. 値を返す。
    return i
def index2(lst, e):
    # Step 1. bisect_left して
    i = bisect_right(lst, e)
    
    # Step 2. 変な値を ValueError で弾いて
    if i == len(lst)+1:
        return -1
    if lst[i-1] != e:
        return -1
    # Step 3. 値を返す。
    return i
def count_sort(arr):
    max_num = max(arr)
    min_num = min(arr)
    count = [0] * (max_num - min_num + 1)
    for ele in arr:
        count[ele - min_num] += 1

    return [ele for ele, cnt in enumerate(count, start=min_num) for __ in range(cnt)]

A=count_sort(A)

for i in B:
    j=index(A,i[0])
    if not j==-1:
        k=index2(A,i[0])
        del A[k:l]
        for i in range(j,k)
            A.append(i[1])
            
    A=count_sort(A)
    print(sum(A))

Submission status: Runtime Error

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

total = []
for i in range(Q):
  for s in range(n):
    if A[s]==index[i][0]:
      A[s] = index[i][1]
    elif A[0:n] = A[0]
      
      total.append(sum(A))
      
print(total)
for k in range(Q):
  print(total[k])

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a=input()
b=input().split(" ")
c=input()
d=int(c)
point1=[]
for i in range(d):
    a=input().split(" ")
    point1.append(a)
point=[int(i for i in point1)]
for i in range(d):
    if i in int(point[i][0]):
        b[i]=int(point[i][1])
        print(sum(point))
    else:
        print(sum(point))

Submission status: Runtime Error

Solution code:
n = int(input())
A = [int(i) for i in input().split()]

q = int(input())
BC = [[int(i) for i in input().split()] for _ in range(q)]
frag = False

for i in range(q) :
    A.sort()
    frag = False
    b = BC[i][0]
    c = BC[i][1]
    for j in range(q) :
        if A[j] == b :
            frag = True
            A[j] = c
        elif (frag and A[j] != b) or j == q :
            print(sum(A))
            break

Submission status: Runtime Error

Solution code:
import collections

N=int(input())
A=[int(_) for _ in input().split(" ")]
Q=int(input())

c = collections.Counter(A)


#d['k10'] = d.pop('k1')    
    
for i in range(Q):
  B,C=map(int,input().split(" "))
  
  if C in c:
    c[C] += c[B]
    del c[B]
  else:
    c[C] = c.pop(B)
  
  #print(c)
  
  s=list(c.keys())
  j=0
  ans=0
  for v in c.values():
    ans += s[j]*v
    j=j+1
  
  print(ans)

Submission status: Runtime Error

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

for i in range(q):
    b, c = map(int, input().split())
    newlist = a_list
    for j in range(n):
        if newlist[j] == b:
          newlist[j] = c
          ans = sum(newlist)
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

hyou = [0]*100001
keisuu = np.array(list(range(0,100001)))

N =  int(input())
a_data = [int(x) for x in input().split()]
Q = int(input())
b_data = [[int(x) for x in input().split()] for _ in range(Q)]

# 数列を分類
for x in a_data:
    hyou[x] += 1
# 操作と合計
for before_value,after_value in b_data:
    # 数値の入れ替え
    hyou[after_value] = hyou[before_value] + hyou[after_value] 
    hyou[before_value] = 0

    # 合計を求めて出力
    hyou_array = np.array(hyou)
    total = np.dot(hyou_array,keisuu)

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


for i in range(Q):
  B, C = map(int, input().split())
  if count[B] != 0:
    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:
N=int(input())
List=list(map(int,input().split()))
Q=int(input())
sum=0
Number=[]
Alphabet=[]
for i in range(Q):
    n,a=input().split()
    Number.append(int(n))
    Alphabet.append(int(a))
for i in range(N):
    sum+=List[i]
for i in range(1,Q+1):
    sum+=(List.count(Number[i]))*(Alphabet[i]-number[i])
    print(sum)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
B=[]
for i in range(Q):
    B.append(list(map(int,input().split())))
sum=int(0)
AA=[int(0)]
for i in range(100000):
    AA.append(int(0))
#for i in range(100001):
#    AA[i]=A.count(int(i))
#for i in range(5):
#    print(AA[i])
sum=int(0)
for i in range(N):
    sum += A[i]
for i in range(Q):
    AA[(B[i][0])]=A.count(int((B[i][0])))
    A.replace(B[i][0],B[i][1])
#    AA[(B[i][1])]=A.count(int((B[i][1])))
    sum = sum + B[i][1]*AA[(B[i][0])]-B[i][0]*AA[(B[i][0])]
#    AA[(B[i][1])] = AA[(B[i][1])]+AA[(B[i][0])]
#    AA[(B[i][0])] = 0
    print(sum)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
l = list(map(int, input()))
q = int(input())
count = 0

for i in range(q):
	count = 0
	b, c = map(int, input().split())
	for a in l:
		if a == b:
			l[count] = c
		print(sum(l))
		count++

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
BC = [tuple(map(int, input().split())) for _ in range(Q)]

for i in range(Q):
    temp = [A == BC[i][0]] * BC[i][1]
    A = A + temp
    print(A.sum())

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 = list(map(int, input().split()))
    if B not in A:
        print(sum_)
        continue
    sum_ = 0
    for j in range(len(A)):
        if A[j] == B:
            A[j] = C
        sum_ += A[j]
    print(sum_)

Submission status: Runtime Error

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

b=[]
c=[]
for i in range(q):
    d,e=input().split()
    b.append(int(d))
    c.append(int(e))

for i in range(q):
    for j in range(len(b)):
        if a[i] == b[j]:
            a[i]=c[j]
            # if a[-1] ==b[j]:
            #     a[-1]=c[i]
print(sum(a))
    # print(a)
# print(a)
# print(b)
# print(c)

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]*(10^6))
ind = np.array(list(range(10^6)))
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:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    import numpy as np
    A.sort()
    ans = sum(A)
    from bisect import bisect_left,bisect_right
    from collections import Counter
    count = Counter(A)
    for i in range(q):
        b,c = map(int, input().split())
        k = count[b]
        if k != 0
            count[c] += count.pop(b)
        ans += (c-b)* k
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import math
import sys
import itertools
from math import gcd
from math import sqrt
from sys import stdin
def input() : return stdin.readline().rstrip()
def mips():
    return map(int,input().split())
def ii():
    return int(input())
sys.setrecursionlimit(10**9)


'''
N = ii()
ans = ''
def keta(N):
    for i in range(1,15):
        if 26**i 
K = keta(N)
alpha = 0
for i in range(26):
    if 26**(K-1) + (26*i) + 1 <= N and N <= (26**(K-1)) + (26*(i+1)):
        alpha = i
        break
table = [chr(i) for i in range(97,123)]
alpha = table[i]
ans += (K-1) * alpha
last = (N-26**(K-1) - (26*memo) - 1)%26
last = table[last]
ans += last
print(ans)
'''

N = ii()
A = [el for el in mips()]
Q = ii()
check = dict()
for i in range(1,max(A)+1):
    check[i] = 0
for el in A:
    check[el] += 1
for i in range(Q):
    B,C = mips()
    if B in check:
        #ans -= check[B] * B
        if C in check:
            check[C] += check[B]
        else:
            check[C] = check[B]
        ans += check[C] * C
        A.append(-(B*check[B]))
        A.append(C*check[C])
        check[B] = 0
    print(sum(A))

Submission status: Runtime Error

Solution code:

N = int(input())
A = list(map(int, input().split()))
Q = int(input())
BC = [list(map(int, input().split())) for _ in range(Q)] 
ans = [0]*(10**5+1)

for i in range (len(A)):
    ans[A[i]] += 1
maxlen = max(max(A),max(max(BC)))
S = for i in range(maxlen+1):
        S += ans [i]*i
for i in range(Q):
    ans[BC[i][1]] += ans[BC[i][0]]
    S = S + ans[BC[i][1]] *BC[i][1] - ans[BC[i][0]]*BC[i][0]    
    print(S)

Submission status: Runtime Error

Solution code:
N=int(input())
List=list(map(int,input().split()))
Q=int(input())
sum=0
Number=[]
Alphabet=[]
for i in range(Q):
    n,a=input().split()
    Number.append(int(n))
    Alphabet.append(int(a))
for i in range(N):
    sum+=List[i]
for i in range(1,Q+1):
    s+=(List.count(Number[i]))*(Alphabet[i]-number[i])
    print(s)

Submission status: Runtime Error

Solution code:
a=input()
b=input().split(" ")
c=input()
d=int(c)
point1=[]
for i in range(d):
    a=input().split(" ")
    point1.append(a)
point=[int(i for i in point1)]
for i in range(d):
    if i in point[i][0]:
        b[i]=point[i][1]
        print(sum(point))
    else:
        print(sum(point))

Submission status: Runtime Error

Solution code:
N = int(input())
li = list(map(int,input().split()))
Q = int(input())
for i in range(Q):
    B,C = map(int,input().split())
    li = [(C if li[j]==B else li[j]) for j in li]
    sum = 0
    for i in range(N):
        sum = sum+li[i]
    print(sum)

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
P=[]
for i in range(Q):
    a,b=input().split()
    P.append((int(a), int(b))
for j in range(len(A)):
	for i in range(i):
		if P[i]==B[j][0]:
             P[i]=B[j][1]
    print(sum(P))

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 _ in range(Q)]
D={}
ma=0
for i in A:
  ma+=i
  if i not in D:
    D[i]=1
  else:
    D[i]+=1
    
for b,c in B:
  if b in D:
    f=D[b]
    t=D[c]
    D[c]=f+t
    D[b]=0
    ma+=(c-b)*f
    print(ma)

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
BC = [tuple(map(int, input().split())) for _ in range(Q)]

for i in range(Q):
    temp = [A == BC[i][0]] * BC[i][1]
    A = A + temp
    print(sum(A))

Submission status: Runtime Error

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

s = sum(a)

num = [0]*10**5

for i in range(10**5):
    num[i] = a.count(i)

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

Submission status: Runtime Error

Solution code:
import numpy as np
import pandas as pd
N = input()
A = input()
Q = int(input())
BC = [input().split() for _ in range(Q)]

for i in range(Q):
    A = A.replace(BC[i][0], BC[i][1])
    str_list = A.split()
    num_list = list(map(int, str_list))
    print(sum(num_list))

Submission status: Runtime Error

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


N = int(input())
A_list = input().split()
Q = int(input())
num_list = [input().split() for _ in range(Q)]

A_cha = ",".join(A_list)
for i in num_list:
    B = i[0]
    C = i[1]
    A_cha = A_cha.replace(B, C)
    A_result = sum(A_cha.split(","))
    print(A_result)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
q = int(input())
u = []
for i in range(q):
    u.append(tuple(map(int,input().split())))

from collections import Counter
z = Counter(a)

sum = sum(a)
for i in range(q):
    try:
        rev = z.pop(u[i][0])
        sum+=(u[i][1]-u[i][0])*rev
        print(sum)
    except KeyError:
        print(sum)
    if u[i][1] in z:
        z[u[i][1]]+=rev
    else:
        z[u[i][1]]=rev

Submission status: Runtime Error

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

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

t = [0] * (N + 1)
s = sum(A)
for a in A:
    t[a] += 1

for _ in range(Q):
    B, C = map(int, readline().split())
    s -= B * t[B]
    s += C * t[B]
    t[C] += t[B]
    t[B] = 0
    print(s)

Submission status: Runtime Error

Solution code:
#d
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())
D = [0] * (10**5)
ans=sum(A)
for i in A:
    D[i]+=1
for x in range(Q):
    ans+=D[B[x]]*int(C[x]-B[x])
    print(ans)
    D[C[x]]+=D[B[x]]
    D[B[x]]=0

Submission status: Runtime Error

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())))
Q = int(input())
for _ in range(Q):
    B, C = map(int, input().spit())
    if A[0] <= B <= A[-1]:
        for x in A:
            if x == B:
                x = C
        print(sum(A))
    else:
        print(sum(A))

Submission status: Runtime Error

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



for i in range(Q):
    
    A = [C[i] if B[i] == j else j for j in A]
    print(A)
    print(sum(A))

Submission status: Runtime Error

Solution code:
s = int(input()) 

dict={}
num_list = list(map(int, input().split()))

length=len(num_list)
count=[0]*(s+1)
for i in range(length):
    count[num_list[i]]+=1
    dict[num_list[i]]=count[num_list[i]]
    
#print(dict)


sum=0
count=0
n = int(input()) 
for i in range(n):
    a,b = map(int, input().split())  
    if dict.get(a)!=None:
        if dict.get(b)!=None:
            count=dict[b]
            dict[b]=dict[a]+dict[b]
        else:
            dict[b]=dict[a]
        dict[a]=0
    else:
        pass
    #print(dict)
    for k, v in dict.items():
        sum+=k*v
    print(sum)
    sum=0

Submission status: Runtime Error

Solution code:
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    A = list(map(int, input().split()))
    q = int(input())
    import numpy as np
    A.sort()
    ans = sum(A)
    from bisect import bisect_left,bisect_right
    from collections import Counter
    count = Counter(A)
    for i in range(q):
        b,c = map(int, input().split())
        k = count[b]
        count[c] += count.pop(b)
        ans += (c-b)* k
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
from collections import Counter
def change_dict_key(d, old_key, new_key, default_value=None):
    d[new_key] = d.pop(old_key, default_value)

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

q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    if b in a_cnt.keys():
        if c in a_cnt.keys():
            a_cnt[c]+=a_cnt[b]
            tmp=a_cnt[b]
            a_cnt.pop(b)
        else:
            change_dict_key(a_cnt,b,c)
    if i==0:
        ans=0
        for key,value in a_cnt.items():
            ans+=key*value
        print(ans)
    else:
        ans+=(c-b)*tmp
        print(ans)

Submission status: Runtime Error

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

total = sum(A)
c_dic = {}
b_dic = []
for i in range(q):
  b, c = map(int, input().split())
  b_count = A.count(b)
  c_count = A.count(c)
  dic[c] = c_count + b_count
  if b in dic:
    b_count = dic[b]
  diff = c - b
  total += b_count*diff
  print(total)

Submission status: Runtime Error

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

for i in range(q):
    b, c = map(int, input().split())
    newlist = a_list
    for j in range(n):
        if newlist[j] == b:
          newlist[j] = c
          ans = sum(newlist)
    print(ans)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import collections
N = int(input())
A = list(map(int, input().split()))
A_amount = dict(collections.Counter(A))

result = sum(A)

Q = int(input())
for q in range(Q):
    B, C = map(int, input().split())
    B_elem = A_amount.get(B, None)
    if B_elem is not None:
        C_elem = A_amount.get(C, None)
        if C_elem is not None:
            A_amount.pop(B)
            A_amount[C] = B_elem+C_elem
        else:
            A_amount.pop(B)
            A_amount[C] = B_elem

        result += (C-B)*B_elem
    print(A_amount)

    print(result)

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]*(10^5+1))
ind = np.array(list(range(10^5+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=[int(_) for _ in input().split()]
q=int(input())
for i in range(q):
    b,c=map(int,input().split())
    a[b-1]=c
    print(sum(a))

Submission status: Runtime Error

Solution code:
import sys
import collections

ri = lambda: int(sys.stdin.readline())
rm = lambda: map(int, sys.stdin.buffer.readline().split())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
a = rl()
il = list(set(a))
li = collections.Counter(a)
q = ri()
res = []
bc = [rm() for _ in range(q)]
for vvv in bc:
    b, c = vvv
    if b in il:
        if c in il:
            li[c] += li[b]
        else:
            li[c] = li[b]
            il.append(c)
        li[b] = 0
        il.remove(b)
    ans = 0
    for k, v in li.items():
        ans += k*v
    res.append(ans)
for aaa in res:
    print(aaa)

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input().split()))
Q = int(input())
B = [0]*10**5
C = [0]*10**5

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

cou = [0]*10**5
for i in range(1,N+1):
  cou[i] = a.count(i)
allS = sum(a)

for i in range(Q): 
  N_b=0
  N_b = cou[B[i]]
  cou[B[i]] -= N_b
  cou[C[i]] += N_b

  allS = allS + (C[i]-B[i])*N_b
  print(allS)

Submission status: Runtime Error

Solution code:
for q in range(Q):
    B, C = map(int, input().split())
    if B in A:
        A2 = [i for i, x in enumerate(A) if x == B]
        A = [C if i == B else i for i in A]
        print(sum(A))
    else:
        print(sum(A))

Submission status: Runtime Error

Solution code:
import sys
#sys.exit()
#sorted( ,reverse=True)
N = int(input())
#S,T = map(int, input().split())
A = list(map(int, input().split()))
#A = [int(input()) for _ in range(N)]
Q = int(input())
#cnt = 0
#cnt += 1

ans = sum(A)
D = [0,]*100000
for i in range (100000):
    D[i] = A.count(i)

for _ in range(Q):
    B,C = map(int, input().split())
    if D[B] > 0:
        ans += D[B]*(C-B)
        D[C] += D[B]
        D[B] = 0
    #print(D[0:10])
    print(ans)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np
input = sys.stdin.readline


N = int(input())
A_list = input().split()
Q = int(input())
num_list = [list(input().split()) for _ in range(Q)]

A_cha = ",".join(A_list)
for i in num_list:
    B = i[0]
    C = i[1]
    A_cha = A_cha.replace(B, C)
    A_result = sum([int(A) for A in A_cha.split(",")])
    print(A_result)

Submission status: Runtime Error

Solution code:
import numpy as np
from numba import njit

@njit
def main():
    n = int(input())
    A = np.array(list(map(int, input().split())))
    q = int(input())
    for _ in range(q):
        b, c = map(int, input().split())
        np.where(A == b, c, A)
        print(np.sum(A))


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
P=[]
for i in range(5):
    a,b=input().split()
    list.append((int(a), int(b))
a=len(A)
for j in range(M):
    for i in range(i):
        if P[i]==B[j][0]:
                P[i]=B[j][1]
    print(sum(P))

Submission status: Runtime Error

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

x = 0

while x < q:
    a = [c[x] if i > b[x] else i for i in a]
    print(sum(a))
    x += 1

Submission status: Runtime Error

Solution code:
n = int(input())
als = list(map(int,input().split()))
ans = sorted(als)
ans02 = []
cot = 0
q = int(input())
for i in range(q):
    b, c = map(int,input().split())
    if b in ans:
      cot = ans.count(b) 
    if b in ans02:
      ans02 =[ans02[j] if ans[j] != b else c for j in range(len(ans02))]  
    print(sum(ans)+(c-b)*cont+sum(ans02))

Submission status: Runtime Error

Solution code:
#d
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())
D = [0] * (10**5)
ans=sum(A)
for i in A:
    D[i]+=1
for x in range(Q):
    ans+=D[B[x]]*(C[x]-B[x])
    print(ans)
    D[C[x]]+=D[B[x]]
    D[B[x]]=0

Submission status: Runtime Error

Solution code:
def sumdic(dic):
  s=0
  for k, v in dic.items():
    s+=int(k)*v
  return s

a=int(input())
dic={}
for x in input().split():
  if x in dic:
    dic[x]+=1
  else:
    dic[x]=1
s=sumdic(dic)
d=int(input())
for i in range(d):
  (b,c)=[x for x in input().split()]
  if c in dic:
    dic[c]+=dic[b]
    s+=(int(c)-int(b))*dic[b]
    dic.pop(b)
  elif b in dic:
    dic[c]=dic[b]
    s+=(int(c)-int(b))*dic[b]
    dic.pop(b)
  print(s)