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=[int(i) for i in input().split()]
Q=int(input())
X=[i+1 for i in range((10**5))]
dic={}
ans=sum(A)
for i in A:
	try:
		dic[i]+=1
	except:
		dic.setdefault(i,1)
for i in range(Q):
	[B,C]=[int(i) for i in input().split()]
	ans+=dic[B]*(C-B)
	try:
		dic[C]+=1
	except:
		dic.setdefault(C,1)
	print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())
p = input().split()
p_int = [int(n) for n in p]
Q = int(input())
for i in range(Q):
  B, C = map(int, input().split())
  p_int[p_int.index(B)] = C
  print(sum(p_int))

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int, input().split()))
q = int(input())
l = [0]*10**5
num = sum(A)
for i in A:
    l[i] = l[i] + 1


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

Submission status: Runtime Error

Solution code:
n = int(input())
A = list(map(int,input().split()))
count = [0]*(10**5)
for a in A:
    count[a] += 1
    
ans = 0
for i in range(10**5):
    if count[i]==0:
        continue
    else:
        ans += i*count[i]

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

Submission status: Runtime Error

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

def main():
    N = int(input())
    A = list(map(int,input().split()))
    Q = int(input())
    BC = [list(map(int, input().split())) for _ in range(Q)]

    d = dict()

    # dictに格納 O(1)
    for a in A:
        try:
            d[a] += 1
        except KeyError:
            d[a] = 1
    
    ans = 0

    # 一旦ans出す
    for dat in d.items():
        ans += dat[0]*dat[1]

    # B,Cの操作
    for bc in BC:
        try:
            d[bc[1]] += d[bc[0]]
        except KeyError:
            try:
                d[bc[1]] = d[bc[0]]
            except KeyError:
                d[bc[1]] = 0
        finally:
        # 増減分だけ変更
            ans += bc[1]*d[bc[0]] - bc[0]*d[bc[0]]
            d[bc[0]] = 0
        print(ans)

main()

Submission status: Runtime Error

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


for q in range(Q):
    b, c = map(int, input().split())
    for n in range(N):
        if b == A[n]:
            A[n] = c
    print(sum(A))

Submission status: Runtime Error

Solution code:
N = int(input())
SUM = 0
map = [0] * 100000
for i in [int(i) for i in input().split()]:
  SUM += i
  map[i] += 1

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

Submission status: Runtime Error

Solution code:
n = int(input())
numlist = list(map(int,input().split()))
numcount = [0]*10**5
for num in numlist:
    numcount[num] += 1
n = int(input())
anslist = []
for i in range(n):
    b,c = map(int,input().split())
    numcount[c] += numcount[b]
    numcount[b] = 0
    result = 0
    for i,num in enumerate(numcount):
        result += i*num
    anslist.append(result)

for ans in anslist:
    print(ans)

Submission status: Runtime Error

Solution code:
N = int(input())	
A_list = list(map(int,input().split()))
Q = int(input())
result = []
while True:
    try:
        B, C = map(int,input().split())
        result.append([C if A == B else A for A in A_list])
    except:
        break

for i in result:
    print(sum(result))

Submission status: Runtime Error

Solution code:
from collections import Counter

def main():
    n = int(input())
    a = Counter(map(int, input().split()))
    temp, a = listed(a)
    q = int(input())
    bc = [tuple(map(int, input().split())) for _ in range(q)]
    
    for b, c in bc:
        t = a[b]
        if t == 0:
            print(temp)
            continue
        a[b] = 0
        temp -= b*t
        a[c] += t
        temp += c*t
        print(temp)

def listed(a):
    x = [0] * 10**5
    kei = 0
    for i,v in a.items():
        x[i] = v
        kei += i*v
    return kei, x

if __name__ == '__main__':
    main()

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())
    a = x.replace(y)
    print(sum(a))

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)
import bisect
import numpy as np
import math
from collections import deque
from collections import defaultdict
from math import gcd

N = int(input())
A = list(map(int, input().split()))
cnt = defaultdict(int)
S = np.sum(A)
for a in A:
    cnt[a] += 1

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    num_of_b = cnt[b]
    S -= num_of_b * b
    S += num_of_b * c
    cnt[c] += num_of_b

    print(S)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import numpy as np

N=int(input())
A=list(map(int, input().split()))
Q=int(input())
B,C=map(int, input().split())
rep=[i for i, x in enumerate(A) if x == B]
for i in range(len(rep)):
    A[rep[i]]=C
ans=np.cumsum(A)
print(ans[1])

for _ in range(Q-1):
    B,C=map(int, input().split())
    rep=[i for i, x in enumerate(A) if x == B]
    for i in range(len(rep)):
        A[rep[i]]=C
    ans+=(C-B)*len(rep)
    print(ans[1])

Submission status: Runtime Error

Solution code:

n=int(input())
arr=list(map(int,input().split()))
sumv=0
d={}
for i in arr:
    sumv+=i
    try:
        d[i]+=1
    except:
        d[i]=1

q=int(input())

while q:
    
    b,c=map(int,input().split())
    sumv+=(c-b)*d[b]
    try:
        d[c]+=d[b]
    except:
        d[c]=d[b]
    d[b]=0
    print(sumv)


    q-=1

Submission status: Runtime Error

Solution code:
n=int(input())
l=[int(i) for i in input().split()]
s=sum(l)
d={}
for i in range(n):
  if(l[i] in d):
    d[i]+=1
  else:
    d[i]=1
q=int(input())
for w in range(q):
  b,c=(int(i) for i in input().split())
  if(b in d):
    s-=((d[b]*b)+(d[b]*c))
    d[c]=d[b]
    d[b]=0
  print(s)

Submission status: Runtime Error

Solution code:
import sys
from math import ceil, floor, sqrt, sin, cos, pi
from itertools import accumulate, permutations, combinations
from fractions import gcd # 最大公約数
from collections import deque, Counter
from operator import itemgetter
from heapq import heappop,heappush
sys.setrecursionlimit(10**7)
def lcm(x, y): return ((x * y) // gcd(x, y)) # 最小公倍数
# list(map(int, input().split()))

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

Submission status: Runtime Error

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

newList = A
for i in L:
  if i[0] in newList:
    newList = [int(str(s).replace(str(i[0]), str(i[1]))) for s in newList]
    total = sum(newList)
    print(total)
  else:
    print(total)

Submission status: Runtime Error

Solution code:
import numpy as np
n = int(input())
arr = np.array(list(map(int, input().split())))
q = int(input())

cnt = np.bincount(arr)
cnt = np.concatenate([cnt, np.zeros(10**5 - cnt.shape[0], dtype=int)])

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

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
q=int(input())
BC=[list(map(int,input().split())) for _ in range(q)]

num=sum(A)
N={}
for i in A:
   if i not in N:N[i]=0
   N[i] +=1

for b,c in BC:
    dig=c-b
    num +=dig*N[b]
    print(num)
    if c not in N:N[c]=0
    N[c] +=N[b]
    N[b]=0

Submission status: Runtime Error

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

def main():
    n = int(input())
    a_li = list(map(int, input().split()))
    a_counter = collections.Counter(a_li)
    q = int(input())

    bc_li = []

    for _ in range(q):
        b, c = map(int, input().split())
        bc_li.append([b, c])


    for b, c in bc_li:
        if b in a_counter:
            ans = 0

            b_num = a_counter.pop(b)

            if c in a_counter:
                a_counter[c] += b_num
            else:
                a_counter[c] = b_num

            for x, y in a_counter.items():
                ans += x * y
            
        print(ans)
        


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
nums=[int(x) for x in input().split()]

q=int(input())
nums.sort()
for i in range(q):
  n1,n2=map(int,input().split())
  index=nums.index(n1)
  for i in range(nums.count(n1)):
      nums[index]=n2
      index+=1
  print(sum(nums))

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):
    bi,ci=map(int,input().split())
    t=0
    for j in range(n):
        if a[j]==bi:
            a[j]=ci
            d=j+1
            while a[d]==bi:
                a[d]=ci
                d+=1
            t=1
        elif t==1:
            break
    print(sum(a))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
d = dict()
s = 0
for num in a:
  if num in d:
    d[num] += 1
  else:
    d[num] = 1
  s += num
for i in range(q):
  B, C = list(map(int, input().split()))
  if B in d:
    if C in d:
      d[C] += d[B]
    else:
      d[C] = d[B]
    s += (C-B)*d[B]
    s[B] = 0
  print(s)

Submission status: Runtime Error

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

for i in range(Q):
    b, c = (int(i) for i in input().split())
    BC.append((b,c))

sums = [0] * 10001

init_sum = sum(A)
for a in A:
    sums[a] += 1

for res in BC:
    sums[res[1]] += sums[res[0]]
    init_sum += (res[1] - res[0]) * sums[res[0]]
    sums[res[0]] = 0
    print(init_sum)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import bisect,collections,copy,heapq,itertools,math,numpy,string
import sys
from decimal import Decimal
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())

N = I()
A = LI()
Q = I()
BC = [LI() for _ in range(Q)]
#print(N,A,Q,BC)
for i in range(Q):
    b,c = BC[i]
    #print(b,c,A)
    for j in range(100000):
        if A[j] > b:
            break
        else:
            if A[j] == b:
                A[j] = c
    #print(A)
    A.sort()
    #print(A)
    print(sum(A))

Submission status: Runtime Error

Solution code:
from collections import Counter
n=int(input())
A=list(map(int,input().split()))
q=int(input())
BC=[list(map(int,input().split())) for _ in range(q)]

num=sum(A)
N={i+1:0 for i in range(9)}
for i in A:
    N[i] +=1

for b,c in BC:
    dig=c-b
    num +=dig*N[b]
    print(num)
    if c not in N:N[c]=0
    N[c] +=N[b]
    N[b]=0

Submission status: Runtime Error

Solution code:
N = int(input())
nums = [int(s) for s in input().rstrip().split()]
Q = int(input())

dct = {}
for k in range(N):
  n = nums[k]
  if n in dct:
    dct[n] += 1
  else:
    dct[n] = 1

for _ in range(Q):
  B, C =(int(s) for s in input().rstrip().split())
  if C in dct:
    dct[C] += dct.pop(B)
  else:
    dct[C] = dct.pop(B)

  sum = 0
  for k, v in dct.items():
    sum += k * v
  
  print(sum)

Submission status: Runtime Error

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

def main():
    n = int(input())
    a_li = list(map(int, input().split()))
    a_counter = collections.Counter(a_li)
    q = int(input())

    bc_li = []

    for _ in range(q):
        b, c = map(int, input().split())
        bc_li.append([b, c])


    for b, c in bc_li:
        ans = 0
        b_num = a_counter.pop(b)

        if c in a_counter:
            a_counter[c] += b_num
        else:
            a_counter[c] = b_num

        for x, y in a_counter.items():
            ans += x * y
            
        print(ans)
        


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n=int(input())
c=list(map(int,input().split()))
q=int(input())
su=0
w={}
for i in range(len(c)):
	su=su+c[i]
	if(c[i] not in w):
		w[c[i]]=1
	else:
		w[c[i]]=w[c[i]]+1
for j in range(q):
	r,s=map(int,input().split())
	count=w[r]
	if(s in w):
		w[s]=w[s]+count
	else:
		w[s]=count
	su=su-count*r+count*s
	print(su)

Submission status: Runtime Error

Solution code:
n=int(input())
l=[]
for i in range(n):
  x=int(input())
  l.append(x)
q=int(input())
b=[]
c=[]
for i in range(q):
  y,z=map(int,input().split())
  b.append(y)
  c.append(z)
for j in range(len(b)):
  for i in l:
    if b[j]==i:
      l.replace(i,c[j])
      print(sum(l))

Submission status: Runtime Error

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

lst = [0] * (10**5)
for A in As:
    lst[A] += 1

val = sum(As)

vals = []
for i in range(Q):
    B, C = map(int,input().split())
    val += (C-B) * lst[B]
    lst[C] += lst[B]
    lst[B] = 0
    vals.append(val)
for val in vals:
    print(val)

Submission status: Runtime Error

Solution code:
def sums(l):
	s = 0
	for v in l:
		s += v
	return s

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

for i in range(n):
	if a[i] in ind:
		ind[a[i]].append(i)
	else:
		ind[a[i]] = [i]

q = int(input())
for _ in range(q):
	b, c = map(int, input().split())
	for i in ind[b]:
		a[i] = c
		if c in ind:
			ind[c].append(i)

		else:
			ind[c] = [i]
			
	print(sums(a))

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
q = int(input())
from collections import Counter
x = Counter(a)
r = sum(a)
for i in range(q):
    b, c = map(int, input().split())
    if b in x:
        m = x[b]
        x.pop(b)
        if c in x:
            x[c] += m
        else:
            x[c] = m
    r += m*(c-b)
    print(r)

Submission status: Runtime Error

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


if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

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

ls = sorted(ls)
ans = sum(ls)
newls = [0]*(n+1)
for p in ls:
  newls[p] += 1
ls = newls

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
N=int(input())
A=[int(x) for x in input().split()]
q=int(input())
for i in range(q):
  total=0
  b,c=map(int,input().split())
  A.remove(b)
  A.append(c)
  for j in range(len(A)):
    total+=A[j]
  print(total)

Submission status: Runtime Error

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

for _ in range(q):
  b,c = map(int, input().split())
  if b in a:
    ans = 0
    t = a.pop(b)
    if c in a:
      a[c] += t
    else:
      a[c] = t
    for x,y in a.items():
      ans += x*y
    print(ans)
  else:
    print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter

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

dic = Counter(A)

Q = int(input())

for _ in range(Q):
    b, c = map(int, input().split())
    if b in dic.keys():
        ans = sumA+(dic[b]*(c-b))

        B = dic[b]
        if c in dic.keys():
            dic[c] += dic[b]
            del dic[b]
        else:
            dic[c] = dic[b]
            del dic[b]
        
        sumA += (dic[c]*c-B*b)
        
        # print(dic)
    print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

N = int(input())

arr = np.array(list(map(int, input().split())))
unique, cnts = np.unique(arr, return_counts=True)

counts = {}
sum = arr.sum()

for key, val in zip(unique, cnts):
    counts[key] = val

Q = int(input())

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

Submission status: Runtime Error

Solution code:
n=int(input())
l=list(map(int,input().split()))
q=int(input())
for _ in range(q):
    b,c=map(int,input().split())
    for i in range(q):
        if l[i]==b:
            l[i]=c
    print(sum(l))

Submission status: Runtime Error

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

lst = [0] * (10**5)
for A in As:
    lst[A] += 1

outs = []

for i in range(0,Q):
    B, C = map(int,input().split())
    lst[C] += lst[B]
    lst[B] = 0
    outs.append(sum([i*j for i,j in enumerate(lst)]))

for out in outs:
    print(out)

Submission status: Runtime Error

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

lst = [0] * (10**5)
for A in As:
    lst[A] += 1

for i in range(0,Q):
    B, C = map(int,input().split())
    lst[C] += lst[B]
    lst[B] = 0
    print(sum([i*j for i,j in enumerate(lst)]))

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))
q = int(input())
b = []
c = []
for i in range(0,q):
  bi, ci = list(map(int, input().split()))
  b.append(bi)
  c.append(ci)
for i in range(0, len(b)):
  if b[i] in arr:
      for j in arr:
          ind = arr.index(b[i])
          if j == b[i]:
              arr[ind] = c[i]
  print(sum(arr))

Submission status: Runtime Error

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

Q = int(input())

s = sum(A)

for i in range(Q):
    b, c = map(int, input().split())
    diff = c - A[b - 1]
    A[b - 1] = c
    s += diff
    print(s)

Submission status: Runtime Error

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

for a in A:
    array[a] += 1
souwa = sum(A)

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

for i in range(Q):
    souwa = souwa + (C[i]-B[i])*array[B[i]]
    array[C[i]] += array[B[i]]
    array[B[i]] = 0
    print(souwa)

Submission status: Runtime Error

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

Q = int(input())

s = sum(A)

for i in range(Q):
    b, c = map(int, input().split())
    diff = c - A[b - 1]
    A[b - 1] = c
     diff
    print(s)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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



A = list(A)

hashmap = {}
s = 0
for i in range(len(A)):
	s += A[i]
	hashmap[A[i]] = hashmap.get(A[i], {'i': 0, 'v': A[i]}) 
	hashmap[A[i]]['i'] += 1

for _ in range(Q):
	# replace every element whose value is B with C
	B, C = map(int, input().split())

	if B in hashmap:
		num = hashmap[B]['i']
		del hashmap[B]
		s -= num * B
		s += num * C

	hashmap[C] = hashmap.get(C, {'i': 0, 'v': C})
	hashmap[C]['i'] += num
	print(s)

Submission status: Runtime Error

Solution code:
array = [0 for i in range(11)]
N = int(input())
A = list(map(int, input().split()))
for a in A:
    array[a] += 1

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

Submission status: Runtime Error

Solution code:
n = int(input())
output = []
arr = list(map(int, input().split()))
k = int(input())
for _ in range(k):
  a,b = map(int, input().split())
  arr[arr.index(a)] = b
  output.append(sum(arr))
for i in output:
  print(i)

Submission status: Runtime Error

Solution code:
n = int(input())
arr = list(map(int, input().split()))
k = int(input())
for _ in range(k):
  a,b = map(int, input().split())
  arr[arr.index(a)] = b
  print(sum(arr))

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

N_list=[0]*(10001)
sumn=0

for a in a_list:
    N_list[a]+=1
    sumn+=a


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

    sumn+=henka

    N_list[c]+=N_list[b]
    N_list[b]=0

    print(sumn)

Submission status: Runtime Error

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

N_list=[0]*(N+1)
sumn=0

for a in a_list:
    N_list[a]+=1
    sumn+=a


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

    sumn+=henka

    N_list[c]+=N_list[b]
    N_list[b]=0

    print(sumn)

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
freq = [0 for i in range(10**5)]
for x in A:
    freq[x] += 1
ans = sum(A)
for q in range(int(input())):
    B, C = map(int,input().split())
    ans -= B*freq[B]
    ans += C*freq[B]
    freq[C] += freq[B]
    freq[B] = 0
    print(ans)

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)]
a.sort
count=[0 for _ in range(100000)]
sum=0
for i in range(n):
  count[a[i]]=count[a[i]]+1
  sum=sum+a[i]
for i in range(q):
  sum=sum+(c[i]-b[i])*count[b[i]]
  print(sum)
  count[c[i]]=count[c[i]]+count[b[i]]
  count[b[i]]=0

Submission status: Runtime Error

Solution code:
n=int(input())
arr=list(map(int,input().split()))
s=0
d={}
for i in arr:
    if i in d:
        d[i]+=1
    else:
        d[i]=1
    s+=i
for i in range(int(input())):
    a,b=map(int,input().split())
    if a in d:
        s=s-a*d[a]+b*d[a]
    if b in d:
        d[b]+=d[a]
    else:
        if a in d:
            d[b]=d[a]
    d[a] = 0
    print(s)