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

n = int(input())
a_list = list(map(int,input().split()))
c_list = a_list.copy()
for i in range(n):
  a_list[i] -= i + 1
count = 0
for i in range(n-1):
  target = a_list[i+1:]
  count += target.count(-(c_list[i] + i + 1))
  
print(count)
  
  

### FIXED CODE ###

n = int(input())
a_list = list(map(int,input().split()))
a_plus = {}
a_minus = {}
for i in range(1,n):
  a_minus[i] = 0
  
for i in range(n):
  minus = (i+1) - a_list[i]
  if 2 <= minus:
    a_minus[minus] += 1
  
count = 0

for j in range(n):
  plus = (j+1) + a_list[j]
  if plus <= n - 1:
    count += a_minus[plus]

    
print(count)
### BUGGY CODE ###

import sys
sys.setrecursionlimit(200000)

N,M = map(int,input().split())
edges = [[]for _ in range(N+1)]
for _ in range(M):
    A,B = map(int,input().split())
    edges[A].append(B)
    edges[B].append(A)

def judgeBiGragh(edges):
    n = len(edges)
    colors = [-1]*(n+1)
    def paint(v,c):
        if colors[v] == -1:
            colors[v] = c
            nxC = (c+1)%2
            for nxV in edges[v]:
                result = paint(nxV,nxC)
                if result == False:
                    return result
        else:
            if colors[v] != c:
                return False
        return True
    return paint(1,0),colors.count(1)

def choose(n,k):
    import math
    return math.factorial(n)//(math.factorial(n-k)*math.factorial(k))

res1,res2 = judgeBiGragh(edges)
if res1:
    print(res2*(N-res2) - M)
else:
    print(choose(N,2)-M)


### FIXED CODE ###

"""
グラフが二部グラフかどうかを判定する。
二部グラフであれば、AグループのノードとBグループのノード同士をそれぞれ結ぶことができる。
二部グラフでない場合、すべてのノード同士を結ぶことができる。
"""
import math
from collections import Counter
import sys
sys.setrecursionlimit(200000)
def choose(n):
    return math.factorial(n)//(math.factorial(n-2)*math.factorial(2))
N,M = map(int,input().split())
edges = [[]for _ in range(N+1)]
for _ in range(M):
    A,B = map(int,input().split())
    edges[A].append(B)
    edges[B].append(A)

colors = [-1]*(N+1)
def f(n,p):
    c = colors[n]
    for v in edges[n]:
        if v != p:
            if colors[v] == -1:
                colors[v] = 1 if c==0 else 0
                f(v,n)
            elif colors[v] == c:
                print(choose(N)-M)
                exit()
colors[1]=0
f(1,0)
count1 = colors.count(0)
count2 = colors.count(1)
print(count1*count2-M)
### BUGGY CODE ###

import math
import collections

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
c = list(sorted(c.items()))
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

if len(c) == 1:
    for i in range(n-1):
        print(combinations_count(n-1, 2), end=' ')
    print(combinations_count(n-1, 2))
    exit()

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c:
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])

### FIXED CODE ###

import collections

def combinations_count(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

n = int(input())
li = list(map(int,input().split()))
c = collections.Counter(li)
ans = [0]*n
x = 0 #全てのボールから、できる組合せの総数

if len(c) == 1:
    ans = [combinations_count(n-1, 2)]*n
    print(*ans)
    exit()

for i in c.items():
    a = i[0]
    b = i[1]
    if b > 2:
        x += combinations_count(b, 2)
    elif b == 2:
        x += 1

for i in c.items():
    a = i[0]
    b = i[1]
    if b > 2:
        y = x - combinations_count(b, 2) + combinations_count(b-1, 2)
    elif b == 2:
        y = x - 1
    else:
        y = x
    ans[a-1] = y

for i in li:
    print(ans[i-1])
### BUGGY CODE ###

n_dice, n_choice = map(int, input().split())
dice_list = tuple(map(int, input().split()))
sumofarea_list = [sum(dice_list[i:i+n_choice]) for i in range(n_dice - n_choice + 1)]
print(max(sumofarea_list)/2 + n_choice / 2)

### FIXED CODE ###

import itertools

n, k = map(int, input().split())
dices = map(int, input().split())
dices_EV = list(map(lambda x:(x+1)/2, dices))
dices_EV_accumulate = list(itertools.accumulate(dices_EV))
dices_EV_sums = [dices_EV_accumulate[i+k] - dices_EV_accumulate[i] for i in range(n-k)]
dices_EV_sums.append(dices_EV_accumulate[k-1])
print(max(dices_EV_sums))
### BUGGY CODE ###


N = int(input())
S = []

ans = 0

for i in range(N):
    S.append(sorted(list(input())))

for i in range(N-1):
    for j in range(i+1,N):
        if S[i] == S[j]:
            ans += 1

print(ans)

### FIXED CODE ###


N = int(input())
anagram_dict = {}
ans = 0

for i in range(N):
    s= ''.join(sorted(list(input())))
    if s in anagram_dict:
        ans += anagram_dict[s]
        anagram_dict[s] += 1
    else:
        anagram_dict[s]=1

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

def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = [e+i+1 for i,e in enumerate(a)]
    c = [i+1-e for i,e in enumerate(a) if i+1-e > 1]
    ans = 0
    for i in c:
        ans += b.count(i)
    print(ans)
if __name__ == '__main__':
    main()

### FIXED CODE ###

from collections import Counter
def main():
    n = int(input())
    a = list(map(int, input().split()))
    b = Counter([e+i+1 for i,e in enumerate(a)])
    c = Counter([i+1-e for i,e in enumerate(a) if i+1-e > 1])
    ans = 0
    for i in c:
        ans += b[i] * c[i]
    print(ans)
if __name__ == '__main__':
    main()
### BUGGY CODE ###

n,k,q = map(int,input().split())
a=[]
for i in range(q):
    a.append(int(input()))

point=[k]*n

for i in a:
    for j in range(1,n+1):
        if i!=j:
            point[j-1]-=1

for i in point:
    if i > 0:
        print('Yes')
    else:
        print('No')

### FIXED CODE ###

from collections import Counter
n,k,q = map(int,input().split())
a=[]
for i in range(q):
    a.append(int(input()))
point=[k-q]*n
r=Counter(a)
for i,j in r.items():
    point[i-1] += j
for i in point:
    if i>0:
        print('Yes')
    else:
        print('No')
### BUGGY CODE ###

import heapq
n=int(input())
x=[]
y=[]
for i in range(n):
  xx,yy=map(int,input().split())
  x.append([xx,i])
  y.append([yy,i])
x.sort()
y.sort()
px=[]
py=[]
for i in range(n-1):
  px.append([x[i+1][0]-x[i][0],x[i][1],x[i+1][1]])
  py.append([y[i+1][0]-y[i][0],y[i][1],y[i+1][1]])
p=px+py
x=[]
y=[]
px=[]
py=[]
heapq.heapify(p)
r=[i for i in range(n)]
rank=[1 for i in range(n)]
def root(x):
  if x==r[x]:
    return x
  else:
    r[x]=root(r[x])
    return r[x]
def union(x,y):
  rx=root(x)
  ry=root(y)
  if rank[rx]>rank[ry]:
    r[rx]=ry
    rank[rx]+=rank[ry]
  else:
    r[ry]=rx
    rank[ry]+=rank[rx]
ans=0
while p:
  path=heapq.heappop(p)
  if root(path[1])==root(path[2]):
    continue
  union(path[1],path[2])
  ans+=path[0]
print(ans)

### FIXED CODE ###

n=int(input())
x=[]
y=[]
for i in range(n):
  xx,yy=map(int,input().split())
  x.append([xx,i])
  y.append([yy,i])
x.sort()
y.sort()
px=[]
py=[]
for i in range(n-1):
  px.append([x[i+1][0]-x[i][0],x[i][1],x[i+1][1]])
  py.append([y[i+1][0]-y[i][0],y[i][1],y[i+1][1]])
p=px+py
x=[]
y=[]
px=[]
py=[]
p.sort()
r=[i for i in range(n)]
rank=[1 for i in range(n)]
def root(x):
  if x==r[x]:
    return x
  else:
    r[x]=root(r[x])
    return r[x]
def union(path):
  rx=root(path[1])
  ry=root(path[2])
  if rx==ry:
    return 0
  else:
    if rank[rx]>rank[ry]:
      r[rx]=ry
      rank[rx]+=rank[ry]
    else:
      r[ry]=rx
      rank[ry]+=rank[rx]
    return path[0]
ans=0
for i in range(len(p)):
  ans+=union(p[i])
print(ans)
### BUGGY CODE ###

N = int(input())
l = list(map(int, input().split()))
order = []
for i in range(N):
  order.append((l.index(i + 1))+1)
print(*order)

### FIXED CODE ###

N = int(input())
l = list(map(int, input().split()))
print(*list(map(lambda x:x+1, sorted(range(len(l)), key=lambda k: l[k]))))
### BUGGY CODE ###

n = int(input())
s = input()

ans = 0

for i in range(1000):
    # 確認するpinの数字の組み合わせと桁の初期化
    pin = f'{i:0>3}'
    current_digit = 0

    for num in s:
        # 見つけると桁を右に一つずらす
        if num == pin[current_digit]:
            current_digit += 1
            # indexが3(桁が3→2→1→0)になったときはそのPINは作れる
            if current_digit == 3:
                ans += 1
                break

print(ans)

### FIXED CODE ###

n = int(input())
s = input()

ans = 0

for i in range(1000):
    # 確認するpinの数字の組み合わせと桁の初期化
    current_digit = 0
    pin = f'{i:0>3}'
    # pin = list(str(i).zfill(3))
    k = pin[current_digit]

    for num in s:
        # 見つけると桁を右に一つずらす
        if num == k:
            current_digit += 1
            # indexが3(桁が3→2→1→0)になったときはそのPINは作れる
            if current_digit <= 2:
                k = pin[current_digit]

            elif current_digit == 3:
                ans += 1
                break

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

import bisect
N,M = map(int,input().split())
A = list(map(int,input().split()))
for i in range(M):
    A.sort()
    B,C = map(int,input().split())
    D = bisect.bisect_left(A,C)
    if D <= B:
        for j in range(D):
            A[j] = C
    else:
        for j in range(B):
            A[j] = C
print(sum(A))

### FIXED CODE ###

N, M = map(int, input().split())
A = list(map(int, input().split()))
B = [(A[i],1) for i in range(N)]
for j in range(M):
    a, b = map(int, input().split())
    B.append((b,a))
B.sort()
B.reverse()
ans, cnt = 0, N
for (c, d) in B:
    use = min(d,cnt)
    ans += use*c
    cnt -= use
print(ans)
### BUGGY CODE ###

import numpy as np
import math
N,M = map(int,input().split())
ls = []
for n in range(N):
  ls.append(list(map(int,input().split())))
ls = np.array(ls)
idx = np.argsort(ls[:,1])[::-1]
#print (ls[idx])
days = list(range(1,M+1))
days = {k: 0 for k in days}
#print (days)
sum = 0
skip_d = -1
for work in ls[idx]:
  d = work[0]
  if d==skip_d:
    pass
  else:
    while(d<=M):
      if d in days:
        sum+=work[1]
        days.pop(d)
        d=M+1
        skip_d = -1
      else:
        d+=1
        skip_d = work[0]
print (sum)
  
    


### FIXED CODE ###

import heapq
N,M = map(int,input().split())
ls = []
h = []
ans = 0
for n in range(N):
    ls.append(list(map(int,input().split())))
ls = sorted(ls,key=lambda ls:ls[0],reverse=False)
n = 0
for m in range(1,M+1):
    #print (m)
    while(n<N and ls[n][0]<=m ):
        heapq.heappush(h, -ls[n][1])
        n+=1
    if len(h)>0:
        ans -= heapq.heappop(h)
        # print (m,ans)
print (ans)
### BUGGY CODE ###

A,B = input().split()
A,B = int(A),int(B)

lcm = 1
for i in range(1,max([A,B])+1):
    if(A%i==0 and B%i==0):
        lcm = i

d = 2
cms = [1]

while(lcm>1):
    if(lcm%d==0):
        lcm /= d
        if(d not in cms):
            cms.append(d)
    else:
        d += 1
print(len(cms))

### FIXED CODE ###

import collections
A,B = input().split()
A,B = int(A),int(B)
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a

A = collections.Counter(prime_factorize(A)).keys()
B = collections.Counter(prime_factorize(B)).keys()


cms = []
for a in A:
    for b in B:
        if(a==b):
            cms.append(a)
print(len(cms)+1)
### BUGGY CODE ###

N = int(input())
P = list(map(int, input().split()))
ans = 0
for i in range(N):
    partP = P[:i+1]
    cnt = 1
    partP.sort()
    for p in partP:
        if cnt == 0:
            break
        elif P[i] <= p:
            continue
        else:
            cnt *= 0
    if cnt == 1:
        ans += 1

print(ans)

### FIXED CODE ###

N = int(input())
P = list(map(int, input().split()))
ans =1 
newP = P[0]
for i in range(1, N):
    newP = (min(newP, P[i]))
    if newP == P[i]:
        ans += 1

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

while True:
    n = int(input())
    if n == 0: break
    w, h = map(int, input().split())
    trees = []
    for i in range(n):
        trees.append(tuple(map(int, input().split())))
    s, t = map(int, input().split())

    max_num = 0
    # loops in upper left coordinate
    for i in range(w - s + 1):
        for j in range(h - t + 1):
            num = 0
            for k in range(s):
                for l in range(t):
                    if (i+k+1, j+l+1) in trees:
                        num += 1
                        if num > max_num:
                            max_num = num
    print(max_num)

### FIXED CODE ###

while True:
    n = int(input())
    if n == 0: break
    w, h = map(int, input().split())
    trees = [[0 for i in range(h+1)] for j in range(w+1)]
    dis = [[0 for i in range(h+1)] for j in range(w+1)]
    for i in range(n):
        x, y = map(int, input().split())
        trees[x][y] = 1

    for i in range(1, w+1):
        for j in range(1, h+1):
            dis[i][j] = trees[i][j] + dis[i-1][j] \
                    + dis[i][j-1] - dis[i-1][j-1]

    s, t = map(int, input().split())

    ans = 0
    for i in range(1, w - s + 2):
        for j in range(1, h - t + 2):
            num = dis[i+s-1][j+t-1] - dis[i+s-1][j-1] \
                    - dis[i-1][j+t-1] + dis[i-1][j-1]
            ans = max(ans, num)

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

n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  ca=dict(Counter(arg))
  m=max(ca.keys())
  for i in range(1,m+1):
    if i not in ca:
      ca[i]=0
  while True:
    for i in range(1,m+1):
      if i==1 and i in ca and ca[i]:
        ca[i]-=1
      elif i>1:
        if i-1 in ca:
          ca[i-1]+=ca[i]
        else:
          ca[i-1]=ca[i]
        ca[i]=0
        if i==m:m-=1
    ret+=1
    if i==1:
      return ret+ca[1]

def dfs(v):
  ret=[]
  if len(ki[v])==0:return 1
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)+1
print(dfs(0)-1)


### FIXED CODE ###

n=int(input())
a=[int(input()) for _ in range(n-1)]
ki=[[] for _ in range(n)]
for i,x in enumerate(a):
  ki[x-1].append(i+1)

ans=0
import sys
sys.setrecursionlimit(10**7)
from collections import Counter
def marge(arg):
  ret=0
  arg.sort(reverse=True)
  for i,x in enumerate(arg):
    ret=max(ret,i+1+x)
  return ret


def dfs(v):
  ret=[]
  if len(ki[v])==0:return 0
  for nv in ki[v]:
    ret.append(dfs(nv))
  return marge(ret)
print(dfs(0))
### BUGGY CODE ###

import math
N=int(input())
total=0

for i in range(1,N+1):
  for j in range(1,N+1):
    for k in range(1,N+1):
      total+=math.gcd(math.gcd(i,j),k)
      
print(total)

### FIXED CODE ###

from math import gcd
N=int(input())
total=0

for i in range(1,N+1):
  for j in range(1,N+1):
    for k in range(1,N+1):
      total+=gcd(gcd(i,j),k)
      
print(total)
### BUGGY CODE ###

if __name__ == '__main__':
    k, s = map(int, input().split())
    ans = 0
    for i in range(k+1):
        for j in range(k+1):
            for l in range(k+1):
                if i+j+l == s:
                    ans += 1
    print(ans)

### FIXED CODE ###

if __name__ == '__main__':
    k, s = map(int, input().split())
    ans = 0
    for i in range(k+1):
        if i > s:
            break
        for j in range(k+1):
            z = s - i - j
            if i + j > s:
                break
            elif 0 <= z and z <= k:
                ans += 1
    print(ans)
### BUGGY CODE ###

from bisect import bisect_left

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

def check(m):
    cnt = 0
    for ai in a:
        if ai == 0:
            if m >= 0:
                cnt += n-1
        elif ai < 0:
            x = -(-m//ai)
            cnt += n - bisect_left(a, x)
            if ai>= x:
                cnt -= 1
        else:
            x = m//ai
            cnt += bisect_left(a, x+1)
            if ai <= x:
                cnt -= 1
    return cnt//2

left = -10**20
right = 10**20
while right-left>1:
    mid = (right+left)//2
    cnt = check(mid)
    if cnt >= k:
        right = mid
    else:
        left = mid

print(right)


### FIXED CODE ###

import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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

plus = minus = zero = 0
ps = []
ms = []
for i in a:
    if i == 0:
        zero += 1
    elif i > 0:
        plus += 1
        ps.append(i)
    else:
        minus += 1
        ms.append(-i)

ps.sort()
ms.sort()

def check(m):
    if m == 0:
        return plus*minus + zero*(zero-1)//2 + zero*(plus+minus) >= k
    elif m > 0:
        tmp = plus*minus + zero*(zero-1)//2 + zero*(plus+minus)
        for l in [ps, ms]:
            X = len(l)
            cur = X-1
            for i in range(X):
                while cur >= 0 and l[i]*l[cur] > m:
                    cur -= 1
                if cur <= i:
                    break
                tmp += cur-i
        return tmp >= k
    else:
        tmp = 0
        cur = 0
        for i in reversed(range(plus)):
            while cur < minus and ms[cur]*ps[i] < -m:
                cur += 1
            tmp += minus-cur
        return tmp >= k

left = -10**20
right = 10**20

while right-left>1:
    mid = (right+left)//2
    if check(mid):
        right = mid
    else:
        left = mid

print(right)
### BUGGY CODE ###

import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

class BIT:
	def __init__(self, n):
		self.n = n
		self.nums = [0] * (n+1)
		
	def add(self, i, x):
		n, nums = self.n, self.nums
		i += 1
		while i <= n:
			nums[i] += x
			i += i & -i
	
	def sum(self, i):
		nums = self.nums
		s = 0
		i += 1
		while i:
			s += nums[i]
			i -= i & -i
		return s
	
	def search(self, x):
		n, nums = self.n, self.nums
		s, p = 0, 0
		for i in range(n.bit_length(), -1, -1):
			np = p + (1<<i)
			if np <= n and s+nums[np] < x:
				s += nums[np]
				p = np
		return p

N, M = rl()
S = rl()
T = rl()

h = defaultdict(list)
for i, t in enumerate(T):
	h[t].append(i)
bit = BIT(M+1)
for i, s in enumerate(S):
	vals = []
	for j in h[s]:
		vals.append((j, bit.sum(j)+1))
	for j, v in vals:
		bit.add(j+1, v%mod)
print((bit.sum(M)+1)%mod)


### FIXED CODE ###

import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

class BIT:
	def __init__(self, n):
		self.n = n
		self.nums = [0] * (n+1)
		
	def add(self, i, x):
		n, nums = self.n, self.nums
		i += 1
		while i <= n:
			nums[i] += x
			i += i & -i
	
	def sum(self, i):
		nums = self.nums
		s = 0
		i += 1
		while i:
			s += nums[i]
			i -= i & -i
		return s
	
	def search(self, x):
		n, nums = self.n, self.nums
		s, p = 0, 0
		for i in range(n.bit_length(), -1, -1):
			np = p + (1<<i)
			if np <= n and s+nums[np] < x:
				s += nums[np]
				p = np
		return p

N, M = rl()
S = rl()
T = rl()

h = defaultdict(list)
for i, t in enumerate(T):
	h[t].append(i)
bit = BIT(M+1)
for i, s in enumerate(S):
	vals = []
	for j in h[s]:
		vals.append((j, bit.sum(j)+1))
	for j, v in vals:
		bit.add(j+1, v%mod)
print((bit.sum(M)+1)%mod)
### BUGGY CODE ###

n, k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
s=set(a)
d=[a.count(i) for i in s]
p=len(s)-k
d=sorted(d)
if p>0: print(sum(d[:p]))
else: print("0")


### FIXED CODE ###

n, k=[int(x) for x in input().split()]
a=[int(x) for x in input().split()]
d={i:0 for i in set(a)}
for i in a: d[i]+=1
print(n-sum([t[1] for t in sorted(d.items(), key=lambda x:-x[1])][:k]))
### BUGGY CODE ###

# input
N, Q = map(int, input().split())
S = input()

# check
counts = []
c_append = counts.append
for i in range(Q):
    l, r = map(int, input().split())
    substr = S[l - 1:r]
    AC_cnt = substr.replace("AC", "*").count("*")
    c_append(AC_cnt)

# output
for c in counts:
    print(c)

### FIXED CODE ###

N, Q = map(int, input().split())
S = input()

a = [0] * N
c = [False] * N
for i in range(1, N):
    if S[i - 1:i + 1] == "AC":
        a[i] = a[i - 1] + 1
        c[i] = True
    else:
        a[i] = a[i - 1]

for i in range(Q):
    l, r = map(int, input().split())
    if l - 1 == 0:
        print(a[r - 1])
    elif c[l - 1] is True:
        print(a[r - 1] - a[l - 1])
    else:
        print(a[r - 1] - a[l - 1])
### BUGGY CODE ###

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


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

MAP  = {}
COST = {}

for i in range(H):
    for j, l in enumerate(list(map(int, input().split()))):
        MAP[l] = (i, j)

for start in range(1, H*W+1):
    for stop in range(1, H*W+1):
        COST['%s,%s' % (start, stop)] = abs(MAP[start][0] - MAP[stop][0]) + abs(MAP[start][1] - MAP[stop][1])

Q = int(input())

for i in range(Q):
    L, R = map(int, input().split())
    cost = 0
    while L != R:
        cost += COST['%s,%s' % (L, L+D)]
        L    += D
    print(cost, flush=True)


### FIXED CODE ###

# speed-test
# https://abc089.contest.atcoder.jp/submissions/2206669

h,w,d = map(int,input().split())
 
dic = {}
for i in range(h):
    a = input().split()
    for j in range(w):
        dic[int(a[j])] = (i+1,j+1)
        
q = int(input())
c = [0]*(h*w+1)
for i in range(d+1,h*w+1):
    pi,pj = dic[i]; qi,qj = dic[i-d]
    c[i] = c[i-d] + abs(pi-qi) + abs(pj-qj)
    
for i in range(q):
    l,r = map(int,input().split())
    print(c[r]-c[l])
### BUGGY CODE ###

import sys
import time
import copy

N = int(input())
b = [int(b) for b in input().split()]
h = {}

t1 = time.time()

def f(b):
  t2 = time.time()
  if t2 - t1 > 1.9:
    print(-1)
    sys.exit()
  
  key = "".join([str(x) for x in b])
  if key in h:
    return copy.copy(h[key])
  
  if len(b) == 1:
    h[key] = b if b[0] == 1 else []
    return copy.copy(h[key])
  
  for i in range(len(b)):
    i = len(b) - 1 - i
    if b[i] == i + 1:
      b_sub = copy.copy(b)
      b_i = b_sub.pop(i)
      a = f(b_sub)
      if len(a) != 0:
        a.append(b_i)
        h[key] = a
        return copy.copy(h[key])
  
  h[key] = []
  return copy.copy(h[key])

a = f(b)
if len(a) == 0:
  print(-1)
else:
  for i in range(N):
    print(a[i])

### FIXED CODE ###

import sys
import time
import copy

N = int(input())
b = [int(b) for b in input().split()]
h = {}

t1 = time.time()

def f(b):
  t2 = time.time()
  if t2 - t1 > 1.5:
    print(-1)
    sys.exit()
  
  key = "".join([str(x) for x in b])
  if key in h:
    return copy.copy(h[key])
  
  if len(b) == 1:
    h[key] = b if b[0] == 1 else []
    return copy.copy(h[key])
  
  for i in range(len(b)):
    i = len(b) - 1 - i
    if b[i] == i + 1:
      b_sub = copy.copy(b)
      b_i = b_sub.pop(i)
      a = f(b_sub)
      if len(a) != 0:
        a.append(b_i)
        h[key] = a
        return copy.copy(h[key])
  
  h[key] = []
  return copy.copy(h[key])

a = f(b)
if len(a) == 0:
  print(-1)
else:
  for i in range(N):
    print(a[i])