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:
import numpy as np
n=int(input())
a=np.array(list(map(int,input().split())))
a0,a1= np.unique(a,return_counts=True)
suma=np.sum(a)
a2=np.array([0]*100000)
for j in range(a0.size):
  a2[a0[j]]=a1[j]
#print(suma)
#print(a0)
#print(a1)
q=int(input())
for i in range(q):
  b,c=map(int,input().split())
  sumb=a2[b]
  a2[b]=0
  a2[c]+=sumb
  sumc=sumb*(c-b)
  suma+=sumc
  print(suma)

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):
    D[C[x]]+=D[B[x]]
    tmp=D[B[x]]
    ans=ans+tmp*(C[x]-B[x])
    print(ans)
    D[B[x]]=0

Submission status: Runtime Error

Solution code:
n = int(input())
*a, = map(int,input().split())
g = []
M = 10**5+1
for i in range(1,5):
    g.append((i,0))
g = dict(g); s = 0
for A in a:
    g[A] += 1
    s += A
q = int(input())
for loop in range(q):
    b,c = map(int,input().split())
    if g[b] == 0:
        print(s)
        continue
    s += g[b] * (c - b)
    g[c] += g[b]
    g[b] == 0
    print(s)

Submission status: Runtime Error

Solution code:
import bisect
N = int(input())
a = list(map(int,input().split()))
q = int(input())
bc = list()
sum_a = 0
output = 0
cnt = 0
for i in range(q):
  bc.append(list(map(int,input().split())))
a.sort()
for i in range(q):
  flag = False
  nn = a.index(bc[i][0])
  for j in range(nn-1, len(a)):
    if a[j] == bc[i][0]:
      a[j] = bc[i][1]
      flag = True
    else:
      if flag == True:
        break
  print(sum(a))
  a.sort()

Submission status: Runtime Error

Solution code:
import numpy as np
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
P = np.array([list(map(int,input().split())) for i in range(Q)]).T
B = P[0]
C = P[1]

for i in range(len(B)):
    A=np.where(A==B[i],C[i],A)
    print(A.sum())

Submission status: Runtime Error

Solution code:
N = int(input())
a = list(map(int,input()))
q = int(input())
for i in range(q):
  summ = 0
  b,c = map(int,input().split())
  for i in range(N):
    if(a[i]==b):
      a[i] = c
    summ += a[i]
  print(summ)

Submission status: Runtime Error

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

N = int(input())
A = list(map(int, input().split()))
A_sum = sum(A)
Q = int(input())
BC = []
counter = [0]*10**5
for i in range(Q):
    b, c = map(int, input().split())
    b_count = A.count(b)
    c_count = A.count(c)
    BC.append([b, c])
    counter[b] = b_count
    counter[c] = c_count
    
for b, c in BC:
    A_sum += (c-b)*counter[b]
    counter[c] += counter[b]
    print(A_sum)

Submission status: Runtime Error

Solution code:
from collections import Counter

n = int(input())
A = list(map(int, input().split()))
q = int(input())
cnt_A = dict(Counter(A))
for i in range(q):
    ans = 0
    b, c = map(int, input().split())
    if cnt_A[b] > 0:
        m = cnt_A[b]
        cnt_A[b] = 0
        if c not in cnt_A:
            cnt_A[c] = m
        else:
            cnt_A[c] += m
    ans = sum([k * v for k, v in cnt_A.items()])
    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)]

list=[0]*100000
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum+=(bc[1]-bc[0])*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x

Submission status: Runtime Error

Solution code:
t=int(input())
for i in range(t):
  a=list(map(int,input().strip().split()))
  q=int(input())
  for i in range(q):
    m,n=map(int,input().strip().split())
    for i in range(len(a)):
      if a[i]==m:
        a[i]=n
    print(sum(a))

Submission status: Runtime Error

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l={}
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    if i in l:
        l[i]+=1
    else:
        l[i]=1
for (f,t) in move:
    l[t]+=l[f]
    l[f]=0
    sum = 0
    for k,v in l.items():
        sum += k*v
    print(sum)

Submission status: Runtime Error

Solution code:
n=int(input())
A=sorted(list(map(int,input().split(" "))))
Q=int(input())
for i in range(Q):
  b,c=map(int,input().split(" "))
  for j in range(n):
    if A[j]==b:
      for k in range(n):
        if A[j+k]!=b:
          break
        else:
          A[j+k]=c
      break
  print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
A =  list(map(int, input().split()))
Q = int(input())
B = [0]*(Q+1)
C = [0]*(Q+1)
S = [0]*(Q+1)


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

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

for i in range(N):
    a[A[i]] += 1
    S[0] += A[i]

for i in range(1,Q+1,1):
    S[i] = S[i-1] + a[B[i]]*(C[i]-B[i])
    a[C[i]] = a[C[i]] + a[B[i]]
    a[B[i]] = 0
    print(S[i])

Submission status: Runtime Error

Solution code:
n=int(input())
list_a=list(map(int,input().split()))
k=int(input())
for i in k:
  a,b=map(int,input().split())
  for k in range(n):
   	if list_a[k]==a:
      list_a[k]=b
    else:
      continue
  print(sum(list_a))

Submission status: Runtime Error

Solution code:
from collections import Counter

def update(dic, b, c):
    n = dic.pop(b)
    dic[c] = n + dic.get(c, 0)
    
def calc_diff(dic, b, c):
    return c * dic[b] - b * dic[b]

def calc_total(dic):
    return sum(k * v for k, v in dic.items())

N = int(input())
lis = list(map(int, input().split()))
dic = dict(Counter(lis))
Q = int(input())
total = calc_total(dic)
for _ in range(Q):
    B, C = map(int, input().split())
    total += calc_diff(dic, B, C)
    print(total)
    update(dic, B, C)

Submission status: Runtime Error

Solution code:

def count(A,B,C,s,q):
    if q == s:
        return 0
    else :
        for i,j in enumerate(A):
            if j == B[s]:
                A[i] = C[s]
        print(sum(A))
        return count(A,B,C,s+1,q)


n = int(input())
*A, = 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())
count(A,B,C,0,q)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().strip().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 b:
    for aa in range(n):
        if(a[aa]==i):
            a[aa]=c[aa]
    print(sum(a))

Submission status: Runtime Error

Solution code:
import bisect
N = int(input())
A = list(map(int, input().split()))
Q = int(input())
A.sort()
tmp_sum = sum(A)
for i in range(Q):
    b,c = map(int, input().split())
    left = bisect.bisect_left(A,b)
    if A[left] == b:
        right = bisect.bisect_right(A,b)
        A[left:right] = []
        n_left = bisect.bisect_left(A,c)
        A = A[:n_left] + [c] * (right - left) + A[n_left:]
        tmp_sum = tmp_sum + c *(right-left) - b * (right - left)
        print(tmp_sum)
    else:
        print(tmp_sum)

Submission status: Runtime Error

Solution code:
def getCalc(AAA, BBB):
    sum = 0
    for i in range(len(AAA)):
        sum += AAA[i] * BBB[i]
    return sum


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

AAA = []
BBB = []

for i in range(10 ** 5 + 1):
    count = A.count(i)
    if (count > 0):
        AAA.append(i)
        BBB.append(count)

for i in range(Q):
    if (B[i] in AAA):
        index = AAA.index(B[i])

        if(C[i] in AAA):
            Cindex = AAA.index(C[i])
            AAA[index] = C[i]
            BBB[index] += BBB[Cindex]
            AAA.pop(Cindex)
            BBB.pop(Cindex)
        else:
            AAA[index] = C[i]

        total = getCalc(AAA, BBB)

    print(total)

Submission status: Runtime Error

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

data = {}

def my_sum(d):
    retval = 0

    for i in d.keys():
        retval += d[i] * i
    return retval

for i in A:
    if i in data.keys():
        data[i] += 1
    else:
        data[i] = 1
 
for i in range(Q):
    B, C = map(int, input().split())
 
    if C in data.keys():
        data[C] = data[C] + data[B]
    else:
        data[C] = data[B]
    del data[B]
 
    print(my_sum(data))

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

list=[0]*100000
sum=sum(A)
for i in A:
    list[i]+=1

for bc in BC:
    x=list[bc[0]]
    sum-=bc[0]*x
    sum+=bc[1]*x
    print(sum)
    list[bc[0]]-=x
    list[bc[1]]+=x

Submission status: Runtime Error

Solution code:
n = int(input())
lis_str = input()
for a in range(int(input())):
    b, c = input().split()
    b = int(b)
    c = input(c)
    lis_str = lis_str.replace(b, c)
    list = lis_str.split()
    list_int = [int(i) for i in list]
    print(sum(list_int))
    list_int = []

Submission status: Runtime Error

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

count = [0 for i in range(10**5)]

for a_i in A:
    count[a_i] += 1

bef_ans = 0
for i in range(Q):
    B, C = map(int, input().split())
    tmp = count[B]
    count[B] = 0
    count[C] += tmp
    ans = 0
    if i == 0:
        for j in range(10**5):
            ans += j*count[j]
    else:
        ans = bef_ans - B*tmp + C*tmp
    print(ans)
    bef_ans = ans

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
dA = {}
for a in A:
  dA[a] = 0
for a in A:
  dA[a] += 1
Q = int(input())
for q in range(Q):
  B,C = map(int,input().split())
  sA = set(dA)
  if B in sA:

  s = 0
  for a in dA:
    s += a * dA[a]
  print(s)

Submission status: Runtime Error

Solution code:
from collections import Counter
input = sys.stdin.readline

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

    d = Counter(A)
    # print(d)

    for i in range(Q):
        ans = 0
        b, c = map(int, input().split())
        if b in d.keys():
            d[c] += d[b]
            d[b] = 0
        for k, v in d.items():
            ans += k * v
        # print(d)
        print(ans)


if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N=int(input())
A=''
A+=input.split()
Q=int(input())
B=list(input().split() for i in range(Q))
for j in  range(Q):
    A.replace(B[j][0],B[j][1])
    for k in A:
        c=0
        c +=int(k)
        print(c)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().strip().split()))
q = int(input())
bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
ii = 0
 
for i in b:
    for aa in a:
        if(aa==i):
            aa=c[i]
    i += 1
    print(sum(a))

Submission status: Runtime Error

Solution code:
port numpy as np

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

npdata = np.arange(10**5+1)

kosuu=np.zeros(10**5+1)
kosuumoto=kosuu
for i in range(N):
    a=A[i]
    kosuu[a]+=1

BC_list=[]

for i in range (Q):
    bc=list(map(int,input().split()))
    BC_list.append(bc)
S=0
for i in range(Q):
    b=BC_list[i][0]
    c=BC_list[i][1]
    kosuu[c]=kosuu[c]+kosuu[b]
    kosuu[b]=0
    S=sum(np.multiply(npdata,kosuu))
    print(int(S))

Submission status: Runtime Error

Solution code:
import numpy as np
N=int(input())
numpy_a = np.array(list(map(int, input().split())))
Q=int(input())
for i in np.arange(Q):
  B,C=map(int, input().split())
  numpy_a=np.where(numpy_a == B, C, numpy_a)
  print(np.sum(numpy_a)

Submission status: Runtime Error

Solution code:
# coding: utf-8

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

A.sort()

dict_a = dict()

for a in A:
    if not (a in dict_a):
        dict_a[a] = 1
    else:
        dict_a[a] += 1


Q = int(input())

B = []
C = []

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

for b, c in zip(B, C):
    if (b in dict_a):
        num = dict_a[b]
    
        if not (c in dict_a):
            dict_a[c] = num
        else:
            dict_a[c] += num


        dict_a.pop(b)

        keys = dict_a.keys()
        vals = dict_a.values()

    sumval = 0
    for k, v in zip(keys, vals):
        sumval += k * v

    print(sumval)

Submission status: Runtime Error

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

import numpy as np
numbers = np.arange(1, 1e5, 1)
A = np.array(A)
ans = []

for i in range(Q):
    B, C = map(int, input().split())
    numbers[B-1] = C
    tempA = A
    for i in range(N):
        tempA[i] = numbers[A[i]-1]
    ans.append(np.sum(A))

for i in ans:
    print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))
k = int(input())
s = sum(arr)
for _ in range(k):
	v = 0
    a,b = map(int, input().split())
    for i in range(len(arr)):
      if arr[i] == a:
      	arr[i] = b
       	v += 1
    s = s - (a*v) + (b*v)
    print(s)

Submission status: Runtime Error

Solution code:
import numpy as np
s = list(map(int, input().split()))
n=s[0]
a = np.array(list(map(int, input().split())))
s = list(map(int, input().split()))
q=s[0]
ans={}
for i in range(q):
    s = list(map(int, input().split()))
    if not (s[0],s[1]) in ans:
        a=np.where(a==s[0],s[1],a)
        ans[(s[0],s[1])]=np.sum(a)
        print(ans[(s[0],s[1])])
    else:
        print(ans[s])

Submission status: Runtime Error

Solution code:
x = int(input())
ar = list(map(int, input().split()))
sum_val = sum(ar)

unique_val = dict()

for i in ar:
    if i in unique_val:
        unique_val[i] += 1
    else:
        unique_val[i] = 1

q = int(input())
while(q):
    a, b = map(int, input().split())
    if a in unique_val:
        value = (b-a)*unique_val[a]
        sum_val += value
        if b in unique_val:
            unique_val[b] = unique_val[b] + unique_val.pop(a)
        else:
            unique_val[b] = unique_val.pop(a)
        print(unique_val)
        
    print(sum_val)
    q -= 1

print(sum_val)

Submission status: Runtime Error

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

count = [0 for i in range(10**5)]

for a_i in A:
    count[a_i] += 1

for i in range(Q):
    B, C = map(int, input().split())
    tmp = count[B]
    count[B] = 0
    count[C] += tmp
    ans = 0
    for j in range(10**5):
        ans += j*count[j]
    print(ans)

Submission status: Runtime Error

Solution code:
# coding: utf-8

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

A.sort()

dict_a = dict()

for a in A:
    if not (a in dict_a):
        dict_a[a] = 1
    else:
        dict_a[a] += 1


Q = int(input())

B = []
C = []

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

for b, c in zip(B, C):
    num = dict_a[b]
    
    if not (c in dict_a):
        dict_a[c] = num
    else:
        dict_a[c] += num


    dict_a.pop(b)

    keys = dict_a.keys()
    vals = dict_a.values()

    sumval = 0
    for k, v in zip(keys, vals):
        sumval += k * v

    print(sumval)

Submission status: Runtime Error

Solution code:
from sys import stdin
N=int(input())
A=list(map(int,input().split()))
Q=int(input())
P = [list(map(int,stdin.readline().split())) for i in range(Q)]
for i in range(0,Q):
    X=P[i]
    A=list(sorted(A))
    S=A.count(X[0])
    J=A.index(X[0])
    A[J:J+S]=[X[1]]*S
    print(sum(A))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
 
n = int(input())
a = list(map(int, input().strip().split()))
q = int(input())
bc = [map(int, input().split()) for _ in range(q)]
b, c = [list(i) for i in zip(*bc)]
ii = 0
 
for i in b:
    for aa in a:
        if(aa==i):
            aa=c[i]
    i += 1
    print(sum(a))

Submission status: Runtime Error

Solution code:
_n = int(input())
_list = list(map(int, input().split(" ")))
_int_list = [0 for _i in range(10**5)]
for _int in _list:
  _int_list[_int] += _int
_q = int(input())

for _i in range(_q):
  [_before, _after] = list(map(int, input().split(" ")))
  _num = _int_list[_before] // _before
  _int_list[_before] = 0
  _int_list[_after] += _after * _num
  print(sum(_int_list))

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(" "))
  for j in range(n):
    if A[j]==b:
      for k in range(n):
        if A[j+k]!=b:
          break
        else:
          A[j+k]=c
      break
  print(sum(A))

Submission status: Runtime Error

Solution code:
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

int main() {
  int N; cin >> N;
  vector<int> A(N);
  long long sum = 0;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    sum += A[i];
  }
  int Q; cin >> Q;
  int B[Q];
  int C[Q];
  for (int i = 0; i < Q; i++) {
    cin >> B[i] >> C[i];
  }

  sort(A.begin(), A.end());

  // O(QlogN)
  for (int i = 0; i < Q; i++) {
    int hi = N;
    int lo = -1;
    while (hi - lo > 1) {
      int mid = (hi + lo) / 2;
      if (A[mid] >= B[i]) hi = mid;
      else lo = mid;
    }
    int index = hi; 
    while (A[index] == B[i]) {
      A[index] = C[i];
      sum += (C[i] - B[i]);
      index++; 
    }
    cout << sum << endl;
    sort(A.begin(), A.end());
  }
}

Submission status: Runtime Error

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

l = {}
ans = []
for e in a:
    l[e] = l[e] + 1 if l.get(e) else 1

for _ in range(q):
    b, c = map(int, input().split())
    l[c] = l[c] + l[b] if l.get(c) else l[b]
    del l[b]
    sum = 0
    for k, v in l.items():
        sum += k * v
    ans.append(sum)

for sum in ans:
    print(sum)

Submission status: Runtime Error

Solution code:
import bisect

def func2(l, value):
    i = -1
    c = len(l)
    result = []
    while i < c:
        try:
            i = l.index(value, i + 1)
            result.append(i)
        except:
            return result
    return result

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

for i in range(q):
    b,c = map(int,input().split())
    if a_li not in b:
        print(sum(a_li))
        continue
    indexes = func2(a_li, b)
    for i in indexes:
        a_li[i] = c
    print(sum(a_li))

Submission status: Runtime Error

Solution code:
def binary_search(list, item):
    low = 0
    high = len(list) - 1

    while low <= high:
        mid = (low + high) //2
        guess = list[mid]
        if guess == item:
            return mid
        if guess > item:
            high = mid -1
        else:
            low = mid + 1

    return None

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())
    
for i in range(Q):
    A.sort()
    id = binary_search(A,B[i])
    if id != None:
        A[id] = C[i]
        r = id + 1
        l = id - 1
        while A[r] == B[i]:
            A[r] = C[i]
            r += 1
            if r == N:
                break
        while A[l] == B[i]:
            A[l] = C[i]
            l -= 1
            if l == -1:
                break
    print(sum(A))

Submission status: Runtime Error

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

data = {}

a_set = set(A)

def my_sum(d):
    retval = 0

    for i in d.keys():
        retval += d[i] * i
    return retval

for i in a_set:
    data[i] = A.count(i)
 
for i in range(Q):
    B, C = map(int, input().split())
 
    if C in data.keys():
        data[C] = data[C] + data[B]
    else:
        data[C] = data[B]
    del data[B]
 
    print(my_sum(data))

Submission status: Runtime Error

Solution code:
import sys
def S(): return sys.stdin.readline().rstrip()
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LS(): return list(sys.stdin.readline().rstrip().split())
INF = 1000000000000

import bisect

n = I()
a = LI()
q = I()

cnt = [0]*100000
for i in a:
    cnt[i] += 1
sm = sum(a)

for i in range(q):
    b,c = LI()
    sm += (c-b)*cnt[b]
    if(c != b):
        cnt[c] += cnt[b]
        cnt[b] = 0
    print(sm)

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
a=list(map(int,input().split()))
d=dict(Counter(a))
ans=sum(a)
q=int(input())
for _ in range(q):
    b,c=map(int,input().split())
    ans+=c*d[b]-b*d[b]
    print(ans)
    if c in d.keys(): d[c]+=d[b]
    else: d[c]=d[b]
    d[b]=0

Submission status: Runtime Error

Solution code:
n = int(input())
a = str(input())
q = int(input())
s = []

for i in range(q):
    b,c = map(int,input().split())
    a = [c if j == b else j for j in a]
    s.append(sum(a))

for k in range(q):
    print(s[k])

Submission status: Runtime Error

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l = [0] * (10 ** 5)
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    l[i]+=1
for (f,t) in move:
    l[t]+=l[f]
    l[f]=0
    sum = 0
    for i in range(10**5):
        sum += i*l[i]
    print(sum)

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]
      print(A)
      total.append(sum(A))
      
print(total)
for k in range(Q):
  print(total[k])

Submission status: Runtime Error

Solution code:
N=int(input())
A=str(input())
Q=int(input())
BC=[list(input().split()) for _ in range(Q)]

for i in range(Q):
    if BC[i][0] in A:
        A=A.replace(BC[i][0],BC[i][1])
        B=A.split(' ')
        B_i = [int(s) for s in B]
    print(sum(B_i))

Submission status: Runtime Error

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

T = [0]*100000
#T[i] = A中の数値iの数

for i in A:
  T[i]+=1
S = sum(A)
for i in BC:
  b,c = i
  n = int(T[b])
  sa = c*n - b*n
  S = S + c*n - b*n
  print(S)
  T[b]=0
  T[c]+=n
  #print(S, [b,T[b], 0],[c,T[c],T[c]+T[b]], sa, S+sa)

Submission status: Runtime Error

Solution code:
n=int(input())
a=list(map(int,input().split()))
import collections
coll=collections.Counter(a)
q=int(input())
suma=sum(a)
lis=[0]*max(a)
for i in a:
    lis[i-1]=coll[i] #lisの「番め」の数字が、aに何個あるか

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
A = map(int, input().split())
q = int(input())
move = []
l = [0] * (10 ** 5)
for _ in range(q):
    b, c = map(int, input().split())
    move.append((b,c))
for i in A:
    l[i]+=1
for (f,t) in move:
    l[t]+=l[f]
    l[f]=0
    sum = 0
    for i in range(10**5):
        sum += i*l[i]
    print(sum)

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

cnt=[0]*(10**5)
for a in A:
  cnt[a]+=1
total=sum(A)
for b,c in BC:
  total+=c*cnt[b]
  total-=b*cnt[b]
  cnt[c]+=cnt[b]
  cnt[b]=0
  print(total)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std;
#define int long long
using vec_int = vector<int>;
using P = pair<int,int>;
using T = tuple<int,int,int>;
using ll = long long;
#define rep(i, n) for(int i = 0; i < (int)(n); i++)

int charToInt(char c){
    char zero_num = '0';
    return (int)c - (int)zero_num;
}

signed main(){
    int N;cin>>N;
    vec_int A(N);
    rep(i,N)cin>>A.at(i);

    int Q; cin>>Q;

    vec_int B(Q),C(Q);
    rep(i,Q)cin>>B.at(i)>>C.at(i);

    map<int, int> int_map;


    int init_val = 0;

    rep(i,N){
        if(int_map.count(A.at(i))==0){
            int_map[A.at(i)] = 1;
        }else{
            int_map[A.at(i)] ++ ;
        }
        init_val += A.at(i);
    }

    rep(i,Q){
        int temp = int_map[B.at(i)];
        int_map[B.at(i)] = 0;
        if(int_map.count(C.at(i))==0){
            int_map[C.at(i)] = temp;
        }else{
            int_map[C.at(i)] += temp ;
        }
        init_val -= temp*B.at(i);
        init_val += temp*C.at(i);
        cout<<init_val<<endl;

    }



    return 0;
}

Submission status: Runtime Error

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

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

ls = [0] * 10 ** 5

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

s = sum(A)

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

Submission status: Runtime Error

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

nums = [0]*(10**5)
n = int(input())
li = list(map(int,input().split()))
q = int(input())
BC = []
for _ in range(q):
    b, c = map(int,input().split())
    BC.append([b, c])

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

for i in range(q):
    nums[BC[i][1]] += nums[BC[i][0]]
    nums[BC[i][0]] = 0
    s = 0
    for i in range(1,10**5):
        s += i*nums[i]
    print(s)

Submission status: Runtime Error

Solution code:
N = int(input())

A_in = input()
    
Q = int(input())

for i in range(Q):
    B, C = map(str, input().split())
    
    A_in = A_in.replace(B, C)
    
    #A_val = []
    #for i in A_in.split():
    #    A_val.append(int(i))
    #
    #print(sum(A_val))
     
    S = 0
    for i in A_in.split():
        S += i
    print(S)

Submission status: Runtime Error

Solution code:
data = {}
total = 0
N = int(input())

for a in input().split():
    a = int(a)
    data[a] = data.get(a, 0) + 1
    total += a

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    diff = c - b
    count = data.get(b)
    total += diff * count
    if count: data[c] = data.get(c, 0) + data.pop(b)
    print(total)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


n = int(input())
a = list(map(int, input().split()))
for i in range(int(input())):
    x, y = map(int, input().split())
    for b in range(len(a)):
        if a[b] == x:
            a[b] = y
    print(sum(a))

Submission status: Runtime Error

Solution code:

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

d = dict()
sa = set(a)
ans = sum(a)

for i in sa:
    d[i] = a.count(i)

for j in range(q):
    b,c = map(int,input().split())
    if (b in d) == True:
        ans += (c-b)*d[b]
    if (c in d) == True:
        x = d[c] + d[b]
        d[c] = x
        d[b] = 0
    else:
        if (b in d) == True:
            d[c] = d[b]
            d[b] = 0
    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)]
Alist=[0]*(10**5)
ans=[]

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

sum=0
for i in range(1,len(Alist)):
    sum+=i*Alist[i]

for bc in BC:
    sum=sum-bc[0]*Alist[bc[0]]+bc[1]*Alist[bc[0]]
    Alist[bc[1]]+=Alist[bc[0]]
    Alist[bc[0]]=0
    ans.append(sum)
ans='\n'.join(map(str,ans))
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)]

def calc(A):
    ans = 0
    for i in range(len(A)):
        ans += A[i] * (i + 1)

    return ans



VALUE = [0 for i in range(100)]

for i in range(N):
    VALUE[A[i] - 1] += 1

for i in range(Q):
    VALUE[BC[i][1] - 1] += VALUE[BC[i][0] - 1]
    VALUE[BC[i][0] - 1] = 0
    print(calc(VALUE))

Submission status: Runtime Error

Solution code:
a = input()
q = int(input())
b = list(input().split() for i in range(q))

for i in range(q):
    j = b[i][0]
    k = b[i][1]
    a = a.replace(j,k)
    l = a.split(' ')
    l = [int(i) for i in l]
    print(sum(l))

Submission status: Runtime Error

Solution code:
N = int(input())

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

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

    A_replace = [c if j == b else j for j in A]

    A = A_replace

    print(sum(A_replace))