Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
N,Q = input().split()
Q = int(Q)
S = input()

list1 = []
count = 0
for i in range(Q):
    l,r = map(int,input().split())
    l = l - 1
    w_string = S[l:r]
    for j, k in enumerate(w_string):
        if 'AC' in w_string[j:j+2]:
            count = count + 1
    list1.append(count)
    count = 0

for i in list1:
    print(i)


Fixed submission:
N, Q = map(int, input().split())
S = input()
t=[0]*(N+1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Runtime Error):
N,Q = input().split()
Q = int(Q)
S = input()

list1 = []

for i in range(Q):
    l,r = map(int,input().split())
    l = l - 1
    w_string = S[l:r]
    for j in w_string:
        if 'AC' in s[j:j+1]:
            count = count + 1
    list1.append(count)
    count = 0

for i in list1:
    print(i)


Fixed submission:
N, Q = map(int, input().split())
S = input()
t=[0]*(N+1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = input().split()
Q = int(Q)
S = input()

list1 = []

for i in range(Q):
    l,r = map(int,input().split())
    l = l - 1
    list1.append(S[l:r].count('AC'))

for i in list1:
    print(i)

Fixed submission:
N, Q = map(int, input().split())
S = input()
t=[0]*(N+1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])

Buggy python submission (verdict: Time Limit Exceeded):
N,Q = input().split()
Q = int(Q)
S = input()

list1 = []

for i in range(Q):
    l,r = input().split()
    list1.append([int(l)-1,int(r)])

for i in range(Q):
    print(S[list1[i][0]:list1[i][1]].count('AC'))


Fixed submission:
N, Q = map(int, input().split())
S = input()
t=[0]*(N+1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
    l, r = map(int, input().split())
    print(t[r-1] - t[l-1])
Buggy python submission (verdict: Time Limit Exceeded):
N = int(input())
S = list(input().strip())

ans = N
for i in range(N):
    cnt = len([1 for d in S[: max(i-1, 0)] if not d == 'E']) + len([1 for d in S[min(i+1, N-1):] if not d == 'W'])
    if ans > cnt: ans = cnt
print(ans)

Fixed submission:
N = int(input())
S = list(input().strip())

ans = len([1 for d in S[1:] if d == 'E'])
cnt = ans
for i in range(1, N):
    if S[i] == 'E': cnt -= 1
    if S[i-1] == 'W': cnt += 1
    if ans > cnt: ans = cnt
print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
def n_gram(seq, n):
    return {seq[i: i+n] for i in range(len(seq) - n + 1)}

def main(s, k):
    tmp = set()
    for i in range(1, len(s) + 1):
            tmp = tmp | n_gram(s, i)
    substr = sorted(list(tmp))
    print(substr[k-1])



if __name__ == '__main__':
    main(input(), int(input()))

Fixed submission:
def n_gram(seq, n):
    return {seq[i: i+n] for i in range(len(seq) - n + 1)}

def main(s, k):
    tmp = set()
    for i in range(1, 6):
        tmp = tmp | n_gram(s, i)
    substr = sorted(list(tmp))
    print(substr[k-1])

if __name__ == '__main__':
    main(input(), int(input()))
Buggy python submission (verdict: Runtime Error):
from collections import Counter
def main(s, t):
    s_c = sorted(Counter(s).items(), key=lambda x: -x[1])
    t_c = Counter(t)
    for i, (k, v) in enumerate(s_c):
        benefit = v - t_c[k]
        if s_c[i + 1][1] <= benefit:
            print(benefit)
            break
    else:
        print(benefit)

if __name__ == '__main__':
    N = int(input().strip())
    s = [input().strip() for _ in range(N)]
    M = int(input().strip())
    t = [input().strip() for _ in range(M)]
    main(s, t)

Fixed submission:
from collections import Counter
def main(s, t):
    s_c = sorted(Counter(s).items(), key=lambda x: -x[1])
    t_c = Counter(t)
    tmp = 0
    for k, v in s_c:
        minus = t_c[k] if k in t_c.keys() else 0
        tmp = max(tmp, v - minus)
    print(tmp)

if __name__ == '__main__':
    N = int(input().strip())
    s = [input().strip() for _ in range(N)]
    M = int(input().strip())
    t = [input().strip() for _ in range(M)]
    main(s, t)
Buggy python submission (verdict: Runtime Error):
def main():
    A, B, C, D = list(map(int, list(input().strip())))

    for op1 in [-1, 1]:
        for op2 in [-1, 1]:
            for op3 in [-1, 1]:
                if A + (op1 * B) + (op2 * C) + (op3 * D) == 7:
                    print(f'{A}{"+" if op1 == 1 else "-"}{B}{"+" if op2 == 1 else "-"}{C}{"+" if op3 == 1 else "-"}{D}=7')
                    return 0

if __name__ == '__main__':
    main()

Fixed submission:
A, B, C, D = list(map(int, list(input().strip())))

for op1 in [-1, 1]:
    for op2 in [-1, 1]:
        for op3 in [-1, 1]:
            if A + (op1 * B) + (op2 * C) + (op3 * D) == 7:
                print('{}{}{}{}{}{}{}=7'.format(A, "+" if op1 == 1 else "-", B, "+" if op2 == 1 else "-", C, "+" if op3 == 1 else "-", D))
                exit()

Buggy python submission (verdict: Runtime Error):
A, B, C, D = list(map(int, list(input().strip())))

for op1 in [-1, 1]:
    for op2 in [-1, 1]:
        for op3 in [-1, 1]:
            if A + (op1 * B) + (op2 * C) + (op3 * D) == 7:
                print(f'{A}{"+" if op1 == 1 else "-"}{B}{"+" if op2 == 1 else "-"}{C}{"+" if op3 == 1 else "-"}{D}=7')
                exit()

Fixed submission:
A, B, C, D = list(map(int, list(input().strip())))

for op1 in [-1, 1]:
    for op2 in [-1, 1]:
        for op3 in [-1, 1]:
            if A + (op1 * B) + (op2 * C) + (op3 * D) == 7:
                print('{}{}{}{}{}{}{}=7'.format(A, "+" if op1 == 1 else "-", B, "+" if op2 == 1 else "-", C, "+" if op3 == 1 else "-", D))
                exit()
Buggy python submission (verdict: Time Limit Exceeded):
def process(a):
    tmp = a[0]
    pushed = []
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.append(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print(process(a))

Fixed submission:
def process(a):
    tmp = a[0]
    pushed = set()
    cnt = 1
    while not tmp in pushed:
        if tmp == 2:
            return cnt
        pushed.add(tmp)
        tmp = a[tmp - 1]
        cnt += 1
    return -1

if __name__ == '__main__':
    a = [int(input().strip()) for _ in range(int(input().strip()))]
    print(process(a))
Buggy python submission (verdict: Runtime Error):
import sys

sys.setrecursionlimit(10000)

def kaijo(N):
    if N == 1:
        return 1
    else:
        return N * kaijo(N-1)

if __name__ == '__main__':
    n = int(input().strip())
    print(kaijo(n) % (10**9 + 7))

Fixed submission:
def process(N):
    ans = 1
    while N > 1:
        ans = ans * N % (10**9 + 7)
        N -= 1
    return ans

if __name__ == '__main__':
    n = int(input().strip())
    print(process(n))

Buggy python submission (verdict: Runtime Error):
def kaijo(N):
    if N == 1:
        return 1
    else:
        return N * kaijo(N-1)

if __name__ == '__main__':
    n = int(input().strip())
    print(kaijo(n) % (10**9 + 7))

Fixed submission:
def process(N):
    ans = 1
    while N > 1:
        ans = ans * N % (10**9 + 7)
        N -= 1
    return ans

if __name__ == '__main__':
    n = int(input().strip())
    print(process(n))
Buggy python submission (verdict: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define PI 3.141592653589793
#define MOD 1000000007

typedef long long ll;

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);

    int a,b,k;
    cin >> a >> b >> k;

    for(int i = a; i < min(a+k, b+1); ++i) cout << i << endl;
    for(int i = max(b-k+1, a+k); i <= b; ++i) cout << i << endl;

    return 0;
}

Fixed submission:
a, b, k = map(int, input().split())

for i in range(a, min(a+k, b+1)):
    print(i)

for i in range(max(b-k+1, a+k), b+1):
    print(i)
Buggy python submission (verdict: Runtime Error):
N, K = list(map(int, input().split()))
S = input()

S_L = list(S)
S_L[K-1] = S_L[K-1].lower
print("".join(S_L))


Fixed submission:
N, K = list(map(int, input().split()))
S = input()

S_L = list(S)
S_L[K-1] = S_L[K-1].lower()
print("".join(S_L))
Buggy python submission (verdict: Runtime Error):
s = input()
s1 = s[0:1]
s2 = s[2:3]

i1 = int(s1)
i2 = int(s2)

if i1 = 0 or i2 = 0: res =NA
if i1 <=12:
	if i2 <= 12:
		res = "AMBIGUOUS"
    else:
      	res = "MMYY"
elif 12 < i1:
  	if i2 <= 12:
      	res = "YYMM"
    else:
      	res = NA
        
print(res)

Fixed submission:
s = input()
s1 = s[0:2]
s2 = s[2:4]

i1 = int(s1)
i2 = int(s2)
 
if 1 <= i1 <=12 and 1 <= i2 <= 12:
    res = "AMBIGUOUS"
elif 1 <= i1 <= 12:
    res = "MMYY"
elif 1 <= i2 <= 12:
    res = "YYMM"
else:
    res = "NA"

print(res)
Buggy python submission (verdict: Runtime Error):
H, W = list(map(int, input().split()))
grid = [[c for c in input()] for r in range(H)]
paths = [[-1 for r in range(W)] for c in range(H)]
paths[0][0] = 1

def calc(r, c):
    if not ((0 <= r <= H-1) and (0 <= c <= W-1)): return 0
    if paths[r][c] == -1:
        if grid[r][c] == "#":
            paths[r][c] = 0
        else:
            paths[r][c] = (calc(r-1, c) + calc(r, c-1)) % (10**9+7)
    return paths[r][c]

print(calc(H-1, W-1))


Fixed submission:
h, w = map(int, input().split())
a = [input() for _ in range(h)]
dp = [[0] * w for _ in range(h)]
 
for i in range(h):
    if a[i][0] == ".":
        dp[i][0] = 1
    else:
        break
 
for i in range(w):
    if a[0][i] == ".":
        dp[0][i] = 1
    else:
        break
 
for i in range(1, h):
    for j in range(1, w):
        if a[i][j] == "#":
            dp[i][j] = 0
        else:
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % (10 ** 9 + 7)
 
print(dp[h - 1][w - 1])

Buggy python submission (verdict: Runtime Error):
H, W = list(map(int, input().split()))
grid = [[c for c in input()] for r in range(H)]
paths = [[-1 for r in range(W)] for c in range(H)]
paths[0][0] = 1

def calc(r, c):
    if not ((0 <= r <= H-1) and (0 <= c <= W-1)): return 0
    if paths[r][c] == -1:
        if grid[r][c] == "#":
            v = 0
        else:
            v = (calc(r-1, c) + calc(r, c-1)) % (10**9+7)
    paths[r][c] = v
    return paths[r][c]

print(calc(H-1, W-1))


Fixed submission:
h, w = map(int, input().split())
a = [input() for _ in range(h)]
dp = [[0] * w for _ in range(h)]
 
for i in range(h):
    if a[i][0] == ".":
        dp[i][0] = 1
    else:
        break
 
for i in range(w):
    if a[0][i] == ".":
        dp[0][i] = 1
    else:
        break
 
for i in range(1, h):
    for j in range(1, w):
        if a[i][j] == "#":
            dp[i][j] = 0
        else:
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % (10 ** 9 + 7)
 
print(dp[h - 1][w - 1])

Buggy python submission (verdict: Runtime Error):
H, W = list(map(int, input().split()))
grid = [[c for c in input()] for r in range(H)]
paths = [[0 for r in range(W)] for c in range(H)]

def calc(r, c):
    if not ((0 <= r <= H-1) and (0 <= c <= W-1)): return 0
    if r == 0 and c == 0: return 1
    if grid[r][c] == "#": return 0
    return (calc(r-1, c) + calc(r, c-1))

v = calc(H-1, W-1) % (10**9 + 7)
print(v)


Fixed submission:
h, w = map(int, input().split())
a = [input() for _ in range(h)]
dp = [[0] * w for _ in range(h)]
 
for i in range(h):
    if a[i][0] == ".":
        dp[i][0] = 1
    else:
        break
 
for i in range(w):
    if a[0][i] == ".":
        dp[0][i] = 1
    else:
        break
 
for i in range(1, h):
    for j in range(1, w):
        if a[i][j] == "#":
            dp[i][j] = 0
        else:
            dp[i][j] = (dp[i - 1][j] + dp[i][j - 1]) % (10 ** 9 + 7)
 
print(dp[h - 1][w - 1])
Buggy python submission (verdict: Runtime Error):
N,K = map(int, input().split())
S = Input()
dir = {'A':'a', 'B':'b', 'C':'c'}

print(S[0:K-1] + dir[S[K-1]]+ S[K::])


Fixed submission:
N,K = map(int, input().split())
S = input()
dir = {'A':'a', 'B':'b', 'C':'c'}

print(S[0:K-1] + dir[S[K-1]]+ S[K::])
Buggy python submission (verdict: Runtime Error):
S = input()

fr_half = int(S[0:2]) 
se_half = int(S[2::])

if se_half > 0 and se_half <= 12:
	if fr_half  > 0 and fr_half <= 12:
		print('AMBIGUOUS')
  	else:
		print('YYMM')
else:
	if fr_half  > 0 and fr_half <= 12:
		print('MMYY')
	else:
		print('NA')

Fixed submission:
S = input()

fr_half = int(S[0:2]) 
se_half = int(S[2::])

if se_half > 0 and se_half <= 12:
  if fr_half  > 0 and fr_half <= 12:
    print('AMBIGUOUS')
  else:
    print('YYMM')
else:
  if fr_half  > 0 and fr_half <= 12:
    print('MMYY')
  else:
   	print('NA')
Buggy python submission (verdict: Runtime Error):
import fractions
def mygcd(N,A,S):
    if S == 'R':
        A.reverse()
    ans = [0, A[0]]
    for i in range(1, N):
        ans.append(fractions.gcd(ans[i],A[i]))
    if S == 'R':
        ans.reverse()
    return ans

def GCDonBlack(N,A):
    L = mygcd(N,A,'L')
    R = mygcd(N,A,'R')
    M = [fractions.gcd(L[i], R[i+1])  for i  in range(0,N)]
    return max(M)

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

Fixed submission:
A,B,T = map(int, input().split())
ans = round((T+0.5)//A)*B
print(ans)
Buggy python submission (verdict: Runtime Error):
def mygcd(N,A,S):
    if S == 'R':
        A.reverse()
    ans = [0, A[0]]
    for i in range(1, N):
        ans.append(math.gcd(ans[i],A[i]))
    if S == 'R':
        ans.reverse()
    return ans

def GCDonBlack(N,A):
    L = mygcd(N,A,'L')
    R = mygcd(N,A,'R')
    M = [math.gcd(L[i], R[i+1])  for i  in range(0,N)]
    return max(M)

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

Fixed submission:
import fractions
def mygcd(N,A,S):
    if S == 'R':
        A.reverse()
    ans = [0, A[0]]
    for i in range(1, N):
        ans.append(fractions.gcd(ans[i],A[i]))
    if S == 'R':
        ans.reverse()
    return ans

def GCDonBlack(N,A):
    L = mygcd(N,A,'L')
    R = mygcd(N,A,'R')
    M = [fractions.gcd(L[i], R[i+1])  for i  in range(0,N)]
    return max(M)

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

Buggy python submission (verdict: Runtime Error):
def GCDonBlack(N,A):
    #count = 0
    rem=[a%min(A) for a in A]
    if sum(rem) == 0:
        return min(A)
    else:
        for i in reversed(range(1,max(rem)+1)):
            count += 1
            bar = N-1
            for ele in A:
                if ele % i == 0:
                    bar -= 1
                if bar == 0:
                    #print("count:{}".format(count))
                    return i
N = int(input())
A = list(map(int, input().split()))
print(GCDonBlack(N,A))

Fixed submission:
import fractions
def mygcd(N,A,S):
    if S == 'R':
        A.reverse()
    ans = [0, A[0]]
    for i in range(1, N):
        ans.append(fractions.gcd(ans[i],A[i]))
    if S == 'R':
        ans.reverse()
    return ans

def GCDonBlack(N,A):
    L = mygcd(N,A,'L')
    R = mygcd(N,A,'R')
    M = [fractions.gcd(L[i], R[i+1])  for i  in range(0,N)]
    return max(M)

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

Buggy python submission (verdict: Time Limit Exceeded):
def GCDonBlack(N,A):
    if sum([a%min(A) for a in A]) == 0:
        return min(A)
    else:
        for i in reversed(range(1,max(A))):
            bar = N-1
            for ele in A:
                if ele % i == 0:
                    bar -= 1
                if bar == 0:
                    return i

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

Fixed submission:
import fractions
def mygcd(N,A,S):
    if S == 'R':
        A.reverse()
    ans = [0, A[0]]
    for i in range(1, N):
        ans.append(fractions.gcd(ans[i],A[i]))
    if S == 'R':
        ans.reverse()
    return ans

def GCDonBlack(N,A):
    L = mygcd(N,A,'L')
    R = mygcd(N,A,'R')
    M = [fractions.gcd(L[i], R[i+1])  for i  in range(0,N)]
    return max(M)

N = int(input())
A = list(map(int, input().split()))
print(GCDonBlack(N,A))
Buggy python submission (verdict: Time Limit Exceeded):
# coding:utf-8

import sys
from collections import deque

INF = float('inf')
MOD = 10 ** 9 + 7
dy, dx = (0, -1, 0, 1), (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    h, w = LI()
    B = [list(SI()) for _ in range(h)]

    D = deque()
    for i, row in enumerate(B):
        for j, c in enumerate(row):
            if c == '#':
                D.append((i, j, 0))

    while D:
        y, x, cnt = D.popleft()
        for i in range(4):
            yy = y + dy[i]
            xx = x + dx[i]

            if 0 <= yy < h and 0 <= xx < w:
                if B[yy][xx] == '#':
                    continue
                B[yy][xx] = '#'
                D.append((yy, xx, cnt + 1))

    print(cnt)


if __name__ == '__main__':
    main()


Fixed submission:
# coding:utf-8

import sys
from collections import deque

INF = float('inf')
MOD = 10 ** 9 + 7
dy, dx = (0, -1, 0, 1), (1, 0, -1, 0)

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    h, w = LI()
    B = [list(SI()) for _ in range(h)]

    D = deque()
    for i, row in enumerate(B):
        for j, c in enumerate(row):
            if c == '#':
                D.append((i, j, 0))

    while D:
        y, x, cnt = D.popleft()
        for i in range(4):
            yy = y + dy[i]
            xx = x + dx[i]

            if 0 <= yy < h and 0 <= xx < w:
                if B[yy][xx] == '#':
                    continue
                B[yy][xx] = '#'
                D.append((yy, xx, cnt + 1))

    print(cnt)




if __name__ == '__main__':
    main()
Buggy python submission (verdict: Runtime Error):
# coding:utf-8

import sys
# from collections import Counter, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, q = LI()
    S = SI()

    B = [LS() for _ in range(q)]
    l, r = 0, n - 1
    ls, rs = '@', '@'
    cl, cr = 0, 0
    for t, d in B[::-1]:
        if d == 'L':
            if t == S[l]:
                cl = l + 1
                l += 1
                ls = t
            if rs == t:
                cr -= 1

        else:
            if t == S[r]:
                cr = n - r
                r -= 1
                rs = t
            if ls == t:
                cl -= 1

    return max(0, n - cl - cr)


print(main())


Fixed submission:
# coding:utf-8

import sys

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def main():
    n, q = LI()
    S = SI()

    Q = [LS() for _ in range(q)]

    def left_out(i):
        if i < 0: return True
        if i >= n: return False
        for t, d in Q:
            if t != S[i]: continue
            if d == 'L':
                i -= 1
                if i < 0: return True
            else:
                i += 1
                if i >= n: return False

        return False

    def right_out(i):
        if i < 0: return False
        if i >= n: return True
        for t, d in Q:
            if t != S[i]: continue
            if d == 'L':
                i -= 1
                if i < 0: return False
            else:
                i += 1
                if i >= n: return True

        return False

    # 左へ落ちるロボット
    l_ok, l_ng = -1, n
    while abs(l_ok - l_ng) > 1:
        mid = (l_ok + l_ng) // 2
        if left_out(mid):
            l_ok = mid
        else:
            l_ng = mid

    # 右へ落ちるロボット
    r_ok, r_ng = n, -1
    while abs(r_ok - r_ng) > 1:
        mid = (r_ok + r_ng) // 2
        if right_out(mid):
            r_ok = mid
        else:
            r_ng = mid

    return max(0, r_ok - l_ok - 1)


print(main())
Buggy python submission (verdict: Time Limit Exceeded):
# coding:utf-8

import sys
from collections import deque, defaultdict

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


def topological_sort(graph: dict) -> list:
    stack = deque([])
    visited = set()
    not_leaf = set(graph.keys())
    sort_list = []

    for i in graph.keys():
        if i in visited:
            continue
        stack.append(i)

        while stack:
            v = stack[-1]
            if v not in not_leaf:
                sort_list.append(stack.pop())
                visited.add(v)
                continue

            for next in graph[v]:
                if next not in visited:
                    stack.append(next)
                    break
            else:
                sort_list.append(stack.pop())
                visited.add(v)

    return sort_list[::-1]


n, m = LI()
T = defaultdict(list)

for _ in range(n + m - 1):
    a, b = LI()
    T[a].append(b)

ts_list = topological_sort(T)


tree = defaultdict(int)
tree[ts_list[0]] = 0
queue = deque([ts_list[0]])
while queue:
    v = queue.popleft()
    for next in T[v]:
        tree[next] = v
        queue.append(next)

print(*tree.values(), sep='\n')


Fixed submission:
# coding:utf-8

import sys
from collections import deque
from operator import itemgetter

INF = float('inf')
MOD = 10 ** 9 + 7

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()


n, m = LI()

G = [[] for _ in range(n)]
RG = [[] for _ in range(n)]
indegree = [0] * n

for _ in range(n + m - 1):
    a, b = LI_()
    G[a].append(b)
    RG[b].append(a)
    indegree[b] += 1

# Topological sort
que = deque()
for i in range(n):
    if indegree[i] == 0:
        que.append(i)

res = []
while que:
    v = que.popleft()
    res.append(v)
    for v2 in G[v]:
        indegree[v2] -= 1
        if indegree[v2] == 0:
            que.append(v2)

# 親の候補が複数ある場合
# 木の高さが最大になるような候補を選ぶ
# -> トポロジカル順序で最も後ろにある候補を選ぶ
order = [i for i, n in sorted(enumerate(res), key=itemgetter(1))]
for i in range(n):
    if len(RG) == 0:
        print(0)
        continue

    parent = -1
    d = -1
    for j in RG[i]:
        if order[j] > d:
            parent = j
            d = order[j]

    print(parent + 1)