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

from collections import deque


h,w=map(int,input().split())
c=list(map(int,input().split()))
d=list(map(int,input().split()))
s=[list(input()) for _ in range(h)]
dict={".":1000000,"#":-1}
s=[[dict[s[y][x]] for x in range(w)]for y in range(h)]

s[c[0]-1][c[1]-1]=0
que=deque([[c[0]-1,c[1]-1]])
temp=[(1,0),(0,1),(-1,0),(0,-1),(2,2),(2,1),(2,0),(2,-1),(2,-2),(1,2),(1,1),(1,-1),(1,-2),(0,2),(0,-2),(-1,2),(-1,1),(-1,-1),(-1,-2),(-2,2),(-2,1),(-2,0),(-2,-1),(-2,-2)]
while que:
    y,x=que.popleft()
    for i,j in temp:
        if 0<=y+i<h and 0<=x+j<w and s[y+i][x+j]>=0:
            nexty,nextx=y+i,x+j
            dist=s[nexty][nextx]
            if i*i+j*j==1 and dist>s[y][x]:
                s[nexty][nextx]=s[y][x]
                que.append([nexty,nextx])
            elif dist>s[y][x]+1:
                s[nexty][nextx]=s[y][x]+1
                que.append([nexty,nextx])

if s[d[0]-1][d[1]-1]==1000000:
    print(-1)
else:
    print(s[d[0]-1][d[1]-1])


### FIXED CODE ###

from collections import deque
from sys import stdin

def bfs():
    readline=stdin.readline
    h,w=map(int,readline().split())
    sy,sx=map(int,readline().split())
    gy,gx=map(int,readline().split())
    sy-=1;sx-=1;gy-=1;gx-=1
    s=[list(readline()) for _ in range(h)]
    dict={".":10000000,"#":-1}
    s=[[dict[s[y][x]] for x in range(w)]for y in range(h)]
    s[sy][sx]=0
    que=deque([[sy,sx]])
    temp=[i for i in range(-2,3)]
    while que:
        y,x=que.popleft()
        s1=s[y][x]
        for i in temp:
            nexty=y+i
            if 0<=nexty<h:
                s2=s[nexty]
                for j in temp:
                    nextx=x+j
                    if 0<=nextx<w and s2[nextx]>=0:
                        dist=s2[nextx]
                        if i*i+j*j<=1 and dist>s1:
                            s[nexty][nextx]=s1
                            que.appendleft([nexty,nextx])
                        elif dist>s1+1:
                            s[nexty][nextx]=s1+1
                            que.append([nexty,nextx])

    if s[gy][gx]==10000000:
        print(-1)
    else:
        print(s[gy][gx])
bfs()
### BUGGY CODE ###

n = int(input())
a = [int(input()) for i in range(n)]

m = max(a)

for i in range(n):
  if a[i]!=m:
    print(m)
  else:
    ans = max([x for j, x in enumerate(a) if j!=i])
    print(ans)

### FIXED CODE ###

n = int(input())
a = [int(input()) for i in range(n)]

b = sorted(a, reverse=True)

for i in range(n):
    if a[i] == b[0]:
        print(b[1])
    else:
        print(b[0])
### BUGGY CODE ###

from functools import lru_cache
N = int(input())
A = list(map(int, input().split()))
import sys
sys.setrecursionlimit(100000)

import random
#N = 20
#A = [random.randint(1, 10**9) for _ in range(N)]


@lru_cache(maxsize=None)
def f(l, r):
  if l > r:
    return 0
  
  n = r - l
  if (N - n) % 2 == 0:
    s = -1
  else:
    s = 1
    
  lv = f(l+1, r) + s*A[l]
  rv = f(l, r-1) + s*A[r]
  
  if s*lv > s*rv:
    return lv
  else:
    return rv
    
   
print(f(0, N-1))


### FIXED CODE ###

N = int(input())
A = list(map(int, input().split()))
#import random
#N = 3000
#A = [random.randint(1, 10**9) for _ in range(N)]


dp = [[0] * (N+1) for _ in range(N+1)]
for g in range(N):
  for l in range(N-g):
    r = l + g
    n = r - l
    s = -1 if (N-n) % 2 == 0 else 1
    lv = dp[l+1][r] + s*A[l]
    rv = dp[l][r-1] + s*A[r]
    if s*lv > s*rv:
      dp[l][r] = lv
    else:
      dp[l][r] = rv

print(dp[0][N-1])
### BUGGY CODE ###

import sys
from itertools import accumulate
readline = sys.stdin.readline
MOD = 10**9+7

N, C = map(int, readline().split())
A = [0] + list(map(int, readline().split()))
B = [0] + list(map(int, readline().split()))

acsq = [list(accumulate([pow(i, j, MOD) for i in range(max(B)+1)])) for j in range(C+1)]
dp = [[0]*(C+1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(1, N+1):
    a, b = A[i], B[i]
    for c in range(C+1):
        res = 0
        for j in range(c+1):
            res = (res + (acsq[c-j][b] - acsq[c-j][a-1])*dp[i-1][j]) % MOD
        dp[i][c] = res
print(dp[N][C])

### FIXED CODE ###

from itertools import accumulate
N, C = map(int, input().split())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
mod = 10**9 + 7
X = [list(accumulate([pow(x, d, mod) for x in range(401)])) for d in range(C+1)]
dp = [[0]*(C+1) for _ in range(N+1)]
dp[0][0] = 1
for i in range(1, N+1):
    for d in range(C+1):
        dp[i][d] = sum([dp[i-1][d-k]*(X[k][B[i-1]] - X[k][A[i-1]-1]) for k in range(d+1)])%mod
        
print(dp[-1][-1])
### BUGGY CODE ###

import sys
input = sys.stdin.readline

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

b = []
li = [0] * 21
r = 0
ans = 0

for i in a:
    ai = bin(i)
    b.append(ai[2:])

for l in range(n):
    while True:
        if r >= n:
            ans += r - l
            break
        tmp = b[r]
        flag = True
        for i in range(len(tmp)):
            if li[i] == 1 and tmp[-i-1] == '1':
                flag = False
            if not flag:
                break
        if flag:
            for i in range(len(tmp)):
                li[i] += int(tmp[-i-1])
            r += 1
        else:
            for i in range(len(b[l])):
                li[i] -= int(b[l][-i-1])
            ans += r - l
            break

print(ans)


### FIXED CODE ###

import sys
input = sys.stdin.readline

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

b = []
li = [0] * 21
r = 0
ans = 0

for i in a:
    ai = bin(i)
    b.append(ai[2:])

for l in range(n):
    while True:
        if r >= n:
            ans += r - l
            break
        tmp = b[r]
        flag = True
        for i in range(len(tmp)):
            if li[i] == 1 and tmp[-i-1] == '1':
                flag = False
            if not flag:
                break
        if flag:
            for i in range(len(tmp)):
                li[i] += int(tmp[-i-1])
            r += 1
        else:
            for i in range(len(b[l])):
                li[i] -= int(b[l][-i-1])
            ans += r - l
            break

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

X = int(input())
C = 0
for i in range(-10000,10000):
	if C == 1:
		break
	for j in range(-10000,10000):
		if i**5-j**5 == X:
			print(i,j)
			C += 1
			break

### FIXED CODE ###

X = int(input())
C = 0
for i in range(-1000,1000):
	if C == 1:
		break
	for j in range(-1000,1000):
		if i**5-j**5 == X:
			print(i,j)
			C += 1
			break
### BUGGY CODE ###

import math
from decimal import *

n, w = map(int, input().split())
cost, weights = [],[]
for i in range(n):
    a,b = map(int, input().split())
    weights.append(a)
    cost.append(b)

def kp(pt, c, ans):
    if(pt==-1 or c==0):
        return ans
    else:
        var1, var2 = 0, 0
        var1= kp(pt-1, c, ans)
        if(c >= weights[pt]):
            var2 = cost[pt]+kp(pt-1, c-weights[pt], ans)
        ans = max(var1, var2)
    return ans
ans = kp(n-1, w, 0)
print(ans)

### FIXED CODE ###

import math

n, w = map(int, input().split())
weights = []
costs = []
for i in range(n):
    a,b = map(int, input().split())
    weights.append(a)
    costs.append(b)
dp = [[0 for i in range(w+1)] for i in range(n)]
for i in range(w+1):
    if(i>= weights[0]):
        dp[0][i] = costs[0]

for i in range(1, n):
    for j in range(w+1):
        if(j >= weights[i]):
            val = max(dp[i-1][j], costs[i]+dp[i-1][j-weights[i]])
            dp[i][j] = val
        else:
            dp[i][j] =max(dp[i][j], dp[i-1][j])
print(dp[-1][-1])
### BUGGY CODE ###

import numpy as np
S = input()
dp = np.zeros((len(S)+1,13)).astype('int')
dp[0][0] = 1
for i in range(len(S)):
    if S[i] == '?':
        c = -1
    else:
        c = int(S[i])
    for j in range(10):
        if c != -1 and c != j:
            continue
        for k in range(13):
            dp[i+1][(k*10+j)%13] += dp[i][k]
    for j in range(13):
        dp[i+1][j] %= int(10**9 + 7)
print(dp[len(S)][5])

### FIXED CODE ###

S = input()
mod = 10**9 + 7
dp = [[0 for i in range(13)] for j in range(len(S)+1)]
dp[0][0] = 1
for i in range(len(S)):
    if S[i] == '?':
        c = -1
    else:
        c = int(S[i])
    for j in range(10):
        if c != -1 and c != j:
            continue
        for k in range(13):
            dp[i+1][(k*10+j)%13] += dp[i][k]%mod
    for j in range(13):
        dp[i+1][j] %= 10**9 + 7
print(dp[len(S)][5]%mod)
### BUGGY CODE ###

S=input()
l=len(S)
t=0
p=0
for i in range(l-1,2,-1):
    p=0
    for j in range(i+1):
        p+=int(S[i-j])*(10**j)%2019

        if p%2019==0:

            t+=1

print(t)

### FIXED CODE ###

s = input()[::-1] # 入力文字列を逆順でsに格納

counts = [0] * 2019
counts[0] = 1

num, d = 0, 1

for char in s:
    num += int(char) * d
    num %= 2019
    d *= 10
    d%= 2019
    counts[num] += 1

ans = 0
for cnt in counts:
    ans += cnt * (cnt - 1) // 2

print(ans) # 答えの出力
### BUGGY CODE ###

import heapq

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

heapq.heapify(A)

for _ in range(M):
    b, c = map(int, input().split())
    for _ in range(b):
        smallest = heapq.heappop(A)
        if smallest >= c:
            heapq.heappush(A, smallest)
            break
        else:
            heapq.heappush(A, c)

print(sum(A))


### FIXED CODE ###

import sys
import heapq

N, M = map(int, input().split())
A = list(map(int, input().split()))
origin = sum(A)
heapq.heapify(A)

BC = [list(map(int, input().split())) for _ in range(M)]
BC = sorted(BC, reverse=True, key=lambda x: x[1])

count = 0

for i in range(M):
    b = BC[i][0]
    c = BC[i][1]
    for _ in range(b):
        smallest = heapq.heappop(A)
        tmp = c - smallest
        if tmp <= 0:
            heapq.heappush(A, smallest)
            break
        else:
            heapq.heappush(A, c)
            origin += tmp
            count += 1
            if count >= N:
                print(origin)
                sys.exit()

print(origin)
### BUGGY CODE ###

# D - Knapsack Queries on a tree

from sys import stdin
import bisect

readline = stdin.readline
bisect_right = bisect.bisect_right

n = int(readline())

v = [0] * n
w = [0] * n
for i in range(n):
    v[i], w[i] = map(int, readline().split())

max_l = 10 ** 5
INF = float('inf')

# dp table (for some amount)
dp = [None] * min(n, 2 ** 12)

# dp[vertex] = list of (weight, value)
dp[0] = [(0, 0), (w[0], v[0])]

for u in range(1, len(dp)):
    parent = (u - 1) // 2

    ls = dp[parent][:]
    ls.extend((ww + w[u], vv + v[u]) for ww, vv in dp[parent]
              if ww + w[u] <= max_l)
    ls.sort()

    # remove unnecessary entries
    tail = 0
    for i in range(1, len(ls)):
        if ls[tail][0] == ls[i][0]:
            ls[tail] = ls[i]
        elif ls[tail][1] < ls[i][1]:
            tail += 1
            ls[tail] = ls[i]
    del ls[tail + 1:]
    dp[u] = ls

def solve(u, l):
    if u < len(dp):
        x = bisect_right(dp[u], (l, INF)) - 1
        return dp[u][x][1]

    parent = (u - 1) // 2
    s = solve(parent, l)
    if l >= w[u]:
        s = max(s, v[u] + solve(parent, l - w[u]))
    return s

q = int(readline())
for i in range(q):
    u, l = map(int, readline().split())
    print(solve(u - 1, l))

### FIXED CODE ###

# D - Knapsack Queries on a tree

from sys import stdin
import bisect

readline = stdin.readline
bisect_right = bisect.bisect_right

n = int(readline())

v = [0] * n
w = [0] * n
for i in range(n):
    v[i], w[i] = map(int, readline().split())

max_l = 10 ** 5
vshift = 21
mask = (1 << vshift) - 1

# dp table (for some amount)
dp = [None] * min(n, 2 ** 12)

# dp[vertex] = list of (weight, value)
dp[0] = [(0 << vshift) + 0, (w[0] << vshift) + v[0]]

for u in range(1, len(dp)):
    parent = (u - 1) // 2

    ls = dp[parent][:]
    ls.extend(x + (w[u] << vshift) + v[u] for x in dp[parent]
              if (x >> vshift) + w[u] <= max_l)
    ls.sort()

    # remove unnecessary entries
    tail = 0
    for i in range(1, len(ls)):
        if (ls[tail] >> vshift) == (ls[i] >> vshift):
            ls[tail] = ls[i]
        elif (ls[tail] & mask) < (ls[i] & mask):
            tail += 1
            ls[tail] = ls[i]
    del ls[tail + 1:]
    dp[u] = ls

def solve(u, l):
    if u < len(dp):
        x = bisect_right(dp[u], (l << vshift) + mask) - 1
        return dp[u][x] & mask

    parent = (u - 1) // 2
    s = solve(parent, l)
    if l >= w[u]:
        s = max(s, v[u] + solve(parent, l - w[u]))
    return s

q = int(readline())
for i in range(q):
    u, l = map(int, readline().split())
    print(solve(u - 1, l))
### BUGGY CODE ###

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

import bisect

for i in range(M):
  b, c=map(int, input().split())
  t=min(b, bisect.bisect(a,c)) # c以下の要素数
  a[:t]=[c]*t
  a.sort()
print(sum(a))
  

### FIXED CODE ###

n, m =map(int,input().split())
A = list(map(int,input().split()))
BC = [tuple(map(int,input().split()))for i in range(m)]

A.sort()
# 書き換え得点がでかい順に並べる
BC.sort(key= lambda x : -x[1])

i = 0
f=0
for b, c in BC:
    for j in range(b):
      # 見てるとこがはみ出してないかつ、cより小さい
      if i+j<n and A[i+j]<c:
        A[i+j]=c
      else: # もう走査しても意味ないからぶれーく
        break
    i+=b
print(sum(A))
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
diff_dict = {}
total_sum = 0
for i in a:
    if i not in diff_dict:
        i_count = a.count(i)
        temp = (i_count)*(i_count-1)//2
        diff_dict[i] = temp-(i_count-1)*(i_count-2)//2
        total_sum += temp
for i in a:
    print(total_sum - diff_dict[i])

### FIXED CODE ###

import collections
n = int(input())
a = list(map(int, input().split()))
cnt = collections.Counter(a)
total = 0
for key in cnt.keys():
    total += cnt[key]*(cnt[key]-1) // 2
for i in a:
    ans = total
    ans -= cnt[i] * (cnt[i]-1) / 2
    ans += (cnt[i]-1) * (cnt[i]-2) / 2
    print(int(ans))
### BUGGY CODE ###

import sys
from collections import deque
h,w,k = map(int, sys.stdin.readline().rstrip("\n").split())
sx,sy,gx,gy = map(int, sys.stdin.readline().rstrip("\n").split())
sx,sy,gx,gy = sx-1,sy-1,gx-1,gy-1
labyrinth = [line.rstrip("\n") for line in sys.stdin]
INF = float('inf')
footprint = [ [[(INF,INF)]*4 for j in range(w)] for i in range(h) ]
m = [(-1, 0), (0, 1), (1, 0), (0, -1)]
q = deque()
for d in range(4):
    footprint[sx][sy][d] = (1,0)
    q.append((sx,sy,d,1,0))
while q:
    x, y, d, c, kk = q.popleft()
    for nd in range(4):
        if nd == d:
            mx,my = m[d]
            nkk = 1 if kk == k else kk + 1
            nc = c + 1 if kk == k else c
        else:
            mx,my = 0,0
            nkk = 0
            nc = c + 1
        nx = x + mx
        ny = y + my
        if 0 <= nx < h and 0 <= ny < w and labyrinth[nx][ny] == '.' and footprint[nx][ny][nd] > (nc, nkk):
            footprint[nx][ny][nd] = (nc, nkk)
            if nc == c:
                q.appendleft((nx, ny, nd, nc, nkk))
            else:
                q.append((nx, ny, nd, nc, nkk))
ans = INF
for d in range(4):
    ans = min(ans, footprint[gx][gy][d][0])
ans = -1 if ans == INF else ans
print(ans)

### FIXED CODE ###

import sys
from collections import deque
h,w,k = map(int, sys.stdin.readline().rstrip("\n").split())
sx,sy,gx,gy = map(int, sys.stdin.readline().rstrip("\n").split())
sx,sy,gx,gy = sx-1,sy-1,gx-1,gy-1
labyrinth = [line.rstrip("\n") for line in sys.stdin]
INF = float('inf')
footprint = [ [[(INF,INF)]*4 for j in range(w)] for i in range(h) ]
m = [(-1, 0), (0, 1), (1, 0), (0, -1)]
q = deque()
for d in range(4):
    footprint[sx][sy][d] = (1,0)
    q.append((sx,sy,d,1,0))
while q:
    x, y, d, c, kk = q.popleft()
    for nd in range(4):
        if nd == d:
            mx,my = m[d]
            nkk = 1 if kk == k else kk + 1
            nc = c + 1 if kk == k else c
        else:
            mx,my = 0,0
            nkk = 0
            nc = c + 1
        nx = x + mx
        ny = y + my
        if 0 <= nx < h and 0 <= ny < w and labyrinth[nx][ny] == '.' and footprint[nx][ny][nd] > (nc, nkk):
            footprint[nx][ny][nd] = (nc, nkk)
            if nc == c:
                q.appendleft((nx, ny, nd, nc, nkk))
            else:
                q.append((nx, ny, nd, nc, nkk))
ans = INF
for d in range(4):
    ans = min(ans, footprint[gx][gy][d][0])
ans = -1 if ans == INF else ans
print(ans)
### BUGGY CODE ###

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

dictS={}
dictS[0]=1

for n in range (1,N):
    if n<K:
        dictS[n]=A[n]*dictS[n-1]
    else:
        dictS[n]=A[n]*dictS[n-1]/A[n-K]


for i in range (K,N):
    if dictS[i]>dictS[i-1]:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

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

for i in range (K,N):
    if A[i]>A[i-K]:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import heapq
import sys
input = sys.stdin.readline
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def solve():
  n = int(input())
  query = []
  xquery = []
  yquery = []
  for i in range(n):
    x,y = (int(i) for i in input().split())
    xquery.append((x,y,i))
    yquery.append((y,x,i))
  xquery = sorted(xquery)
  yquery = sorted(yquery)
  for i in range(n-1):
    tx,ty,place = xquery[i]
    tx2,ty2,place2 = xquery[i+1]
    cost = min(abs(tx2-tx),abs(ty2-ty))
    heapq.heappush(query,(cost,place,place2))
    
    ty,tx,place = yquery[i]
    ty2,tx2,place2 = yquery[i+1]
    cost = min(abs(tx2-tx),abs(ty2-ty))
    heapq.heappush(query,(cost,place,place2))
  ct = 0
  uf = UnionFind(n)
  while len(query) > 0:
    cost,place1,place2 = heapq.heappop(query)
    if uf.same_check(place1,place2):
      continue
    else:
      uf.union(int(place1),int(place2))
      ct += cost
  print(ct)
      

solve()

### FIXED CODE ###

import heapq
import sys
input = sys.stdin.readline
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

def solve():
  n = int(input())
  query = []
  xquery = []
  yquery = []
  for i in range(n):
    x,y = (int(i) for i in input().split())
    xquery.append((x,y,i))
    yquery.append((y,x,i))
  xquery.sort()
  yquery.sort()
  for i in range(n-1):
    tx,ty,place = xquery[i]
    tx2,ty2,place2 = xquery[i+1]
    cost = min(abs(tx2-tx),abs(ty2-ty))
    if place > place2:
      place,place2 = place2,place
    heapq.heappush(query,(cost,place,place2))
    
    ty,tx,place = yquery[i]
    ty2,tx2,place2 = yquery[i+1]
    cost = min(abs(tx2-tx),abs(ty2-ty))
    if place > place2:
      place,place2 = place2,place
    heapq.heappush(query,(cost,place,place2))
  ct = 0
  uf = UnionFind(n)

  while len(query) > 0:
    cost,place1,place2 = heapq.heappop(query)
    if uf.same_check(place1,place2):
      continue
    else:
      uf.union(int(place1),int(place2))
      ct += cost
  print(ct)
      

solve()
### BUGGY CODE ###

import numpy as np
N, M = map(int, input().split())

key = np.zeros(N)
for i in range(0, M):
    L, R = map(int, input().split())
    for j in range(L, R+1):
        key[j-1]+=1

print(np.sum(key==M))

### FIXED CODE ###

#!/usr/bin/env python3

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

min_, max_ = 0, 10**5
for i in range(m):
    a, b = list(map(int, input().split()))

    min_ = max(min_, a)
    max_ = min(max_, b)

print(max(max_-min_+1, 0))
### BUGGY CODE ###

from functools import partial
from itertools import islice


def take(n, iterable):
    return list(islice(iterable, n))


def chunked(iterable, n):
    return iter(partial(take, n, iter(iterable)), [])


def distance(x, y):
    return abs(x[0] - y[0]) + abs(x[1] - y[1])


def solve(s):
    h, w, d, *aqlr = map(int, s.split())
    a, q, lr = aqlr[:h * w], aqlr[h * w], aqlr[h * w + 1:]
    n = h * w
    pos = [0] * (n + 1)
    for i, _a in enumerate(a):
        pos[_a] = (i // w, i % w)
    cost = [[0] * (n + 1) for _ in range(n + 1)]
    for i in range(d + 1, n + 1):
        dis = distance(pos[i - d], pos[i])
        for j in range(i):
            if (i - j) % d == 0:
                cost[i][j] = cost[i - d][j] + dis
    ans = []
    for l, r in chunked(lr, 2):
        ans.append("%d" % cost[r][l])
    return "\n".join(ans)


n, m, d = map(int, input().split())
l = '{} {} {}\n'.format(n, m, d) + '\n'.join([input() for _ in range(n)])
q, = map(int, input().split())
l += '\n{}\n'.format(q) + '\n'.join([input() for _ in range(q)])
print(solve(l))


### FIXED CODE ###

import numpy as np

h, w, d = map(int, input().strip().split(" "))
a = [[int(i) for i in input().strip().split(" ")] for _ in range(h)]
q = int(input().strip())
lr = [input().strip().split(" ") for _ in range(q)]
rs = [None for _ in range(h * w)]
cs = [None for _ in range(h * w)]
for _r, _a in enumerate(a):
    for _c, __a in enumerate(_a):
        rs[__a - 1] = _r
        cs[__a - 1] = _c
rs_d = [rs[i::d] for i in range(d)]
cs_d = [cs[i::d] for i in range(d)]
costs = [
    np.cumsum(
        np.abs(np.array(_rs[1:]) - np.array(_rs[:-1])) +
        np.abs(np.array(_cs[1:]) - np.array(_cs[:-1]))) for _rs, _cs in zip(rs_d, cs_d)
]
for l, r in lr:
    l = int(l) - 1
    r = int(r) - 1
    if l == r:
        print(0)
    elif l < d:
        print(costs[r % d][r // d - 1])
    else:
        print(costs[r % d][r // d - 1] - costs[l % d][l // d - 1])
### BUGGY CODE ###

n,q =map(int,input().split())
s = input()

d= {}

for k in range(n):
    d[k]=0

for j in range(n):
    if j ==n-1:
        tmp_str = s[j]
    else:
        tmp_str = s[j:]

    d[j] += tmp_str.count('AC')

# print(d)

ans = []
for _ in range(q):
    l,r = map(int,input().split())

    ans.append(d[l-1] -d[r-1])


for i in ans:
    print(i)

### FIXED CODE ###

n,q =map(int,input().split())
s = input()

d= {}

for k in range(n):
    d[k]=0

total = 0
for j in range(n-1):
    if s[j]=='A' and s[j+1]=='C':
        total+=1
        d[j+1]+=total
    else:
        d[j+1]=total
        
# print(d)

ans = []
for _ in range(q):
    l,r = map(int,input().split())

    ans.append(d[r-1] -d[l-1])


for i in ans:
    print(i)
### BUGGY CODE ###

n = int(input())
a = list(map(int, input().split()))
ans = 0
flag = 0
if n % 2 == 1:
  if a.count(0) != 1: flag = 1
  else:
    for i in range(n):
      if a[i] != 0 and a.count(a[i]) != 2:
        flag = 1
else:
  if a.count(0) != 0: flag = 1
  else:
    for i in range(n):
      if a.count(a[i]) != 2:
        flag = 1

if flag == 0:
  print(2 ** (n // 2))
else:
  print(0)

### FIXED CODE ###

import math
N = int(input())


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

A.sort()
ans = 0
flag = True
if N % 2 == 1:
  # 0,2,2,4,4,～みたいに並んでるはず
  if A[0] == 0:
    for i in range(1,N,2):
      if A[i] != i+1 or A[i+1] != i+1:
        flag = False
        break
  
    if flag: ans = 2**(N//2) % (10**9 + 7)

else:
  if A[0] != 0:
    for i in range(0,N,2):
      if A[i] != i+1 or A[i+1] != i+1:
        flag = False
        break
  
    if flag: ans = 2**(N//2) % (10**9 + 7)
    
    
    
print(ans)
### BUGGY CODE ###

n = int(input())
s = list(input() for _ in range(n))
ss = sorted(list(set(s)))
lens = len(ss)
l = [0]*len(ss)
for i in range(lens):
    l[i] =s.count(ss[i])
for i in range(lens):
    if l[i] == max(l):
        print(ss[i])

### FIXED CODE ###

N = int(input())

d = {}
for _ in range(N):
    S = input()
    if S in d:
        d[S] += 1
    else:
        d[S] = 1

m = max(d.values())
for s in sorted(k for k in d if d[k] == m):
    print(s)