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

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
graph = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)



def dfs(v, dist, memo):
    memo[v] = dist
    for nv in graph[v]:
        if memo[nv] >= 0:
            continue
        dfs(nv, dist+1, memo)


memo = [-1] * n
dfs(0, 0, memo)

node = 0
dist = 0
for i in range(n):
    if memo[i] > dist:
        dist = memo[i]
        node = i


memo = [-1] * n
dfs(node, 0, memo)

dist = max(memo)
count = dist + 1

dp = [0] * (count + 10)
dp[0] = 0
dp[1] = 1
dp[2] = 0
dp[3] = 1
dp[4] = 1


if count >= 5:
    for i in range(5, count+1):
        if dp[i-2] == 1 and dp[i-1] == 1:
            dp[i] = 0
        else:
            dp[i] = 1

if dp[count]:
    print("First")
else:
    print("Second")



### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

n = int(input())
graph = [[] for _ in range(n)]
for i in range(n-1):
    a, b = map(int, input().split())
    a -= 1
    b -= 1
    graph[a].append(b)
    graph[b].append(a)


def dfs(v, dist, memo):
    memo[v] = dist
    for nv in graph[v]:
        if memo[nv] >= 0:
            continue
        dfs(nv, dist+1, memo)

memo = [-1] * n
dfs(0, 0, memo)

node = 0
dist = 0
for i in range(n):
    if memo[i] > dist:
        dist = memo[i]
        node = i

memo = [-1] * n
dfs(node, 0, memo)

dist = max(memo)
count = dist + 1

dp = [0] * (count + 10)
dp[0] = 0
dp[1] = 1
dp[2] = 0
dp[3] = 1
dp[4] = 1


if count >= 5:
    for i in range(5, count+1):
        if dp[i-2] == 1 and dp[i-1] == 1:
            dp[i] = 0
        else:
            dp[i] = 1

if dp[count]:
    print("First")
else:
    print("Second")
### BUGGY CODE ###

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


c = 1
for i in range(1, n ):

    if p[i] <= min(p[0:i ]) :
        c = c + 1
print(c)

### FIXED CODE ###

n = int(input())
p = list(map(int, input().split()))
c = 0
m =  p[0]
for i in range( n ):
    if p[i] <= m :
        c = c + 1
        m = p[i]
print(c)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
aset = set(a)
b = []
c = []
for i in aset:
    if a.count(i)>1:
        b.append(i)
    
    if a.count(i)>3:
        c.append(i)
b.sort()
c.sort()
m = len(b)
if m<2:
    if len(c)<1:
        print(0)
    else:
        print(c[-1]*c[-1])
else:
    if len(c)<1:
        print(b[-1]*b[-2])
    else:
        ans = max(b[-2]*b[-1],c[-1]*c[-1])
        print(ans)

### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
i = 1
j = 1
l = []
while i<n:
    if a[i]==a[i-1]:
        l.append(a[i])
        i+=2
    else:
        i+=1
l.append(0)
l.append(0)
print(l[0]*l[1])
### BUGGY CODE ###

import sys
input = sys.stdin.readline
n = int(input())
h = list(map(int,input().split()))
count = [0]*n
for i in range(n-1):
  for j in range(i+1,n):
    if h[j-1] >= h[j]:
      count[i] += 1
    else:
      break

m = max(count)
print(m)

### FIXED CODE ###

import sys
input = sys.stdin.readline
n = int(input())
h = list(map(int,input().split()))

count,m = 0,0

for i in range(n-1):
  if h[i] >= h[i+1]:
    count += 1
  else:
    m = max(m,count)
    count = 0
m = max(m,count)
print(m)
### BUGGY CODE ###

#n=int(input())
#n,m=list(map(int,input().split()))
#a=list(map(int,input().split()))
import sys
sys.setrecursionlimit(10**6)
import math
import itertools
from collections import defaultdict
from collections import Counter
import numpy as np

import heapq
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
od=998244353
n,k,x=list(map(int,input().split()))

n_c_i=[1]
for i in range(1,x+1):
    n_c_i.append(n_c_i[-1]*(n-i)//i)
#print(n_c_i)
pow_k=pow(k-1,n-1,od)
inv_k=pow(k-1,od-2,od)
ans=0
#print(ans)
for i in range(x+1):
        
    #pow_k=(pow_k*inv_k)%od
    p_k=(k*pow(k-1,n-i-1,od))%od
    #ans+=comb*k*pow_k%od
    ans+=(n_c_i[i]*p_k)%od
    ans%=od

print(ans)



### FIXED CODE ###

#n=int(input())
#n,m=list(map(int,input().split()))
#a=list(map(int,input().split()))
import sys
sys.setrecursionlimit(10**6)
import math
import itertools
from collections import defaultdict
from collections import Counter
import numpy as np

import heapq
def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))
od=998244353
n,k,x=list(map(int,input().split()))

n_c_i=[1]
for i in range(1,x+1):
    n_c_i.append((n_c_i[-1]*(n-i)*pow(i,od-2,od))%od)
#print(n_c_i)
pow_k=pow(k-1,n-1,od)
inv_k=pow(k-1,od-2,od)
ans=0
#print(ans)
for i in range(x+1):
        
    #pow_k=(pow_k*inv_k)%od
    p_k=(k*pow(k-1,n-i-1,od))%od
    #ans+=comb*k*pow_k%od
    ans+=(n_c_i[i]*p_k)%od
    ans%=od

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

# -*- coding: utf-8 -*-
 
N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N):
    if P[i] <= min(P[:i+1]):
        ans += 1
print(ans)

### FIXED CODE ###

# -*- coding: utf-8 -*-
 
N = int(input())
P = list(map(int, input().split()))

ans = 0
min_val = P[0]
for i in range(N):
    if P[i] <= min(min_val, P[i]):
        ans += 1
        min_val = min(min_val, P[i])
print(ans)
### BUGGY CODE ###

def main():
    import collections
    N, K = map(int, input().split())
    A = list(map(int, input().split()))

    s = [0]*(N+1)
    for i in range(N):
        s[i+1] = s[i] + A[i]

    table = collections.defaultdict(set)
    for i, x in enumerate(s):
        table[x % K].add(i)
    ans = 0
    for x in table:
        for y in table:
            if x == y:
                continue
            else:
                for z in table[x]:
                    k = (x-y) % K
                    if z-k in table[y]:
                        ans += 1
                    k = (y-x) % K
                    if z+k in table[y]:
                        ans += 1
    print(ans//2)
main()

### FIXED CODE ###

import collections
N, K = map(int, input().split())
A = list(map(int, input().split()))
table = collections.defaultdict(int)

ans = 0
s = [0]*(N+1)
a = set()
for i in range(N):
    s[i+1] = s[i]+A[i]

s = [(s[i]-i) % K for i in range(N+1)]
for i in range(N+1):
    ans += table[s[i]]  # i番目まで取る組み合わせの数
    table[s[i]] += 1
    if i >= K-1:
        # Kで割ったあまりがKを超えることはないので、期限切れのものを引く
        table[s[i-K+1]] -= 1

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

from collections import defaultdict
class UnionFind:
    def __init__(self,nodes):
        self.parents = {node : node for node in nodes}
    def root(self,x):
        if x == self.parents[x]:
            return x
        else:
            rt = self.root(self.parents[x])
            self.parents[x] = rt
            return rt
    def find(self,x,y):
        return self.root(x) == self.root(y)
    def union(self,x,y):
        self.parents[self.root(x)] = self.root(y)
    def get_sets(self):
        res = defaultdict(list)
        for node, parent in self.parents.items():
            res[self.root(node)].append(node)
        # return [*res.values()]
        return list(res.values())

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

uf = UnionFind(range(1,N+1))

for i in [0]*M:
    x, y = map(int,input().split(' '))
    uf.union(x,y)

ans = 0
for n in range(1,N+1):
    if uf.find(n,p[n-1]):
        ans += 1
print(ans)





### FIXED CODE ###

from collections import defaultdict
class UnionFind:
    def __init__(self,nodes):
        self.parents = {node : node for node in nodes}
    def root(self,x):
        if x == self.parents[x]:
            return x
        else:
            rt = self.root(self.parents[x])
            self.parents[x] = rt
            return rt
    def find(self,x,y):
        return self.root(x) == self.root(y)
    def union(self,x,y):
        self.parents[self.root(x)] = self.root(y)
    def get_sets(self):
        res = defaultdict(list)
        for node, parent in self.parents.items():
            res[self.root(node)].append(node)
        # return [*res.values()]
        return list(res.values())

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

uf = UnionFind(range(1,N+1))

for i in [0]*M:
    x, y = map(int,input().split(' '))
    uf.union(x,y)

ans = 0
for n in range(1,N+1):
    if uf.find(n,p[n-1]):
        ans += 1
print(ans)
### BUGGY CODE ###

def factorial_mod(n, mod):
    a = 1
    for i in range(1, n + 1):
        a *= i
        a %= mod
    return a


def comb_mod(n, k, mod):
    fact_n = factorial_mod(n, mod)
    fact_k = factorial_mod(k, mod)
    fact_n_k = factorial_mod(n - k, mod)
    return (fact_n * pow(fact_k, mod - 2, mod) * pow(fact_n_k, mod - 2, mod)) % mod


n, m, k = map(int, input().split())
s = 0
mod = 10 ** 9 + 7
for x in range(m):
    for i in range(x, m):
        s += abs(x-i) * n **2
for y in range(n):
    for j in range(y, n):
        s += abs(y-j) * m ** 2


print(s * comb_mod(n * m - 2, k - 2, mod) % mod)


### FIXED CODE ###

def factorial_mod(n, mod):
    a = 1
    for i in range(1, n + 1):
        a *= i
        a %= mod
    return a


def comb_mod(n, k, mod):
    fact_n = factorial_mod(n, mod)
    fact_k = factorial_mod(k, mod)
    fact_n_k = factorial_mod(n - k, mod)
    return (fact_n * pow(fact_k, mod - 2, mod) * pow(fact_n_k, mod - 2, mod)) % mod


n, m, k = map(int, input().split())
s = 0
mod = 10 ** 9 + 7
for x in range(m):
    s += (m - x - 1) * (m - x) // 2 * n **2
for y in range(n):
    s += (n - y - 1) * (n - y) // 2 * m ** 2


print(s * comb_mod(n * m - 2, k - 2, mod) % mod)
### BUGGY CODE ###

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


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

BC_list = []

for i in range(M):
    B, C = map(int, input().split())

    BC_list.append({
        'B': B,
        'C': C
    })


BC_list.sort(reverse=True, key=lambda x: x['C'])

A_last = min(A)

for pair in BC_list:

    if pair['C'] < A_last:
        break

    for _ in range(pair['B']):
        A.append(pair['C'])

A.sort(reverse=True)

print(sum(A[0:N]))


### FIXED CODE ###

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


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

BC_list = []

for i in range(M):
    B, C = map(int, input().split())

    BC_list.append({
        'B': B,
        'C': C
    })

BC_list.sort(reverse=True, key=lambda x: x['C'])

A_last = min(A)

limit_count = 0

for pair in BC_list:

    if pair['C'] < A_last:
        break

    for _ in range(pair['B']):
        A.append(pair['C'])

    limit_count += pair['B']
    if limit_count > N:
        break

A.sort(reverse=True)

print(sum(A[0:N]))
### BUGGY CODE ###

n, m =map(int,input().split())
p = []
for i in range(m):
    p.append(input().split())

del_l = []
for j in range(m):
    if p[j][1] == 'AC' and p[j][0] not in del_l:
        del_l.append(p[j][0])
    if p[j][1] =='WA' and p[j][0] in del_l:
        p[j][0] = 'pass'
        p[j][1] = 'pass'
pen = 0
for s in range(m):
    if p[s][1] == 'WA' and p[s][0] in del_l:
        pen += 1
print(len(del_l),pen)

### FIXED CODE ###

n, m = map(int,input().split())
a = [input().split() for i in range(m)]
p = [int(i[0]) for i in a]
q = [j[1] for j in a]

ac = 0
wa = 0
wa_c = [0] * n
ac_c = [False] * n

for t in range(m):
    if ac_c[p[t]-1]:
        continue
    if q[t] == 'AC':
        ac += 1
        ac_c[p[t]-1] = True
        wa += wa_c[p[t]-1]
    else:
        wa_c[p[t]-1] += 1
print(ac,wa)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
res = 1
for a_i in a:
    if 0 == a_i:
        print(0)
        exit(0)
    elif res > 10 ** 18:
        res = -1
    else:
        res *= a_i

print(-1 if res > 10 ** 18 else res)


### FIXED CODE ###

n = int(input())
a = list(map(int, input().split()))
res = 1
for a_i in a:
    if 0 == a_i:
        print(0)
        exit(0)
    elif res > 10 ** 18:
        continue
    else:
        res *= a_i

print(-1 if res > 10 ** 18 else res)
### BUGGY CODE ###

import sys
from collections import deque


input = sys.stdin.readline
n, m = map(int, input().split())
info = tuple(map(int, input().split()) for i in range(m))
s, t = map(int, input().split())

graph = [[] for i in range(n)]
for i, j in info:
    graph[i-1].append(j-1)


graph2 = [[] for i in range(n)]
q = deque()
q.append((s - 1, 0))
INF = float("inf")
visited = [INF] * n
visited[0] = 0

while q:
    pos, times = q.popleft()
    for i in graph[pos]:
        if (times + 1) % 3 != 0:
            q.append((i, times + 1))
        if (times + 1) % 3 == 0 and times + 1 < visited[i]:
            visited[i] = times + 1
            q.append((i, times+1))

if visited[t-1] == float("inf"):
    print(-1)
else:
    print(visited[t-1]//3)
    

### FIXED CODE ###

import sys
from collections import deque


input = sys.stdin.readline
n, m = map(int, input().split())
info = tuple(map(int, input().split()) for i in range(m))
s, t = map(int, input().split())

graph = [[] for i in range(n)]
for i, j in info:
    graph[i-1].append(j-1)

q = deque()
q.append((s - 1, 0))
INF = 10 ** 10
visited = [10 ** 10] * n
visited_1 = [10 ** 10] * n
visited_2 = [10 ** 10] * n
visited[s - 1] = 0
while q:
    pos, times = q.popleft()
    for i in graph[pos]:
        if (times + 1) % 3 == 1 and times + 1 < visited_1[i]:
            visited_1[i] = times + 1
            q.append((i, times + 1))

        elif (times + 1) % 3 == 2 and times + 1 < visited_2[i]:
            visited_2[i] = times + 1
            q.append((i, times + 1))
            
        elif (times + 1) % 3 == 0 and times + 1 < visited[i]:
            visited[i] = times + 1
            q.append((i, times + 1))

if visited[t - 1] == 10 ** 10:
    print(-1)
else:
    print(visited[t-1]//3)
### BUGGY CODE ###

a,b,k=map(int,input().split())
n =[]
for i in range(a,b+1):
    n.append(i)
nf=n[:k]
nb=n[len(n)-k:]
n=nf+nb
n=list(set(n))
n.sort()
for i in n:
    print(i)

### FIXED CODE ###

a,b,k=map(int,input().split())
n =[]
for i in range(a,a+k if k<(b-a) else b+1):
    n.append(i)
for i in range(b-k+1 if k<(b-a) else b+1,b+1):
    if(i not in n):
        n.append(i)
for i in n:
    print(i)
### BUGGY CODE ###

n = int(input())
left = 0
right = n - 1
while left != right:
    mid = (left + right) // 2
    print(left)
    sl = input()
    if sl[0] == "V":
        quit()
    print(right)
    sr = input()
    if sr[0] == "V":
        quit()
    print(mid)
    sm = input()
    if sm[0] == "V":
        quit()

    if sm - sl + 1 % 2 == 1 and sm != sl:
        left += 1
        right = mid - 1
        continue
    elif right - mid + 1 % 2 == 1 and sm != sr:
        right -= 1
        left = mid - 1
        continue
    elif sr - sm + 1 % 2 == 1 and sm == sl:
        left += 1
        right= mid - 1
        continue
    else:
        left = mid + 1
        right -= 1



### FIXED CODE ###

n = int(input())

left = 0
right = n
print(left, flush=True)
sl = input()
if sl[0] == "V":
    quit()
while right - left > 1:
    mid = (left + right) // 2
    print(mid, flush=True)
    sm = input()
    if sm[0] == "V":
        quit()

    if ((mid - left) % 2 == 1) ^ (sl == sm):
        left = mid
        sl = sm
    else:
        right = mid
### BUGGY CODE ###

N, K = map(int, input().split())
cnt = [int(x) * 0.5 + 0.5 for x in input().split()]

ans = [sum(cnt[x:x+K]) for x in range(N-K+1)] #if len(cnt[x:x+K]) == K]
print(max(ans))

### FIXED CODE ###

N, K = map(int, input().split())
cnt = [int(x) * 0.5 + 0.5 for x in input().split()]

add = [0]
for i in cnt:
    add.append(add[-1]+i)

ans = [add[i+K]-add[i] for i in range(N-K+1)]

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

n,k=map(int,input().split())
xy=[list(map(int,input().split())) for i in range(n)]
x,y=[[xy[i][0],i] for i in range(n)],[[xy[i][1],i] for i in range(n)]
x.sort()
y.sort()
ans=10**18*4
def upper_k(x_sub1,x_sub2,y_sub1,y_sub2):
    global n,k,xy
    z=[0]*n
    for i in range(n):
        if x_sub1<=xy[i][0]<=x_sub2 and y_sub1<=xy[i][1]<=y_sub2:
            z[i]=1
    return sum(z)>=k

for i1 in range(n):
    i1_sub=n-i1
    x_sub1=x[i1][0]
    for l1 in range(i1_sub,1,-1):
        x_sub2=x[i1+l1-1][0]
        for i2 in range(n):
            i2_sub=n-i2
            y_sub1=y[i2][0]
            l,r=2,i2_sub
            if r<l:
                break
            while l+1<r:
                t=(l+r)//2
                y_sub2=y[i2+t-1][0]
                if upper_k(x_sub1,x_sub2,y_sub1,y_sub2):
                    r=t
                else:
                    l=t
            y_sub2=y[i2+r-1][0]
            if upper_k(x_sub1,x_sub2,y_sub1,y_sub2):
                ans=min((x_sub2-x_sub1)*(y_sub2-y_sub1),ans)
            if l!=r:
                y_sub2=y[i2+l-1][0]
                if upper_k(x_sub1,x_sub2,y_sub1,y_sub2):
                    ans=min((x_sub2-x_sub1)*(y_sub2-y_sub1),ans)
print(ans)


### FIXED CODE ###

n,k=map(int,input().split())
x,y=[[0,0] for i in range(n)],[[0,0] for i in range(n)]
xy=[list(map(int,input().split())) for i in range(n)]
for i in range(n):
    x_sub,y_sub=xy[i]
    x[i][0]=x_sub
    x[i][1]=i
    y[i][0]=y_sub
    y[i][1]=i
x.sort()
y.sort()
ans=(10**18)*5
for i1 in range(n):
    for l1 in range(n-i1,1,-1):
        for i2 in range(n):
            for l2 in range(n-i2,1,-1):
                x_sub1=x[i1][0]
                x_sub2=x[i1+l1-1][0]
                y_sub1=y[i2][0]
                y_sub2=y[i2+l2-1][0]
                z=[0]*n
                for i in range(n):
                    if x_sub1<=xy[i][0]<=x_sub2 and y_sub1<=xy[i][1]<=y_sub2:
                        z[i]=1
                if sum(z)>=k:
                    ans=min((x_sub2-x_sub1)*(y_sub2-y_sub1),ans)
                else:
                    break
print(ans)
### BUGGY CODE ###

n = int(input())
A = list(range(1,n+1))
B = list(range(1,n+1))
C = list(range(1,n+1))
ans =0
for i in A:
    for j in B:
        for k in C:
            if i*j+k ==n:
                ans +=1
print(ans)

### FIXED CODE ###

def slove():
    ans = 0
    for a in range(1,N):
        for b in range(1,N):
            c = N-a*b
            if c<=0: #a,b,cは>0なのでcが負になるときは組み合わせ除外
                break
            ans +=1
    return ans
N = int(input())
ans = slove()
print(ans)
### BUGGY CODE ###

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

ans=10**9
c=0
for i in range(l,r+1):
    for j in range(l,r+1):
        if i!=j:
            ans=min(ans,(i*j)%2019)
        c+=1
        if c>2019**2:
            continue
print(ans)
    

### FIXED CODE ###

l,r=map(int,input().split())
r=min(r,l+2020)
  
ans=10**9
c=0
for i in range(l,r+1):
    for j in range(l,r+1):
        if i!=j:
            ans=min(ans,(i*j)%2019)
        c+=1
print(ans)
### BUGGY CODE ###

import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
a = 0
for i in range(n):
	L,R = 0,0
	for j in range(0,i):
		L = math.gcd(L,list1[j])
	for j in range(i+1,n):
		R = math.gcd(R,list1[j])
	a = max(a,math.gcd(L,R))

print(a)

### FIXED CODE ###

from collections import deque
import fractions as math
n = int(input())
list1 = list(map(int,input().split()))
list1 = deque(list1)
L = deque([0])
R = deque([0])
M = []

for i in range(len(list1)-1):
	L.append(math.gcd(L[i],list1[i]))
	R.append(math.gcd(R[i],list1[n-i-1]))

for i in range(len(L)):
	M.append(math.gcd(L[i],R[len(R)-i-1]))
print(max(M))
### BUGGY CODE ###

import heapq
import sys

input = sys.stdin.readline
INF = 10**18

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

graph_s = [[] for _ in range(n)]
graph_t = [[] for _ in range(n)]

for e in edge:
    graph_s[e[0]-1].append([e[1]-1,e[2]])
    graph_s[e[1]-1].append([e[0]-1,e[2]])
    graph_t[e[0]-1].append([e[1]-1,e[3]])
    graph_t[e[1]-1].append([e[0]-1,e[3]])

dist_s = [INF]*n
dist_t = [INF]*n

dist_s[s-1] = 0
dist_t[t-1] = 0

heap_s = [[0,s-1]]
heapq.heapify(heap_s)

heap_t = [[0,t-1]]
heapq.heapify(heap_t)

while heap_s:
    cost,node = heapq.heappop(heap_s)
    if dist_s[node] < cost:
        continue
    for adj,adjcost in graph_s[node]:
        if dist_s[node] + adjcost < dist_s[adj]:
            dist_s[adj] = dist_s[node] + adjcost
            heapq.heappush(heap_s,[dist_s[adj],adj])

while heap_t:
    cost,node = heapq.heappop(heap_t)
    if dist_t[node] < cost:
        continue
    for adj,adjcost in graph_t[node]:
        if dist_t[node] + adjcost < dist_t[adj]:
            dist_t[adj] = dist_t[node] + adjcost
            heapq.heappush(heap_t,[dist_t[adj],adj])

dist = INF
ans = []
                                              
for i in range(n):
    dist = min(dist,dist_t[-1-i]+dist_s[-1-i])
    ans.append(10**15-dist)
    
for i in range(n):
    print(ans[-1-i])

### FIXED CODE ###

import heapq
import sys

input = sys.stdin.readline
INF = 10**18

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

graph_s = [[] for _ in range(n)]
graph_t = [[] for _ in range(n)]

for e in edge:
    graph_s[e[0]-1].append([e[1]-1,e[2]])
    graph_s[e[1]-1].append([e[0]-1,e[2]])
    graph_t[e[0]-1].append([e[1]-1,e[3]])
    graph_t[e[1]-1].append([e[0]-1,e[3]])

dist_s = [INF]*n
dist_t = [INF]*n

dist_s[s-1] = 0
dist_t[t-1] = 0

heap_s = [[0,s-1]]
heapq.heapify(heap_s)

heap_t = [[0,t-1]]
heapq.heapify(heap_t)

while heap_s:
    cost,node = heapq.heappop(heap_s)
    if dist_s[node] < cost:
        continue
    for adj,adjcost in graph_s[node]:
        if dist_s[node] + adjcost < dist_s[adj]:
            dist_s[adj] = dist_s[node] + adjcost
            heapq.heappush(heap_s,[dist_s[adj],adj])

while heap_t:
    cost,node = heapq.heappop(heap_t)
    if dist_t[node] < cost:
        continue
    for adj,adjcost in graph_t[node]:
        if dist_t[node] + adjcost < dist_t[adj]:
            dist_t[adj] = dist_t[node] + adjcost
            heapq.heappush(heap_t,[dist_t[adj],adj])

dist = INF
ans = []
                                              
for i in range(n):
    dist = min(dist,dist_t[-1-i]+dist_s[-1-i])
    ans.append(10**15-dist)
    
for i in range(n):
    print(ans[-1-i])
### BUGGY CODE ###

import heapq
def SME(start,end):
    A = []
    if end > N : end = N
    count = 0
    for i,x in enumerate(Whole[start:end]):
        heapq.heappush(A,[x,i])
    for j in range(start,end):
        a = heapq.heappop(A)
        for i in range(L):
            idx = a[1] + i
            try :
                if ANS[idx] : continue
                else :
                    ANS[idx] = a[0]
                    count += 1
            except IndexError : continue
        if count > N * 0.7 : break
    for i in range(L):
        if ANS[i] : continue
        else : ANS[i] = min(Whole[:i+1])
    for i in range(L,N):
        if ANS[i] : continue
        else : ANS[i] = min(Whole[i+1-L:i+1])
    return ANS

[N,L] = [int(x) for x in input().split()]
Whole = [int(x) for x in input().split()]
ANS = [None]*N
ANS = SME(0, N)
for i in ANS[L-1:-1]:print(i, end=" ")
print(ANS[-1])

### FIXED CODE ###

def ascend(a,b):
    mini = min(A[a:a+L])
    ans[a] = mini
    for i in range(a+1,b):
        if A[i+L-1] <= mini : mini = A[i+L-1]
        elif A[i-1] == mini : mini = min(A[i:i+L])
        ans[i] = mini

def descend(a,b):
    mini = min(A[b-1:b+L-1])
    ans[b-1] = mini
    for i in range(b-2,a-1,-1):
        if A[i] <= mini : mini = A[i]
        elif A[i+L] == mini: mini = min(A[i:i+L])
        ans[i] = mini
    
[N,L] = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
ans = [None]*(N-L+1)
count = 0;
if N > 10 ** 4:
    for i in range(100):
        if A[i+1] > A[i] : count += 1
    if count > 80 : descend(0, N-L+1)
    else : ascend(0,N-L+1)
else :
    ascend(0, N-L+1)
for i in ans[:-1] : print(i, end=" ")
print(ans[-1])
### BUGGY CODE ###

import re
N, Q = map(int, input().split())
S = list(input())
qs = [list(map(int, input().split())) for _ in range(Q)]
pattern = 'AC'
for q in qs:
    string = ''.join(S[q[0]-1:q[1]])
    m = re.findall(pattern, string)
    print(len(m))

### FIXED CODE ###

import sys
input = sys.stdin.readline
N, Q = map(int, input().split())
S = list(input())
lr = [list(map(int, input().split())) for _ in range(Q)]
cnt = [0]*N
is_a = False
for i in range(N-1):
  if S[i] + S[i+1] == 'AC':
    cnt[i+1] = 1
for i in range(1, N):
  cnt[i] += cnt[i-1]

for l, r in lr:
  l -= 1
  r -= 1
  print(cnt[r]-cnt[l])