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())
s=[]
s=input().split()
q=int(input())
for i in range(q):
 x,y=map(int,input().split())
 s=re.sub(x,y,s)

for j in range(q)
 ans+=int(s[j])

print(ans)

Submission status: Runtime Error

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

bckt = [0]*(n+1)
for i in a:
    bckt[i] += 1
    
s = sum(a)

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

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())
A = np.array([int(a) for a in input().split()])
ans = sum(A)
lst = np.zeros(10**5)
for a in A:
    lst[a] += 1
   
for q in range(int(input())):
    B,C = map(int,input().split())
    ans += (C - B) * lst[B]
    print(int(ans))
    lst[C] += lst[B]
    lst[B] = 0

Submission status: Runtime Error

Solution code:
import collections

N = int(input())
A = list(map(int, input().split()))  # (N,)
Q = int(input())
B, C = zip(*[list(map(int, input().split())) for _ in range(Q)])  # (Q,), (Q,)

value_count_map = collections.defaultdict(int)
value_sum_map = collections.defaultdict(int)
for a in A:
    value_count_map[a] += 1
    value_sum_map[a] += a

for b, c in zip(B, C):
    b_cnt = value_count_map[b]
    value_count_map[c] += b_cnt
    value_sum_map[c] += c * b_cnt
    if b in value_count_map:
        del value_count_map[b]
        del value_sum_map[b]
    print(sum(v for v in value_sum_map.values()))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

num = []

for i in range(1, 100000):
	num.append(A.count(i))

S = sum(A)
slist = []

for i in range(Q):
	B, C = map(int, input().split())
	numb = num[B-1]
	S += (C - B) * numb
	num[C-1] += numb
	num[B-1] = 0
	slist.append(S)

[print(i) for i in slist]

Submission status: Runtime Error

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

num = []

for i in range(1, 100000):
	# 行列Aの要素にiが何個あるか数えてnumに追加
	num.append(A.count(i))

S = sum(A)
slist = []

for i in range(Q):
	B, C = map(int, input().split())
	numb = num[B-1]
	S += (C - B) * numb
	num[C-1] += numb
	num[B-1] = 0
	slist.append(S)

[print(i) for i in slist]

Submission status: Runtime Error

Solution code:
# ans

N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100000
for i in A:
  cnt[i] += 1
Q = int(input())
S = sum(A)

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

Submission status: Runtime Error

Solution code:
# ans

N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100000
for i in range(N):
  cnt[A[i]] += 1
Q = int(input())
S = sum(A)

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

Submission status: Runtime Error

Solution code:
# ans

N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100000
for i in range(N):
  cnt[i] += 1
Q = int(input())
S = sum(A)

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

Submission status: Runtime Error

Solution code:
# ans

N = int(input())
A = list(map(int, input().split()))
cnt = [0] * 100000
for i in range(A):
  cnt[i] += 1
Q = int(input())
S = sum(A)

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

Submission status: Runtime Error

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

counter = dict(collections.Counter(a))

for i in range(q):
    s = 0
    counter[c[i]] += counter[b[i]]
    counter[b[i]] = 0
    for key, val in counter.items():
        s += key * val
    print(s)

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 23 21:04:33 2020

@author: naoki
"""

N = int(input()) # input the row A length "N" 
A = list(map(int,input().split())) # input the row of A 
Q = int(input()) # input the number of trying operation 
BC = [list(map(int,input().split())) for i in range(Q)]
S = 0
num = [0 for i in range(100001)] 
for i in A:
    S += i # Sum of all conponents of the row "A"
    num[i]+=1 

for B,C in BC:
    S = S + (C - B) * num[B+1]
    num[C+1] += num[B+1]
    num[B+1] = 0 
    print(S)

Submission status: Runtime Error

Solution code:
from collections import Counter


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter(A)
    q = int(input())
    s = sum(a)
    for _ in range(q):
        b, c = map(int, input().split())
        s -= C[b]*b
        s += C[b]*c
        C[c] += c[b]
        c[b] = 0
        print(s)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
rom collections import Counter


def main():
    n = int(input())
    a = list(map(int, input().split()))
    c = Counter
    q = int(input())
    s = sum(a)
    for _ in range(q):
        b, c = map(int, input().split())
        s -= C[b]*b
        s += C[b]*c
        C[c] += c[b]
        c[b] = 0
        print(s)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
#D                 more faster method than above               see it ..........
l = lambda : list(map(int,input().split().strip()))
n = int(input())
a = l
t = [0]*(10**5+1)
s = 0
for i in a:
    s += i
    t[i] += 1
q = int(input())
for _ in range(q):
    b,c = l
    s += (c-b)*t[b]
    t[c] += t[b]
    t[b] = 0
    print(s)

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split(" "))
nums = {}
sum = 0
for e in a:
    if e in nums:
         nums[e] += 1
    else:
        nums[e] = 1
    sum += e
q = int(input())
for i in range(q):
    [b, c] = map(int, input().split())
    cnt = nums[b]
    sum -= cnt * b
    sum += cnt * c
    del nums[b]
    if c in nums:
        nums[c] += cnt
    else:
        nums[c] = cnt
    print(sum)

Submission status: Runtime Error

Solution code:
n = int(input())
a = map(int, input().split(" "))
nums = {}
sum = 0
for e in a:
    if e in nums:
         nums[e] += 1
    else:
        nums[e] = 1
    sum += e
q = int(input())
for i in range(q):
    [b, c] = map(int, input().split())
    cnt = nums[b]
    sum -= cnt * b
    sum += cnt * c
    nums[b] = 0
    if c in nums:
        nums[c] += cnt
    else:
        nums[c] = cnt
    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 _ in range(Q)]
numList = [0]*10**5
res=sum(A)
for i in A:
    numList[i] += 1



for i in range(Q):
    dev = B[i][1]-B[i][0]
    res += numList[B[i][0]]*dev
    numList[B[i][1]] +=numList[B[i][0]]
    numList[B[i][0]] = 0
    print(res)

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)]
numList = [0]*10**5
sum=0
for i in A:
    numList[i] += 1
    sum+=i



for i in range(Q):
    dev = B[i][1]-B[i][0]
    sum += numList[B[i][0]]*dev
    numList[B[i][1]] +=numList[B[i][0]]
    numList[B[i][0]] = 0
    print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
Q = int(input())
numList = [0]*10**5
sum=0
for i in A:
    numList[i] += 1
    sum+=i

B = [list(map(int,input().split())) for _ in range(Q)]

for i in range(Q):
    dev = B[i][1]-B[i][0]
    sum += numList[B[i][0]]*dev
    numList[B[i][1]] +=numList[B[i][0]]
    numList[B[i][0]] = 0
    print(sum)

Submission status: Runtime Error

Solution code:
form collections import Counter
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
counter=Counter(A)
sum_res=sum(A)
for i in range(Q):
  b,c=map(int,input().split())
  sum_res-=counter[b]*b
  sum_res+=counter[b]*c
  counter[c]+=counter[b]
  counter[b]=0
  print(sum_res)

Submission status: Runtime Error

Solution code:


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10**5)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    # print(nizi)
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] += nizi[B]
    nizi[B] = 0
    print(sumall)

Submission status: Runtime Error

Solution code:


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    # print(nizi)
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] += nizi[B]
    nizi[B] = 0
    print(sumall)

Submission status: Runtime Error

Solution code:


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (10**5)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] =+ nizi[B]
    nizi[B] = 0
    print(sumall)

Submission status: Runtime Error

Solution code:


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (max(A)+1)
for i in range(len(A)):
    # print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] =+ nizi[B]
    nizi[B] = 0
    print(sumall)

Submission status: Runtime Error

Solution code:


N = int(input())
A = list(map(int, input().split()))
Q = int(input())
nizi = [0] * (max(A)+1)
for i in range(len(A)):
    print(A[i], A)
    nizi[A[i]] += 1
sumall = sum(A)
for q in range(Q):
    B, C = map(int, input().split())
    sumall += (C-B) * nizi[B]
    nizi[C] =+ nizi[B]
    nizi[B] = 0
    print(sumall)

Submission status: Runtime Error

Solution code:
def main():
    n = int(input())

    As = list(map(int, input().split()))
    nums = [0] * (10**5)
    ans = 0
    for a in As:
        ans += a
        nums[a] += 1

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


if __name__ == "__main__":
    main()

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
n = int(input())
a =list(map(int,input().split()))
s =sum(a)
x = [0]*100001
for i in a:
    x[i] += 1

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

Submission status: Runtime Error

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

num_count = [0]*(10**5+1)
tmp_sum = sum(A)
for i in A:
    num_count[i] += 1

for q in range(int(input()):
    B,C = map(int,input().split())
    gain = C*(num_count[B])
    loss = B*(num_count[B])
    num_count[C] += num_count[B]
    num_count[B] = 0
    tmp_sum = tmp_sum + gain - loss
    print(tmp_sum)

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
from collections import Counter
sys.setrecursionlimit(10**8)
INF = float("inf")


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def main():
    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())
    # 個数を持つ
    # Biの個数 O(1),C[i]の個数 O(1) 更新 O(1) 総和O(1)

    group = [None]*(10**5)
    uf = UnionFind(N)

    # union_find
    # N個の数に対して、i番目の数がどのグループに属するのかを示す
    # group
    # j (< 10**5)に対して、jがつけられているグループの根の番号を示す

    for i, a in enumerate(A):
        # グループにつけられている番号がaであるようなグループはあるか？
        # なければ自信のグループにつけられている番号として保持
        # あればそこに合流
        if group[a] is None:
            group[a] = i
        else:
            uf.union(i, group[a])
            group[a] = uf.find(i)
    # print(A)
    # print(group)
    # print(uf.parents)

    tot = sum(A)
    for i in range(Q):
        if group[B[i]] is None:
            b = 0
        elif group[C[i]] is None:
            b = uf.size(group[B[i]])
            group[C[i]] = uf.find(group[B[i]])
            group[B[i]] = None
        else:
            b = uf.size(group[B[i]])
            uf.union(group[B[i]], group[C[i]])
            group[C[i]] = uf.find(group[B[i]])
            group[B[i]] = None
        tot = tot - B[i]*b + C[i]*b
        print(tot)
        # print(group)
        # print(uf.parents)

    return


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

void printArray(vector<int> arr) {
	for (int a: arr) cout << a << " ";
	cout << '\n';
}

int a[100001];

int main() {
	std::ios_base::sync_with_stdio(false);
	int n, q;
	cin >> n;
	map<int, int> count;
	ll sum = 0;
	for (int i = 0; i<n; i++) {
		cin >> a[i];
		sum += a[i];
		if (count.find(a[i]) == count.end()) count[a[i]] = 1;
		else count[a[i]]++;
	} 
	cin >> q;
	for (int _=0; _<q; _++) {
		int b,c,temp;
		cin >> b >> c;
		if (count.find(b) != count.end()) {
			temp = count[b];
			count.erase(b);
			sum = sum - b*temp + c*temp;
			if (count.find(c) == count.end()) count[c] = temp;
			else count[c] += temp;
		}
		cout << sum << '\n';
	}
	
}

Submission status: Runtime Error

Solution code:
#from collections import defaultdict

n = int(input())
cnt =[0]*10**5

l = list(map(int, input().split()))

for i in l:
    cnt[i] += 1
    
sums = sum(l)
    
m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    diff = (b-a)*cnt[a]
    sums += diff
    print(sums)
    
    cnt[b] += cnt[a]
    cnt[a] = 0

Submission status: Runtime Error

Solution code:
N=int(input())
number=list(map(int,input().split()))
sum_number=sum(number)
Q=int(input())
number_count=[0]*100000
for i in range(100000):
    number_count[i]=number.count(i)

for _ in range(Q):
    B,C=map(int,input().split())
    sum_number += (C-B)*number_count[B]
    number_count[C] += number_count[B]
    number_count[B] -= number_count[B]
    print(sum_number)

Submission status: Runtime Error

Solution code:
N=int(input())
number=list(map(int,input().split()))
sum_number=sum(number)
Q=int(input())
number_count=[0]*100000
for i in range(100000):
    number_count[i]=number.count(i)

for j in range(Q):
    B,C=map(int,input().split())
    sum_number += (C-B)*number_count[B]
    number_count[C] += number_count[B]
    number_count[B] -= number_count[B]
    print(sum_number)

Submission status: Runtime Error

Solution code:
n = int(input())

l = list(map(int, input().split()))

sort(l)

m = int(input())

for i in range(m):
    a,b = map(int, input().split())
    
    l = [b if j == a else j for j in l]

    print(sum(l))

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:
n = int(input())
a = list(map(int, input().split()))

mx = max(a)
table = [0] * (mx + 1)
for i in a:
    table[i] += 1

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

Submission status: Runtime Error

Solution code:
import time
import random
def run(n, seq, num, trials):
    '''
    あなたは、 N 個の正整数 A1 , A2 , ⋯ , AN からなる数列 A を持っています。 
    あなたは、これから以下の操作を Q 回、続けて行います。 
    i 回目の操作では、値が Bi である要素すべてを Ci に置き換えます。 すべての 
    i ( 1 ≤ i ≤ Q) に対して、 i 回目の操作が行われた後の数列 A のすべての要素の和、 Si を求めてください。

    [制約]
    入力は全て整数
    1 ≤ N ,Q ,Ai ,Bi ,Ci ≤ 10^5 
    Bi ≠ Ci

    [入力例]
    4
    1 2 3 4
    3
    1 2
    3 4
    2 4
    
    [出力例]
    11
    12
    16
    '''

    #  TODO 下全部 LTE で不正解

    # count_dict = {i+1 : seq.count(i+1) for i in range(100000)}
    # count_list = [seq.count(i+1) for i in range(100000)]
    # count_list = [0 if i+1 not in seq else seq.count(i + 1) for i in range(100000)]
    # count_list = [count_list[i-1] if i not in seq or count_list[i-1] != 0 else seq.count(i) for i in seq]

    count_list = [0] * 100000
    # for i in seq:
    #     count_list[i-1] = count_list[i-1] if count_list[i-1] != 0 else seq.count(i)

    # seq = []
    # for i in range(10000):
    #     seq.append(random.randint(1, 100000))
    
    for i in set(seq):
        count_list[i] = seq.count(i)
    
    # count_list = []
    # for i in range(100000):
    #     count_list.append(0 if i+1 not in seq else seq.count(i+1))

    # s_time = time.time()
    _sum = sum(i*v for i,v in enumerate(count_list))
    # print(time.time() - s_time)
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b] = 0
        count_list[c] += _val

if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

Submission status: Runtime Error

Solution code:
def run(n, seq, num, trials):

    count_list = [0] * 100000
    for i in set(seq):
        count_list[i] = seq.count(i)
    _sum = sum(i*v for i,v in enumerate(count_list))
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b] = 0
        count_list[c] += _val

if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

Submission status: Runtime Error

Solution code:
def run(n, seq, num, trials):
    count_list = [0] * 100000
    for i in set(seq):
        count_list[i] = seq.count(i)

    _sum = sum(i*v for i,v in enumerate(count_list))
    for i in range(num):
        b, c = trials[i]
        _val = count_list[b]
        if _val == 0:
            print(_sum)
            continue
        _sum += (c-b)*_val
        print(_sum)
        count_list[b] = 0
        count_list[c] += _val

if __name__ == '__main__':
    n = int(input())
    seq = list(map(int, input().split()))
    num = int(input())
    trials = [map(int, input().split()) for _ in range(num)]
    run(n, seq, num, trials)

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

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

Submission status: Runtime Error

Solution code:
from collections import defaultdict

N = int(input())
_A = [*map(int, input().split())]
Q = int(input())
val = [[*map(int, line.split())] for line in open(0)]

A = defaultdict(int)
for _a in _A:
    A[_a] += 1

ans = sum(_A)
for v in val:
    if v[0] in A.keys():
        num = A[v[0]]
        A[v[0]] = 0
        A[v[1]] += num
        ans += (v[1] - v[0]) * num
    print(ans)

Submission status: Runtime Error

Solution code:
import sys

sys.setrecursionlimit(10**7)

n = int(input())

a = list(map(int, input().split()))
a_num = [0] * 100000
for a_i in a:
    a_num[a_i] += 1

q = int(input())

ans = sum(a)


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

Submission status: Runtime Error

Solution code:
n = int(input())

a = list(map(int, input().split()))
a_num = [0] * 100000
for a_i in a:
    a_num[a_i] += 1

q = int(input())

ans = sum(a)


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

Submission status: Runtime Error

Solution code:
n = int(input())

a = list(map(int, input().split()))
a_num = [0] * 100000
for a_i in a:
    a_num[a_i - 1] += 1

q = int(input())

ans = sum(a)


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

Submission status: Runtime Error

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

list_a.sort()

counts = []

for i in range(1, 100000):
    if i in list_a:
        counts.append(list_a.count(i))
    else:
        counts.append(0)

q = int(input())
ans = sum(list_a)

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

    ans += counts[b-1] * (c - b)

    counts[c-1] += counts[b-1]
    counts[b-1] = 0

    print(ans)

Submission status: Runtime Error

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

list_a.sort()

counts = []

for i in range(100000):
    if i in list_a:
        counts.append(list_a.count(i))
    else:
        counts.append(0)

q = int(input())
ans = sum(list_a)

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

    ans += counts[b] * (c - b)

    counts[c] += counts[b]
    counts[b] = 0

    print(ans)

Submission status: Runtime Error

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

list_a.sort()

counts = []

for i in range(100000):
    counts.append(list_a.count(i))

q = int(input())
ans = sum(list_a)

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

    ans += counts[b] * (c - b)

    counts[c] += counts[b]
    counts[b] = 0

    print(ans)

Submission status: Runtime Error

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

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

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

for i in range(Q):
    b = B[i]
    c = C[i]

    SUM = SUM - b * num[b]
    SUM = SUM + c * num[b]
    num[c] += num[b]
    num[b] = 0
    print(SUM)

Submission status: Runtime Error

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

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

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

for i in range(Q):
    b = B[i]
    c = C[i]

    SUM = SUM - b * num[b]
    SUM = SUM + c * num[c]
    num[c] += num[b]
    num[b] = 0
    print(SUM)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int,input().split()))
dist = [0]*max(a)
ans = sum(a)
for i in a:
    dist[i-1] += 1

q = int(input())
for i in range(q):
    b,c = map(int,input().split())
    if dist[b-1] != 0:
        dist[c-1] += dist[b-1]
        ans += dist[b-1] * (c-b)
        dist[b-1] = 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())

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

cnt = Counter(a)

s = sum(cnt)

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

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 i in range(q):
	B, C = map(int, input().split())
	b.append(B)
	c.append(C)

cnt = Counter(a)

s = sum(arr)

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

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 i in range(q):
	B, C = map(int, input().split())
	b.append(B)
	c.append(C)

arr = Counter(a)

s = sum(arr)

for i in range(q):
	s -= b[i]*arr[b[i]]
	s += c[i]*arr[c[i]]
	ans[c[i]] += ans[b[i]]
	ans[b[i]] = 0
	print(s)

Submission status: Runtime Error

Solution code:
#Replacing
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
Q = int(input())
u, count = np.unique(A,return_counts=True)
asum = A.sum()
for i in range(Q):
    B, C = list(map(int, input().split()))
    posB = np.where(u==B)
    posC = np.where(u==C)
    asum += int(count[posB]) * (C-B)
    if posC==[]:
        u[posB] = B
    else :
        count[posC] += count[posB]
        u = np.delete(u,posB)
        count = np.delete(count,posB)
    print(asum)
    # print(u)
    # print(count)

Submission status: Runtime Error

Solution code:
n=int(input())
arr=list(map(int,input().split()))
ans=[0]*100000
for i in range(n):
    ans[arr[i]]+=1
sm=sum(arr)
q=int(input())
for i in range(q):
    x,y=(map(int,input().split()))
    sm=sm-(x*ans[x])
    sm=sm+(y*ans[x])
    ans[y]+=ans[x]
    ans[x]=0
    print(sm)

Submission status: Runtime Error

Solution code:
n=int(input())
arr=list(map(int,input().split()))
ans=[0]*100000
for i in range(n):
    ans[arr[i]]+=1
sm=sum(arr)
q=int(input())
for i in range(q):
    x,y=(map(int,input().split()))
    ans[y]+=ans[x]
    sm=sm-(x*ans[x])
    sm=sm+(y*ans[x])
    ans[x]=0
    print(sm)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

from functools import lru_cache
@lru_cache(maxsize=None)
def idx(x):
    return bs.bisect_left(a2,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(b)
        if a3[b_idx]!=0:
            if c in a2:
                c_idx=idx(c)
            else:
                a2.append(c)
                a3.append(0)
                c_idx=-1
            sum+=(c-b)*a3[b_idx]
            a3[c_idx]+=a3[b_idx]
            a3[b_idx]=0
#        del a2[b_idx]
#        del a3[b_idx]
#    print(a2,a3)
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
q=int(input())
sum=sum(a)
a2,a3=[],[]
import bisect as bs

for i in set(a):
    a2.append(i)
    a3.append(a.count(i))

#print(sum)
#print(a2,a3)

from functools import lru_cache
@lru_cache(maxsize=None)
def idx(x):
    return bs.bisect_left(a2,x)

for i in range(q):
    b,c=map(int,input().split())
    if b in a2:
        b_idx=idx(b)
        if c in a2:
            c_idx=idx(c)
        else:
            a2.append(c)
            a3.append(0)
            c_idx=-1
        sum+=(c-b)*a3[b_idx]
        a3[c_idx]+=a3[b_idx]
        a3[b_idx]=0
#        del a2[b_idx]
#        del a3[b_idx]
#    print(a2,a3)
    print(sum)

Submission status: Runtime Error

Solution code:
import numpy
a=int(input())
b=input()
c=int(input())
for i in range(c):
  d,e=map(int,input().split())
  b=b.replace(d,e)

d=list(map(int,d.split()))

print(numpy.sum(d))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
query = [list(map(int,input().split())) for _ in range(q)]

all_sum  = sum(a)

cnt = {}
for i in a:
    if i not in cnt:
        cnt[a] = 0
    cnt[a] += 1

for (b, c) in query:
    if b in cnt:
        all_sum -= b * cnt[b]
        all_sum += c * cnt[b]
        if c not in cnt:
            cnt[c]=0 
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(all_sum)