Harryxun/sft_time huggingface dataset:
### BUGGY CODE ###

from collections import deque
n, Q = map(int, input().split())
edges = [[] for _ in range(n+1)]
count = [0]*(n+1)
used = []*(n+1)
q = deque()
q.append((1, 0))

for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a].append(b)
    edges[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    count[p] += x

while(q):
    k = q.popleft()
    used.append(k[0])
    count[k[0]] += count[k[1]]
    for i in edges[k[0]]:
        if not i in used:
            q.append((i, k[0]))

print(*count[1:])


### FIXED CODE ###

from collections import deque
n, Q = map(int, input().split())
edges = [[] for _ in range(n+1)]
count = [0]*(n+1)
q = deque()
q.append((1, 0))

for _ in range(n-1):
    a, b = map(int, input().split())
    edges[a].append(b)
    edges[b].append(a)
for _ in range(Q):
    p, x = map(int, input().split())
    count[p] += x

while(q):
    k = q.popleft()
    count[k[0]] += count[k[1]]
    for i in edges[k[0]]:
        if not i == k[1]:
            q.append((i, k[0]))

print(*count[1:])
### BUGGY CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import numpy as np

H, W = map(int, input().split())

grid = np.array([list(input()) for _ in range(H)])

# dp[h][w] = (0,0) から (h,w) までの経路の本数
dp = np.zeros((H, W))
# 初期値
dp[0][0] = 1

# mod用
MOD_INT = 1000000007

for h in range(H):
    for w in range(W):
        # 右が範囲内の空マスなら現在マスの経路数を足す
        if w + 1 < W and grid[h][w + 1] == ".":
            dp[h][w + 1] += dp[h][w]
            # modを取って更新
            dp[h][w + 1] = np.mod(dp[h][w + 1], MOD_INT)
        # 下が範囲内の空マスなら現在マスの経路数を足す
        if h + 1 < H and grid[h + 1][w] == ".":
            dp[h + 1][w] += dp[h][w]
            # modを取って更新
            dp[h + 1][w] = np.mod(dp[h + 1][w], MOD_INT)

print(int(dp[H - 1][W - 1]))


### FIXED CODE ###

#!/usr/bin/env python
# -*- coding: utf-8 -*-

H, W = map(int, input().split())

grid = [list(input()) for _ in range(H)]

# dp[h][w] = (0,0) から (h,w) までの経路の本数
dp = [[0] * W for _ in range(H)]
# 初期値
dp[0][0] = 1

# mod用
MOD_INT = 10 ** 9 + 7

for h in range(H):
    for w in range(W):
        # 右が範囲内の空マスなら現在マスの経路数を足す
        if w + 1 < W and grid[h][w + 1] == ".":
            # modを取って更新
            dp[h][w + 1] += dp[h][w] % MOD_INT
        # 下が範囲内の空マスなら現在マスの経路数を足す
        if h + 1 < H and grid[h + 1][w] == ".":
            # modを取って更新
            dp[h + 1][w] += dp[h][w] % MOD_INT

ans = dp[H - 1][W - 1] % MOD_INT
print(int(ans))
### BUGGY CODE ###

#解説AC
n=int(input())
a=list(map(int,input().split()))
dp=[[[-1]*(n+1) for i in range(n+1)] for j in range(n+1)]
# dp[a][b][c]=(1個の皿がa枚,2個の皿がb枚,3個の皿がc枚ある時, 全て食べ終わるまでの操作回数の期待値)
# dp[a][b][c]の遷移
#               確率          行先
# 1枚が引かれる  a/N　　　    dp[a-1][b][c]
# 2枚が引かれる  b/N　　　    dp[a+1][b-1][c]
# 3枚が引かれる  c/N　　　    dp[a][b+1][c-1]
# 0枚が引かれる  (N-a-b-c)/N　dp[a][b][c]

#dp[a][b][c]=dp[a-1][b][c]*a/n+dp[a+1][b-1][c]*b/n+dp[a][b+1][c-1]*c/n+dp[a][b][c]*(n-a-b-c)/n+1
#dp[a][b][c]=(dp[a-1][b][c]*a+dp[a+1][b-1][c]*b+dp[a][b+1][c-1]*c+n)/(a+b+c)

def memo(i,j,k):
    if dp[i][j][k]+1:
        return dp[i][j][k]
    ans=n
    if i>0:
        ans+=memo(i-1,j,k)*i
    if j>0:
        ans+=memo(i+1,j-1,k)*j
    if k>0:
        ans+=memo(i,j+1,k-1)*k
    dp[i][j][k]=ans/(i+j+k)
    return ans/(i+j+k)
dp[0][0][0]=0
x,y,z=0,0,0
for i in a:
    if i==1:
        x+=1
    elif i==2:
        y+=1
    else:
        z+=1
print(memo(x,y,z))

### FIXED CODE ###

#解説AC
n=int(input())
a=list(map(int,input().split()))
dp=[[[-1]*(301) for i in range(301)] for j in range(301)]
# dp[a][b][c]=(1個の皿がa枚,2個の皿がb枚,3個の皿がc枚ある時, 全て食べ終わるまでの操作回数の期待値)
# dp[a][b][c]の遷移
#               確率          行先
# 1枚が引かれる  a/N　　　    dp[a-1][b][c]
# 2枚が引かれる  b/N　　　    dp[a+1][b-1][c]
# 3枚が引かれる  c/N　　　    dp[a][b+1][c-1]
# 0枚が引かれる  (N-a-b-c)/N　dp[a][b][c]

#dp[a][b][c]=dp[a-1][b][c]*a/n+dp[a+1][b-1][c]*b/n+dp[a][b+1][c-1]*c/n+dp[a][b][c]*(n-a-b-c)/n+1
#dp[a][b][c]=(dp[a-1][b][c]*a+dp[a+1][b-1][c]*b+dp[a][b+1][c-1]*c+n)/(a+b+c)

def memo(i,j,k):
    if dp[i][j][k]+1:
        return dp[i][j][k]
    ans=n
    if i>0:
        ans+=memo(i-1,j,k)*i
    if j>0:
        ans+=memo(i+1,j-1,k)*j
    if k>0:
        ans+=memo(i,j+1,k-1)*k
    dp[i][j][k]=ans/(i+j+k)
    return ans/(i+j+k)
dp[0][0][0]=0
x,y,z=0,0,0
for i in a:
    if i==1:
        x+=1
    elif i==2:
        y+=1
    else:
        z+=1
print(memo(x,y,z))
### BUGGY CODE ###

from fractions import gcd
from functools import reduce

MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))

def lcm(x, y):
    return int(x*y)//int(gcd(x, y))

B = [1 for i in range(N)]
global_lcm = reduce(lambda a, b : lcm(a, b), A)
for i in range(N):
    B[i] = int(global_lcm//A[i])
ans = 0
for i, b in enumerate(B):
    ans += b % MOD
    ans %= MOD

print(ans)

### FIXED CODE ###

from fractions import gcd
from functools import reduce, lru_cache

MOD = 10**9+7
N = int(input())
A = list(map(int, input().split()))

def lcm(x, y):
    return int(x*y)//int(gcd(x, y))

B = [1 for i in range(N)]
global_lcm = reduce(lcm, A) % MOD
ans = 0
for i in range(N):
    ans += (global_lcm*pow(A[i], MOD-2, MOD)) % MOD
    ans %= MOD

print(ans)
### BUGGY CODE ###

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]


arr = []
for x in ab:
    if n in x:
        if x.index(n) == 0:
            arr.append(x[1])
        else:
            arr.append(x[0])

for a, b in ab:
    if a in arr:
        if b == 1:
            print('POSSIBLE')
            exit()
    if b in arr:
        if a == 1:
            print('POSSIBLE')
            exit()

print('IMPOSSIBLE')


### FIXED CODE ###

n, m = map(int, input().split())
ab = [list(map(int, input().split())) for _ in range(m)]


s = set()
for x in ab:
    if n in x:
        if x.index(n) == 0:
            s.add(x[1])
        else:
            s.add(x[0])

for a, b in ab:
    if a in s:
        if b == 1:
            print('POSSIBLE')
            exit()
    if b in s:
        if a == 1:
            print('POSSIBLE')
            exit()

print('IMPOSSIBLE')
### BUGGY CODE ###

n, q = map(int, input().split())
s=input()
l=[0]*q
r=[0]*q
t=[0]*q
for i in range(q):
  l[i],r[i] = map(int, input().split())
  t[i]=s[l[i]-1:r[i]].count("AC")
  print(t[i])

### FIXED CODE ###

n, q = map(int, input().split())
s=input()
d=[0]*n
c=0
for i in range(n-1):
  if s[i:i+2]=="AC":
    c+=1
  d[i+1]=c
for i in range(q):
  l,r= map(int, input().split())
  print(d[r-1]-d[l-1])
### BUGGY CODE ###

# -*- coding- utf-8 -*-
# C - Sum of gcd of Triplets (Easy)
# モジュールのインポート
import math
import itertools
from functools import reduce

# 標準入力の取得
K = int(input())

# 求解処理
def gcd(*numbers):
    return reduce(math.gcd, numbers)

result = 0
for a,b,c in itertools.product(range(1, K+1), repeat=3):
    result += gcd(a, b, c)

# 結果出力
print(result)

### FIXED CODE ###

# -*- coding- utf-8 -*-
# C - Sum of gcd of Triplets (Easy)
# モジュールのインポート
import math
import itertools
from functools import reduce

# 標準入力の取得
K = int(input())

# 求解処理
result = 0
for a,b in itertools.product(range(1, K+1), repeat=2):
    d = math.gcd(a, b)
    for c in range(1, K+1):
        result += math.gcd(d, c)

# 結果出力
print(result)
### BUGGY CODE ###

class DisjointSet(object):

    def __init__(self):
        self.leader = {} # maps a member to the group's leader
        self.group = {} # maps a group leader to the group (which is a set)

    def add(self, a, b):
        leadera = self.leader.get(a)
        leaderb = self.leader.get(b)
        if leadera is not None:
            if leaderb is not None:
                if leadera == leaderb: return # nothing to do
                groupa = self.group[leadera]
                groupb = self.group[leaderb]
                if len(groupa) < len(groupb):
                    a, leadera, groupa, b, leaderb, groupb = b, leaderb, groupb, a, leadera, groupa
                groupa |= groupb
                del self.group[leaderb]
                for k in groupb:
                    self.leader[k] = leadera
            else:
                self.group[leadera].add(b)
                self.leader[b] = leadera
        else:
            if leaderb is not None:
                self.group[leaderb].add(a)
                self.leader[a] = leaderb
            else:
                self.leader[a] = self.leader[b] = a
                self.group[a] = set([a, b])

n, m = [int(x) for x in input().split()]
b = []
for i in range(m):
  b.append(tuple(int(x) for x in input().split()))
r = []
t = n*(n-1)//2
ds = DisjointSet()
for i in range(m):
  q, p = b[-i-1]
  r.insert(0, t)
  if p not in ds.leader or q not in ds.leader or ds.leader[p] != ds.leader[q]:
    aa = len(ds.group[ds.leader[p]]) if p in ds.leader else 1
    bb = len(ds.group[ds.leader[q]]) if q in ds.leader else 1
    t -= aa*bb
  ds.add(p, q)
for x in r:
  print(x)

### FIXED CODE ###

class Unionfind():
    def __init__(self, progsize):
        self.table = [-1 for _ in range(progsize)]
        self.size = [1 for _ in range(progsize)]

    def find(self, x):
        while(self.table[x] >= 0):
            x = self.table[x]
        return x

    def union(self, x, y):
        xl = self.find(x)
        yl = self.find(y)
        if xl != yl:
            if self.table[xl] <= self.table[yl]:
                self.table[yl] = xl
                self.table[xl] -= 1
                self.size[xl] += self.size[yl]
            else:
                self.table[xl] = yl
                self.table[yl] -= 1
                self.size[yl] += self.size[xl]

n, m = map(int, input().split())
bridge = [tuple(map(int, input().split())) for _ in range(m)]
bridge.reverse()
uf = Unionfind(n)
cur = n * (n-1) // 2
ans = [cur]
for a, b in bridge:
    ap = uf.find(a-1)
    bp = uf.find(b-1)
    if ap != bp:
        cur -= uf.size[ap] * uf.size[bp]
    uf.union(a-1, b-1)
    ans.append(cur)
for a in reversed(ans[0:m]):
    print(a)
### BUGGY CODE ###

n = int(input())
s = list(input())
i=0
while i+1 < len(s):
  if s[i]==s[i+1]:
    s=s[:i]+s[i+1:]
  else:
    i+=1
print(len(s))

### FIXED CODE ###

n = int(input())
s = list(input())
cnt=1
for i in range(n-1):
  if s[i] != s[i+1]:
    cnt+=1
print(cnt)
### BUGGY CODE ###

import sys
input = sys.stdin.readline

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
can_make = [[None for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        x = A[i][j] - B[i][j]
        if i == j == 0:
            can_make[i][j] = set([abs(x)])
            continue
        st = set()
        if i > 0:
            for y in can_make[i-1][j]:
                st.add(abs(y + x))
                st.add(abs(y - x))
        if j > 0:
            for y in can_make[i][j-1]:
                st.add(abs(y + x))
                st.add(abs(y - x))
        can_make[i][j] = st
print(min(can_make[-1][-1], key=lambda x: abs(x)))



### FIXED CODE ###

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

M = (H+W)*80
dp = [[0] * W for _ in range(H)]
dp[0][0] |= 1 << (M-C[0][0])
for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        bit = 0
        if i > 0:
            bit |= dp[i-1][j]
        if j > 0:
            bit |= dp[i][j-1]
        dp[i][j] = (bit >> C[i][j]) | (bit << C[i][j])
ans = M
for i in range(M+1):
    if (dp[-1][-1] >> i) & 1:
        ans = min(ans, abs(M-i))
print(ans)
### BUGGY CODE ###

#ABC077-C
N=int(input())
A=sorted(list(map(int,input().split())))
B=sorted(list(map(int,input().split())))
C=sorted(list(map(int,input().split())))
ans=0
for i in range(N):
    b=B[i]
    if A[-1]<b:
        a_comb=N
    elif A[0]>=b:
        a_comb=0
    elif A[0]<b and A[1]>=b:
        a_comb=1
    else:
        p=N//2
        while True:
            if A[p]<b and A[p+1]>=b:
                a_comb=p+1
                break
            elif A[p]<b and A[p+1]<b:
                p=(N+p)//2
                continue
            elif A[p]>=b:
                p=(p+1)//2
                continue
    if C[0]>b:
        c_comb=N
    elif C[-1]<=b:
        c_comb=0
    elif C[-1]>b and C[-2]<=b:
        c_comb=1
    else:
        p=N//2
        while True:
            if C[p]>b and C[p-1]<=b:
                c_comb=N-p
                break
            elif C[p]>b and C[p-1]>b:
                p=(p+1)//2
                continue
            elif C[p]<=b:
                p=(N+p)//2
                continue
    ans+=a_comb*c_comb
print(ans)

### FIXED CODE ###

#ABC077-C
N=int(input())
A=sorted(list(map(int,input().split())))
B=sorted(list(map(int,input().split())))
C=sorted(list(map(int,input().split())))

ans=0
for i in range(N):
    b=B[i]
    left=0
    right=N-1
    if A[-1]<b:
        a_comb=N
    elif A[0]>=b:
        a_comb=0
    elif A[0]<b and A[1]>=b:
        a_comb=1
    else:
        a_comb=0
        while left<=right:
            mid=(left+right)//2
            if A[mid]<b and A[mid+1]>=b:
                a_comb=mid+1
                break
            elif A[mid]<b and A[mid+1]<b:
                left=mid+1
            elif A[mid]>=b:
                right=mid-1

    left=0
    right=N-1
    if C[0]>b:
        c_comb=N
    elif C[-1]<=b:
        c_comb=0
    elif C[-1]>b and C[-2]<=b:
        c_comb=1
    else:
        c_comb=0
        while left<=right:
            mid=(left+right)//2
            if C[mid]>b and C[mid-1]<=b:
                c_comb=N-mid
                break
            elif C[mid]>b and C[mid-1]>b:
                right=mid-1
            elif C[mid]<=b:
                left=mid+1
    ans+=a_comb*c_comb
print(ans)
### BUGGY CODE ###

H,W=map(int,input().split())
import heapq
w=[[0 for i in range(W)]for j in range(H)]
for i in range(H):
  S=input()
  for j in range(W):
    w[i][j]=S[j]
    
L=[[0 for i in range(W)] for j in range(H)]
R=[[0 for i in range(W)] for j in range(H)]
U=[[0 for i in range(W)] for j in range(H)]
D=[[0 for i in range(W)] for j in range(H)]

for i in range(H):
  for j in range(W):
    if w[i][j]=='#':
      L[i][j]=0
    elif j==0:
      L[i][j]=1
    else:
      L[i][j]=L[i][j-1]+1
for i in range(H):
  for j in range(W-1,-1,-1):
    if w[i][j]=='#':
      R[i][j]=0
    elif j==W-1:
      R[i][j]=1
    else:
      R[i][j]=R[i][j+1]+1
for i in range(H):
  for j in range(W):
    if w[i][j]=='#':
      D[i][j]=0
    elif i==0:
      D[i][j]=1
    else:
      D[i][j]=D[i-1][j]+1  
for i in range(H-1,-1,-1):
  for j in range(W):
    if w[i][j]=='#':
      U[i][j]=0
    elif i==H-1:
      U[i][j]=1
    else:
      U[i][j]=U[i+1][j]+1
m=0

for i in range(H):
  for j in range(W):
    x=L[i][j]+R[i][j]+D[i][j]+U[i][j]-3
    m=max(x,m)
print(m)
    

### FIXED CODE ###

H,W=map(int,input().split())
import heapq
w=[[0 for i in range(W)]for j in range(H)]
for i in range(H):
  S=input()
  for j in range(W):
    w[i][j]=S[j]
    
L=[[0 for i in range(W)] for j in range(H)]
R=[[0 for i in range(W)] for j in range(H)]
U=[[0 for i in range(W)] for j in range(H)]
D=[[0 for i in range(W)] for j in range(H)]

for i in range(H):
  for j in range(W):
    if w[i][j]=='#':
      L[i][j]=0
    elif j==0:
      L[i][j]=1
    else:
      L[i][j]=L[i][j-1]+1
for i in range(H):
  for j in range(W-1,-1,-1):
    if w[i][j]=='#':
      R[i][j]=0
    elif j==W-1:
      R[i][j]=1
    else:
      R[i][j]=R[i][j+1]+1
for i in range(H):
  for j in range(W):
    if w[i][j]=='#':
      D[i][j]=0
    elif i==0:
      D[i][j]=1
    else:
      D[i][j]=D[i-1][j]+1  
for i in range(H-1,-1,-1):
  for j in range(W):
    if w[i][j]=='#':
      U[i][j]=0
    elif i==H-1:
      U[i][j]=1
    else:
      U[i][j]=U[i+1][j]+1
m=0

for i in range(H):
  for j in range(W):
    x=L[i][j]+R[i][j]+D[i][j]+U[i][j]-3
    m=max(x,m)
print(m)
### BUGGY CODE ###

#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

m = [[0] * 2005 for i in range(4005)]

def f():
    K, N = map(int, raw_input().split())
    MOD = 998244353

    for i in range(4001):
        m[i][0] = 1
    for i in range(2001):
        m[i][i] = 1
    for i in range(1, 4001):
        for j in range(1, 2001):
            m[i][j] = (m[i - 1][j] + m[i - 1][j - 1]) % MOD

    m2 = [1] * 4005
    for i in range(1, 4001):
        m2[i] = (m2[i - 1] * 2) % MOD

    ls = []
    for i in range(2, K + 2):
        ans = 0
        if i % 2 == 0:
            rem = i / 2 - 1
            for j in range(rem + 1):
                ans += m[N + K - 2 - rem * 2][K - 2 - rem * 2 + j] \
                * m[rem][j] * m2[j]
                ans += m[N + K - 3 - rem * 2][K - 2 - rem * 2 + j] \
                * m[rem][j] * m2[j]
                ans %= MOD
        else:
            rem = i / 2
            for j in range(rem + 1):
                ans += m[N + K - 1 - rem * 2][K - 1 - rem * 2 + j] \
                * m[rem][j] * m2[j]
                ans %= MOD
        ls.append(ans % MOD)

    ls += ls[:-1][::-1]

    for num in ls:
        print num

f()


### FIXED CODE ###

#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(10000000)

m = [[0] * 2005 for i in range(4005)]

def f():
    K, N = map(int, raw_input().split())
    MOD = 998244353

    for i in range(4001):
        m[i][0] = 1
    for i in range(2001):
        m[i][i] = 1
    for i in range(1, 4001):
        for j in range(1, 2001):
            m[i][j] = (m[i - 1][j] + m[i - 1][j - 1]) % MOD

    m2 = [1] * 4005
    for i in range(1, 4001):
        m2[i] = (m2[i - 1] * 2) % MOD

    ls = []
    for i in range(2, K + 2):
        ans = 0
        if i % 2 == 0:
            rem = i / 2 - 1
            for j in range(rem + 1):
                ans += m[N + K - 2 - rem * 2][K - 2 - rem * 2 + j] \
                * m[rem][j] * m2[j]
                ans += m[N + K - 3 - rem * 2][K - 2 - rem * 2 + j] \
                * m[rem][j] * m2[j]
                ans %= MOD
        else:
            rem = i / 2
            for j in range(rem + 1):
                ans += m[N + K - 1 - rem * 2][K - 1 - rem * 2 + j] \
                * m[rem][j] * m2[j]
                ans %= MOD
        ls.append(ans % MOD)

    ls += ls[:-1][::-1]

    for num in ls:
        print num

f()
### BUGGY CODE ###

n=int(input())
import sys
array = []

for i in range(1,n+1):
    if n % i == 0:
      array.append(i)
if len(array)%2==1:
  a=array[int(len(array)/2)]
  print(2*a-2)
else:
  a=array[int(len(array)/2)-1]
  b=array[int(len(array)/2)]
  ans=a-1+b-1
  print(ans)

### FIXED CODE ###

n=int(input())
array = []
for i in range(1, int(n**0.5)+1):
  if n % i == 0:
    array.append(i)
    if i != n // i:
      array.append(n//i)
array.sort()
      
if len(array)%2==1:
  a=array[int(len(array)/2)]
  print(2*a-2)
else:
  a=array[int(len(array)/2)-1]
  b=array[int(len(array)/2)]
  print(a-1+b-1)
### BUGGY CODE ###

N, M = map(int, input().split())
As = list(map(int, input().split()))

for _ in range(M):
    As.sort()
    b, c = map(int, input().split())
    for i in range(b):
        if As[i] < c:
            As[i] = c

print(sum(As))


### FIXED CODE ###

N, M = map(int, input().split())
As = list(map(int, input().split()))
As.sort(reverse=True)

BCs = []
for _ in range(M):
    b, c = map(int, input().split())
    BCs.append({'b': b, 'c': c})

BCs = sorted(BCs, key=lambda bc: bc['c'], reverse=True)
#print(BCs)

new_cards = []
for i in range(M):
    new_cards += [BCs[i]['c'] for _ in range(BCs[i]['b'])]
    if len(new_cards) >= N:
        break

if len(new_cards) < N:
    new_cards += [0 for _ in range(N-len(new_cards))]
#print(new_cards)

A_index = 0
new_index = 0
new_As = []
for i in range(N):
    if As[A_index] >= new_cards[new_index]:
        new_As.append(As[A_index])
        A_index += 1
    else:
        new_As.append(new_cards[new_index])
        new_index += 1

print(sum(new_As))
### BUGGY CODE ###

import sys
def main():
  input = sys.stdin.readline
  N=int(input())
  L=list(map(int,input().split()))
  Q=int(input())
  for i in range(Q):
    A,B=map(int,input().split())
    R=[B for K in L if K==A]
    L=[i for i in L if i!=A]
    L.extend(R)
    print(sum(L))
if __name__ == '__main__':
    main()

### FIXED CODE ###

import sys
input = sys.stdin.readline 
N=int(input())
L=list(map(int,input().split()))
Q=int(input())
R=[0]*(10**5)
for i in L:
  R[i-1]+=1
ans=sum(L)
for i in range(Q):
  a,b=map(int,input().split())
  R[b-1]+=R[a-1]
  ans+=(b-a)*R[a-1]
  R[a-1]=0
  print(ans)
### BUGGY CODE ###

x, k, d = (int(x) for x in input().split())

posabs_list = []

for i in range(k+1):
        pos = x + d * (k-i) - d * i
        posabs = abs(pos)
        posabs_list.append(posabs)
print(min(posabs_list))

### FIXED CODE ###

import math

x, k, d = (int(x) for x in input().split())

x = abs(x)
k1 = math.floor(x/d)

if k < k1 :
        k1 = k
else :
        k1 = k1

k2 = k1 - k

y = x - k1 * d

if k2%2 == 0 :
        print(abs(y))
else :
        print(abs(y - d))
### BUGGY CODE ###

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

X = 0
for i in range(K):
  X = A[X]-1

print(X + 1)

### FIXED CODE ###

N, K = map(int,input().split())
A = list(map(int, input().split()))
V = [-1]*N

X = 0
V[0] = 0
T = 0

while T < K:
  T += 1
  X = A[X]-1
  if V[X] != -1:
    t = T - V[X]
    k = (K - T) % t
    for i in range(k):
      X = A[X]-1
    break
  V[X] = T

print(X+1)
### BUGGY CODE ###

from itertools import combinations
n = int(input())
arr = [input()[0] for s in range(n)]
arr = [s for s in arr if s in "MARCH"]
combi = [len(set(i)) for i in combinations(arr, 3)]
print(sum([1 for i in combi if i==3]))

### FIXED CODE ###

from itertools import combinations
from collections import Counter

n = int(input())
S = Counter()
for i in range(n):
  S[input()[0]] +=  1 # Counter == dictionary
  
print(sum([S[a]*S[b]*S[c] for a, b, c in combinations("MARCH", 3)]))
### BUGGY CODE ###

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

print(sum(l[i] == min(l[:i+1]) for i in range(n)))

### FIXED CODE ###

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

m = 200001
ans = 0

for i in range(n):
  if l[i] < m:
    m = l[i]
    ans += 1

print(ans)
### BUGGY CODE ###

#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1
dydx4 = ((-1,0),(0,-1),(0,1),(1,0))

h,w = map(int,input().split())
A = [list(input()) for i in range(h)]
dis = [[inf] * w for i in range(h)]
import heapq
def bfs(si,sj):
  que = []
  que.append((0,si,sj))
  dis[si][sj] = 0
  while que:
    cost,i,j = heapq.heappop(que)
    if dis[i][j] < cost: continue
    for di,dj in dydx4:
      ni = i + di
      nj = j + dj
      if not (0 <= ni < h and 0 <= nj < w):continue
      if A[ni][nj] == "#" and dis[ni][nj] == inf:
        dis[ni][nj] = 0
        heapq.heappush(que,(0,ni,nj))
      if cost + 1 < dis[ni][nj]: 
        dis[ni][nj] = cost+1
        que.append((cost+1,ni,nj))
for i in range(h):
  for j in range(w):
    if A[i][j] == "#":
      bfs(i,j)
      break
  else:continue
  break
print(max(map(max,dis)))




### FIXED CODE ###

#!/usr/bin/env python3
import sys
import heapq
from collections import deque
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)
inf = float('inf')
mod = 10**9+7

h,w=map(int,input().split())
A=[list(input()) for i in range(h)]
# A=(tuple(input()) for i in range(h))
# print(A)
dis=[[-1]*w for i in range(h)]
hq=deque()
for i in range(h):
    for j in range(w):
        if A[i][j]=="#":
            hq.append((0,i,j))
            dis[i][j]=0
dii=[(1,0),(-1,0),(0,1),(0,-1)]
# heapq.heapify(hq)
ans=0
while hq:
    count,i,j=hq.popleft()
    for di,dj in dii:
        if not (0<=i+di<h and 0<=j+dj<w):
            continue
        if dis[i+di][j+dj]==-1:
            hq.append((count+1, i+di, j+dj))
            dis[i+di][j+dj]=count+1
            ans=max(ans,count+1)
#             # print(count+1)
# ans=0
# for Ai in dis:
#     ans=max(ans,max(Ai))
print(ans)
# for d in dis:
#     print(d)