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

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

mod = 10**9+7
N = 10**5+50
fac = [1]*(N+1)
finv = [1]*(N+1)
for i in range(N):
    fac[i+1] = fac[i] * (i+1) % mod
finv[-1] = pow(fac[-1], mod-2, mod)
for i in reversed(range(N)):
    finv[i] = finv[i+1] * (i+1) % mod

def cmb1(n, r, mod):
    if r <0 or r > n:
        return 0
    r = min(r, n-r)
    return fac[n] * finv[r] * finv[n-r] % mod

ans = 0
for i in range(n*m-1):
    xi, yi = divmod(i, m)
    for j in range(i, n*m):
        xj, yj = divmod(j, m)
        d = abs(xi-xj)+abs(yi-yj)
        ans += d
        #ans += d*cmb1(n*m-2, k-2, mod)
ans %= mod
ans *= cmb1(n*m-2, k-2, mod)
ans %= mod
print(ans)


### FIXED CODE ###

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

mod = 10**9+7
N = 2*10**5+50
fac = [1]*(N+1)
finv = [1]*(N+1)
for i in range(N):
    fac[i+1] = fac[i] * (i+1) % mod
finv[-1] = pow(fac[-1], mod-2, mod)
for i in reversed(range(N)):
    finv[i] = finv[i+1] * (i+1) % mod

def cmb1(n, r, mod):
    if r <0 or r > n:
        return 0
    r = min(r, n-r)
    return fac[n] * finv[r] * finv[n-r] % mod

ans = 0
for i in range(n*m):
    x, y = divmod(i, m)
    # x
    ans += x*m*cmb1(n*m-2, k-2, mod)*x
    ans -= (n-1-x)*m*cmb1(n*m-2, k-2, mod)*x
    # y
    ans += y*n*cmb1(n*m-2, k-2, mod)*y
    ans -= (m-1-y)*n*cmb1(n*m-2, k-2, mod)*y

ans %= mod
print(ans)
### BUGGY CODE ###

import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        T = X[n:n + K]
        l = abs(T[0]) + abs(T[0] - T[K-1])
        r = abs(T[K-1]) + abs(T[0] - T[K-1])
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()


### FIXED CODE ###

import sys
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

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


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, K = il()
    X = il()

    ret = MAX
    for n in range(N - K + 1):
        l = abs(X[n]) + abs(X[n]-X[n+K-1])
        r = abs(X[n+K-1]) + abs(X[n+K-1] - X[n])
        ret = min(ret, l, r)
    print(ret)


if __name__ == '__main__':
    main()
### BUGGY CODE ###

s = input()
t = input()

ls = len(s)
lt = len(t)

table = [['' for _ in range(lt + 1)] for __ in range(ls + 1)]

for ns in range(ls):
    for nt in range(lt):
        if s[ns] == t[nt]:
            table[ns + 1][nt + 1] = max(table[ns][nt] + s[ns], table[ns + 1][nt + 1], key=lambda x:len(x))
        table[ns + 1][nt + 1] = max(table[ns + 1][nt + 1], table[ns + 1][nt], table[ns][nt + 1] , key=lambda x:len(x))

print(table[-1][-1])

### FIXED CODE ###

s = input()
t = input()

ls = len(s)
lt = len(t)

dp = [[0 for _ in range(lt + 1)] for __ in range(ls + 1)]

for ns in range(ls):
    for nt in range(lt):
        if s[ns] == t[nt]:
            dp[ns + 1][nt + 1] = max(dp[ns][nt] + 1, dp[ns + 1][nt + 1])
        dp[ns + 1][nt + 1] = max(dp[ns + 1][nt + 1], dp[ns + 1][nt], dp[ns][nt + 1])

ans = ''
i, j = ls, lt
while i > 0 and j > 0:
    if dp[i][j] == dp[i - 1][j]:
        i -= 1
    elif dp[i][j] == dp[i][j - 1]:
        j -= 1
    else:
        ans = s[i - 1] + ans
        i -= 1
        j -= 1
print(ans)
### BUGGY CODE ###

import heapq
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for i in range(M)]
for b, c in BC:
    A.extend([c]*b)
_A = [-a for a in A]
heapq.heapify(_A)
ans = 0
for i in range(N):
    ans += -1 * heapq.heappop(_A)
print(ans)


### FIXED CODE ###

from collections import defaultdict as dd
N, M = map(int, input().split())
A = list(map(int, input().split()))
BC = [tuple(map(int, input().split())) for i in range(M)]

cards = dd(int)
for a in A:
    cards[a] += 1
for b, c in BC:
    cards[c] += b

ans = 0
for k, v in sorted(cards.items(), key=lambda x: -x[0]):
    if N > v:
        ans += k*v
    else:
        ans += k*N
        break
    N -= v
print(ans)
### BUGGY CODE ###

def main():
  import sys
  read = sys.stdin.buffer.read
  r = 998244353
  U, S, *A = map(int, read().split())
  import copy
  ans = 0
  f = [0]*3010
  for a in A:
      c = copy.deepcopy(f)
      f = [0]*a+c[:-a]
      f = [x+y for x,y in zip(f,c)]
      f[0] += 1
      f[a] += 1
      ans += f[S]
  ans %= r
  print(ans)
if __name__ == '__main__':
    main()

### FIXED CODE ###

def main():
  import sys
  read = sys.stdin.buffer.read
  r = 998244353
  U, S, *A = map(int, read().split())
  import copy
  ans = 0
  f = [0]*3010
  for a in A:
      c = copy.copy(f)
      f = [0]*a+c[:-a]
      f = [x+y for x,y in zip(f,c)]
      f[0] += 1
      f[a] += 1
      ans += f[S]
  ans %= r
  print(ans)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

#import numpy as np

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

for i in range(n):
    for j in range(n-1):
        A[i][j] -= 1

#print(A)

S = [0 for i in range(n)]
#F = [[0 for i in range(n)] for j in range(2*n*n)]

n1n = n*(n-1)
nm2 = n - 2
nm1 = n - 1
maxga = n
maxgan = n

d = 0
for ii in range(n*n*2):
    maxga = maxgan
    F = [0] * n
    co = 0
    i = 0
    ga = 0
    for jj in range(n*n*2):
        si = S[i]
        if F[i] == 0 and si <= nm2:
            aite = A[i][si]
            #print(S)
            #print("aite",aite)
            sai = S[aite]
            if sai <= nm2 and A[aite][sai] == i and F[aite] == 0:
                #print("g", i, aite)
                S[i] += 1
                S[aite] += 1
                F[i] = 1
                F[aite] = 1
                co = 0
                ga += 1
                if sai == nm2:
                    maxgan -= 1
                if si == nm2:
                    maxgan -= 1

        i = (i+1) % n
        co += 1
        #print(S,maxga//2)
        if co == nm1 or ga == maxga//2:
            break
    d += 1
    #print("d", d)
    if ga == 0:
        if sum(S) == n1n:
            print(d-1)
        else:
            print(-1)
        break



### FIXED CODE ###

import sys
input = sys.stdin.readline

n = int(input())
A = [list(map(int,input().split())) for i in range(n)]
for i in range(n):
    for j in range(n-1):
        A[i][j] -= 1

AA = [[j for j in i] for i in A]

ID = [[-1] * n for i in range(n)]
id_ = 0
for i in range(n):
    for j in range(i+1,n):
        ID[i][j] = id_
        ID[j][i] = id_
        id_ += 1

for i in range(n):
    for j in range(n-1):
        A[i][j] = ID[i][A[i][j]]

M = [[] for i in range(id_)]
IN = [0] * id_
for i in range(n):
    for j in range(n-2):
        M[A[i][j]].append(A[i][j+1])
        IN[A[i][j+1]] +=1
# print(A)
# print(M)

# 以下、トポロジカルソート

S = []
s = 0

for i in range(id_):
    if IN[i] == 0:
        S.append(i)

while s < len(S):
    for x in M[S[s]]:
        IN[x] -= 1
        # print(x,IN)
        if IN[x] == 0:
            S.append(x)
    s += 1

# print(S)

if len(S) < id_:
    print(-1)
else:
    T = [1] * id_
    for i in range(id_):
        for j in M[S[i]]:
            T[j] = max(T[j], T[S[i]] + 1)
    print(max(T))


# 以下、閉路検出(今は削除)

# V = [0] * id_
# S = [0]
# V[0] = 1

# import sys

# def visit(x):
#     if V[x] == 1:
#         print(-1)
#         sys.exit()
#     elif V[x] == 0:
#         V[x] = 1
#         for i in M[x]:
#             visit(i)
#         V[x] = 2

# for i in range(id_):
#     if V[i] == 0:
#         visit(i)
# print("heironasi")

# 閉路検出ここまで
### BUGGY CODE ###

import sys
read = sys.stdin.read
readlines = sys.stdin.readlines
from itertools import product
def main():
    n = int(input())
    s = input()

    r = [i for i, c in enumerate(s) if c == 'R']
    g = [i for i, c in enumerate(s) if c == 'G']
    b = [i for i, c in enumerate(s) if c == 'B']
    com = product(r, g, b)
    r = 0
    for c in com:
        r += sum(c) * 2 != (min(c) + max(c)) * 3
    print(r)

if __name__ == '__main__':
    main()


### FIXED CODE ###

def main():
    n = int(input())
    s = list(input())
    sr = [0] * n
    sg = [0] * n
    sb = [0] * n
    for i1 in range(n):
        ts = s[i1]
        if ts == 'R':
            sr[i1] = 1
        elif ts == 'G':
            sg[i1] = 1
        else:
            sb[i1] = 1
    if n <= 3:
        print(0)
        exit()
    r = 0
    for ia in range(0,n-3):
        ta = s[ia]
        if ta == 'R':
            r += sum(sg[ia+1:]) * sum(sb[ia+1:])
        elif ta == 'G':
            r += sum(sr[ia+1:]) * sum(sb[ia+1:])
        else:
            r += sum(sg[ia + 1:]) * sum(sr[ia + 1:])
        for ib in range((n - ia)//2+1):
            if ia + ib * 2 < n:
                if ta != s[ia + ib] and ta != s[ia + ib + ib] and s[ia+ib]!= s[ia + ib + ib]:
                    r -= 1
    print(r)

if __name__ == '__main__':
    main()
### BUGGY CODE ###

import sys
sys.setrecursionlimit(100000)
 
N, K = list(map(int, input().split()))
ab = []
for i in range(N-1):
    ab.append(list(map(int, input().split())))
 
ki = []
 
for i in range(N):
    ki.append(dict())
    ki[-1]["eda"] = []
 
for i in range(N-1):
    ki[ab[i][0] - 1]["eda"].append([ab[i][0] - 1, ab[i][1] - 1])
    ki[ab[i][1] - 1]["eda"].append([ab[i][1] - 1, ab[i][0] - 1])
 
def P(m ,n):
    if m <= 0 or n <= 0:
        return 0
    else:
        num = 1
        for i in range(n):
            num *= m-i
        return num
 
def saiki(i, base, ki):
    num = 1
    eda_num = len(ki[i]["eda"])-1
    if eda_num == 0:
        return num
    num *= P(K-2, eda_num)
 
    for edge in ki[i]["eda"]:
        if edge[1] != base:
            num *= saiki(edge[1], i, ki)
            num = num % 1000000007
            #print(num)
 
    return num
 
total_num = K
if len(ki[0]["eda"]) != 0:
    total_num *= P(K-1, len(ki[0]["eda"]))
    for edge in ki[0]["eda"]:
        total_num *= saiki(edge[1], 0, ki)
        total_num = total_num % 1000000007
        if total_num == 0:
            break
 
print(total_num)

### FIXED CODE ###

import sys
sys.setrecursionlimit(10**7)
INF=10**9+7
def LI(): return list(map(int, sys.stdin.readline().split()))
 
def main():
    n, k = LI()
    graph=[[] for i in range(n)]
    for i in range(n-1):
        a,b=LI()
        graph[a-1].append(b-1)
        graph[b-1].append(a-1)
    ans=1
    check=[False]*n
    stack=[0]
    ans=1
    while len(stack)>0:
        x=stack.pop()
        check[x]=True
        count=0
        for i in graph[x]:
            if not check[i]:
                count+=1
                stack.append(i)
        if x==0:
            temp=1
            for i in range(count):
                temp=temp*(k-1-i)%INF
            ans=ans*temp%INF
        else:
            temp=1
            for i in range(count):
                temp=temp*(k-2-i)%INF
            ans=ans*temp%INF
    print((ans*k)%INF)
 
 
if __name__ == "__main__":
    main()
### BUGGY CODE ###

import math as mt 
import collections
N = int(raw_input())
MAXN = N+1
  
# stores smallest prime factor for 
# every number 
spf = [0 for i in range(MAXN)] 
  
# Calculating SPF (Smallest Prime Factor)  
# for every number till MAXN. 
# Time Complexity : O(nloglogn) 
def sieve(): 
    spf[1] = 1
    for i in range(2, MAXN): 
          
        # marking smallest prime factor  
        # for every number to be itself. 
        spf[i] = i 
  
    # separately marking spf for  
    # every even number as 2 
    for i in range(4, MAXN, 2): 
        spf[i] = 2
  
    for i in range(3, int(mt.ceil(mt.sqrt(MAXN)))): 
          
        # checking if i is prime 
        if (spf[i] == i): 
              
            # marking SPF for all numbers 
            # divisible by i 
            for j in range(i * i, MAXN, i):  
                  
                # marking spf[j] if it is  
                # not previously marked 
                if (spf[j] == j): 
                    spf[j] = i 
# A O(log n) function returning prime  
# factorization by dividing by smallest  
# prime factor at every step 
def getFac(x): 
    ret = list() 
    while (x != 1): 
        ret.append(spf[x]) 
        x = x // spf[x] 
  
    return ret 


sieve() 
ans = 0
for i in range(1,N+1):
    fac = getFac(i)
    if len(fac) == 1:
        ans += i*2
    else:
        res = 1
        dic = collections.Counter(fac)
        for v in dic.values():
            res*=(v+1)
        ans+= res*i
print(ans)

### FIXED CODE ###

FAST_IO = 1
if FAST_IO:
    import io, sys, atexit
    rr = iter(sys.stdin.read().splitlines()).next
    sys.stdout = _OUTPUT_BUFFER = io.BytesIO()
    @atexit.register
    def write():
        sys.__stdout__.write(_OUTPUT_BUFFER.getvalue())
else:
    rr = raw_input
rri = lambda: int(rr())
rrm = lambda: map(int, rr().split())
N = rri()
ans= 0
for i in range(1, N+1):
    first = i
    last = i*(N//i)
    ans += (last+first)*(N//i)/2
print(ans)


#26879
### BUGGY CODE ###

N, S = map(int, input().split())
data = list(map(int, input().split()))

MOD = 998244353
# dp = [[0]*(3000+1) for _ in range(N)]
# ans = 0

# for i, num in enumerate(data):
#     dp[i][num] = 1
#     for j in range(i):
#         for k in range(S-num, 0, -1):
#             if dp[j][k] >= 1:
#                 dp[j][k+num] += dp[j][k]
#             if k == num:
#                 dp[j][num] += 1
#     for j in range(i+1):
#         ans += dp[j][S]
#     ans %= MOD

# print(ans)

ans = 0
dp = [[] for _ in range(3001)]
dp[0] = [0]
for i, num in enumerate(data):
    for j in range(S-num, -1, -1):
        for item in dp[j]:
            dp[j+num] += [item+(1<<i)]

for item in dp[S]:
    for i in range(N+1):
        if item < (1<<i):
            a = N - i + 1
            break
    for i in range(N):
        if item & (1<<i) != 0:
            b = i + 1
            break
    ans = (a*b+ans)%MOD

print(ans)


### FIXED CODE ###

P = 998244353

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

dp0 = [[0]*(S+1) for _ in range(N)]
dp1 = [[0]*(S+1) for _ in range(N)]
dp2 = [[0]*(S+1) for _ in range(N)]

dp0[0][0] = 1
dp1[0][0] = 1
if A[0] <= S:
    dp1[0][A[0]] = 1

for i in range(1, N):
    dp0[i][0] = 1
    dp1[i][0] = dp1[i-1][0] + 1
    dp2[i][S] = dp2[i-1][S] + dp1[i-1][S]
    if A[i] == S:
        dp2[i][S] += dp1[i-1][0] + 1
        dp1[i][S] += dp1[i-1][0] + 1
    elif A[i] < S:
        dp1[i][A[i]] += 1 + dp1[i-1][0]
    for j in range(1, S+1):
        dp1[i][j] += dp1[i-1][j]
        if j + A[i] < S:
            dp1[i][j+A[i]] += dp1[i-1][j]
        elif j + A[i] == S:
            dp2[i][S] += dp1[i-1][j]
            dp1[i][S] += dp1[i-1][j]
        dp0[i][j] %= P
        dp1[i][j] %= P
        dp2[i][j] %= P

print(dp2[N-1][S])
### BUGGY CODE ###

import numpy as np
N = int(input())
A = list(map(int,input().split()))
aa = np.array(A)
aa = np.sort(aa)
for i in range(len(aa)):
  judge = True
  j = i+1
  div = []
  while True:
    if len(aa) <= j:
      break
    if aa[j]%aa[i] == 0:
      if aa[j] == aa[i]:
        div.append(i)
      div.append(j)
    j += 1
  aa = np.delete(aa,div,0)

print(len(aa))

### FIXED CODE ###

import numpy as np
N = int(input())
A = list(map(int,input().split()))
aa = sorted(A)
ans = [0 for i in range(aa[-1]+1)]
for i in aa:
  ans[i] += 1
  if ans[i] == 1:
    for j in range(i*2,aa[-1]+1,i):
      ans[j] += 2
print(ans.count(1))
### BUGGY CODE ###

#!/usr/bin/python3
# -*- coding: utf-8 -*-
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

n = int(input())
a_list = list(map(int, input().split()))
flag1 = 1
ans2 = a_list[n - 1]
for i in range(n - 1):
    if a_list[i] == 1:
        continue
    if ans2 != 1:
        ans2 = gcd(ans2,a_list[i])
    if flag1 == 1:
        for j in range(i + 1, n):
            if gcd(a_list[i], a_list[j]) != 1:
                flag1 = 0
                break

if flag1 == 1:
    print("pairwise coprime")
elif ans2 == 1:
    print("setwise coprime")
else:
    print("not coprime")

### FIXED CODE ###

#!/usr/bin/python3
# -*- coding: utf-8 -*-
def gcd(a,b):
    if b == 0:
        return a
    else:
        return gcd(b,a%b)

n = int(input())
a_list = list(map(int, input().split()))
a_list.sort()
ans2 = a_list[0]
for i in range(1, n):
    ans2 = gcd(ans2, a_list[i])
    if ans2 == 1:
        break

if ans2 != 1:
    print("not coprime")
else:
    flag = 1
    max_a = a_list[n - 1] + 1
    num_flag_list = [True] * max_a
    d_list = list(range(0, max_a))
    d_list[0] = 1
    num_flag_list[0] = num_flag_list[1] = False
    for i in range(2, int(max_a**0.5) + 1):
        if num_flag_list[i]:
            for j in range(i**2, max_a, i):
                if num_flag_list[j] == True:
                    num_flag_list[j] = False
                    d_list[j] = i
    p_set = set()
    for a in a_list:
        if a == 1:
            continue
        temp_p_set = set()
        while True:
            p = d_list[a]
            if p not in temp_p_set:
                if p in p_set:
                    flag = 0
                    break
            temp_p_set.add(p)
            p_set.add(p)
            a = a // p
            if a == 1:
                break
        if flag == 0:
            break
        
    if flag == 1:
        print("pairwise coprime")
    else:
        print("setwise coprime")
### BUGGY CODE ###

# D
from collections import deque
from bisect import bisect_left as bl

f=lambda:map(int,input().split())
N,M=f()
D=[[] for _ in [0]*(N+1)]
F=[0]*(N+1)
F[0]=1
for _ in range(M):
    a,b=f()
    if not b in D[a]:
        D[a].append(b)
    if not a in D[b]:
        D[b].append(a)

q=deque()
res=0
while bl(F,1):
    index=bl(F,1)-1
    q.appendleft(index)
    tmp=0
    while q:
        k=q.pop()
        if F[k]!=1:
            tmp+=1
            q.extend(D[k])
            D[k]=0
            F[k]=1
    res=max(res,tmp)

print(res)

### FIXED CODE ###

# D
from collections import deque
from bisect import bisect_left as bl

f=lambda:map(int,input().split())
N,M=f()
D=[[] for _ in [0]*(N+1)]
F=[0]*(N+1)
F[0]=1
for _ in range(M):
    a,b=f()
    D[a].append(b)
    D[b].append(a)

q=deque()
res=0


while bl(F,1):
    index=bl(F,1)-1
    q.append(index)
    tmp=0
    while q:
        k=q.pop()
        if F[k]!=1:
            tmp+=1
            F[k]=1
            for d in list(set(D[k])):
                if F[d]!=1:
                    q.append(d)
            D[k]=0
    res=max(res,tmp)

print(res)
### BUGGY CODE ###

N = int(input())
a = [int(input()) for _ in range(N)]

op_cnt = len(a)

ans = 0
for i in range(2 ** op_cnt):
    op = [0] * op_cnt  
    for j in range(op_cnt):
        if ((i >> j) & 1):
            op[op_cnt - 1 - j] = a[j]
    temp = sum(op)
    if sum(op)%10 == 0:
        temp = 0
    if temp > ans:
        ans = temp

print(ans)

### FIXED CODE ###

N = int(input())
s1 = []
s2 = []
 
for i in range(N):
    a = int(input())
    if a % 10 != 0:
        s1.append(a)
    else:
        s2.append(a)

if len(s1) == 0:
    print(0)
else:
    if sum(s1) % 10 == 0:
        print(sum(s1)-min(s1)+sum(s2))
    else:
        print(sum(s1)+sum(s2))
### BUGGY CODE ###

import sys,collections
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
 
H,W,D = Is()
d = dict()
for i in range(H):
    s = Is()
    for j,e in enumerate(s):
        d[e] = (i,j)
Q = I()
for q in range(Q):
    l,r = Is()
    maho = 0
    for k in range(l,r,D):
        maho += abs(d[k+D][0] - d[k][0]) + abs(d[k+D][1] - d[k][1])
    print(maho)

### FIXED CODE ###

import sys
def Is(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())

H,W,D = Is()
d = dict()
for i in range(H):
    s = Is()
    for j,e in enumerate(s):
        d[e] = (i,j)
m = [0]*(H*W+1)
for i in range(D+1,H*W+1):
    m[i] = m[i-D] + abs(d[i-D][0] - d[i][0]) + abs(d[i-D][1] - d[i][1])

Q = I()
for q in range(Q):
    l,r = Is()
    print(m[r]-m[l])
### BUGGY CODE ###

from sys import stdin

n = int(stdin.readline().rstrip())
a = [int(x) for x in stdin.readline().rstrip().split()]

a_n = []
for i in range(1, n + 1):
    a_i = a.index(i) + 1
    a_n.append(a_i)

print(*a_n)

### FIXED CODE ###

from sys import stdin

n = int(stdin.readline().rstrip())
a = {i + 1: int(x) for i, x in enumerate(stdin.readline().rstrip().split())}

a_sorted = sorted(a.items(), key=lambda x: x[1])

final = []
for i in range(n):
    final.append(a_sorted[i][0])

print(*final)
### BUGGY CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]
max_val = max(A)
for i, num in enumerate(A):
  if num < max_val:
    print(max_val)
  else:
    print(max(A[:i] + A[i+1:]))

### FIXED CODE ###

N = int(input())
A = [int(input()) for _ in range(N)]

max_val = max(A)
count = A.count(max_val)

subset = list(set(A))
if len(subset) > 1:
  subset.remove(max_val)
  max_val_2 = max(subset)

for i, num in enumerate(A):
  if num < max_val:
    print(max_val)
  else:
    if count > 1:
      print(max_val)
    else:
      print(max_val_2)
### BUGGY CODE ###

k = int(input())
s = input()
n = len(s)
mod = 10 ** 9 + 7

f = [1 for _ in range(n+k+1)]
f_inv = [1 for _ in range(n+k+1)]
for i in range(n+k):
    f[i+1] = f[i] * (i+1) % mod
    f_inv[i+1] = pow(f[i+1], mod-2, mod)

def comb(n, k):
    return f[n] * f_inv[k] * f_inv[n-k] % mod

ans = 0
for i in range(k+1):
    x = pow(25, i, mod) * pow(26, k-i, mod) * comb(n-1+i, n-1)
    ans += x
    ans %= mod

print(ans)

### FIXED CODE ###

k = int(input())
s = input()
n = len(s)
mod = 10 ** 9 + 7

f = [1, 1]
f_inv = [1, 1]
inv = [0, 1]
 
for i in range(2, n+k+1):
    f.append((f[-1] * i ) % mod)
    inv.append((-inv[mod % i] * (mod // i) ) % mod)
    f_inv.append((f_inv[-1] * inv[-1]) % mod)

def comb(n, k):
    return f[n] * f_inv[k] * f_inv[n-k] % mod

ans = 0
for i in range(k+1):
    x = pow(25, i, mod) * pow(26, k-i, mod) * comb(n-1+i, n-1)
    ans += x
    ans %= mod

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

import math
n,k = map(int,input().split())
a = sorted(list(map(int,input().split())))
f = sorted(list(map(int,input().split())),reverse=True)
check = []
for i in range(n):
    check.append((a[i]*f[i],a[i],f[i]))

def search(x):
    count = 0

    for i,j,k in check:
        if i > x:
            y = math.floor(x/k)
            count += j-y
    return count

left = -1
right = 10**13
while right > left+1:
    mid = (right+left)//2
    if search(mid) > k:
        left = mid
    else:
        right = mid
print(right)

### FIXED CODE ###

def main():
    import math
    import sys
    input = sys.stdin.readline

    n,k = map(int,input().split())
    a = sorted(list(map(int,input().split())))
    f = sorted(list(map(int,input().split())),reverse=True)
    check = []
    for i in range(n):
        check.append((a[i]*f[i],a[i],f[i]))

    def search(x):
        count = 0

        for i,j,k in check:
            if i > x:
                y = math.floor(x/k)
                count += j-y
        return count

    left = -1
    right = 10**12+2
    while right > left+1:
        mid = (right+left)//2
        if search(mid) > k:
            left = mid
        else:
            right = mid
    print(right)
if __name__ == "__main__":
    main()