Buggy python submission (verdict: Time Limit Exceeded):

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))



mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



H,W,D=acinput()
#Ad={}
Ad=[0]*(H*W+1)
for i in range(H):
    ar=acinput()
    for j,sar in enumerate(ar):
        #Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d=[0]*(10**7)

#for x in range(1, H*W+1):
#    dp[x][x]=0

for x in range(1,H*W+1):
    y=x+D
    while True:
        if y > H*W:
            break
        #print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        #dp[x][y]=dp[x][y-D]+S
        d[y]=d[y-D]+S
        y+=D
        

#print(np.array(dp))

Q=int(input())
for i in range(Q):
    l,r=acinput()
    print(d[r]-d[l])


#print(Ad)
#print(np.array(dp))



Fixed submission:

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)

Buggy python submission (verdict: Runtime Error):

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))



mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



H,W,D=acinput()
#Ad={}
Ad=[0]*(H*W+1)
for i in range(H):
    ar=acinput()
    for j,sar in enumerate(ar):
        #Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]

for x in range(1, H*W+1):
    dp[x][x]=0

for x in range(1,H*W+1):
    y=x+D
    while True:
        if y > H*W:
            break
        #print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        dp[x][y]=dp[x][y-D]+S
        y+=D
        

#print(np.array(dp))

Q=int(input())
for i in range(Q):
    l,r=acinput()
    print(dp[l][r])


#print(Ad)
#print(np.array(dp))



Fixed submission:

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)

Buggy python submission (verdict: Time Limit Exceeded):

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))



mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

H,W,D=acinput()
#Ad={}
Ad=[0]*(H*W+1)
for i in range(H):
    ar=acinput()
    for j,sar in enumerate(ar):
        #Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]

for x in range(1, H*W+1):
    dp[x][x]=0

for x in range(1,H*W+1):
    y=x+D
    while True:
        if y > H*W:
            break
        #print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        dp[x][y]=dp[x][y-D]+S
        y+=D
        

#print(np.array(dp))

Q=int(input())
for i in range(Q):
    l,r=acinput()
    print(dp[l][r])


#print(Ad)
#print(np.array(dp))



Fixed submission:

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)

Buggy python submission (verdict: Runtime Error):

import numpy as np


def acinput():
    return list(map(int, input().split(" ")))



directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

H,W,D=acinput()
#Ad={}
Ad=[0]*(H*W+1)
for i in range(H):
    ar=acinput()
    for j,sar in enumerate(ar):
        Ad[sar]=np.array((i,j))

dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]

for x in range(1, H*W+1):
    dp[x][x]=0

for x in range(1,H*W+1):
    y=x+D
    while True:
        if y > H*W:
            break
        #print(x,y)
        tmp = Ad[y]-Ad[y-D]
        S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        dp[x][y]=dp[x][y-D]+S
        y+=D
        

#print(np.array(dp))

Q=int(input())
for i in range(Q):
    l,r=acinput()
    print(dp[l][r])


#print(Ad)
#print(np.array(dp))



Fixed submission:

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)

Buggy python submission (verdict: Runtime Error):

import numpy as np


def acinput():
    return list(map(int, input().split(" ")))



directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

H,W,D=acinput()
#Ad={}
Ad=[0]*(H*W+1)
for i in range(H):
    ar=acinput()
    for j,sar in enumerate(ar):
        Ad[sar]=np.array((i,j))

dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]

for x in range(1, H*W+1):
    dp[x][x]=0

for x in range(1,H*W+1):
    y=x+D
    while True:
        if y > H*W:
            break
        #print(x,y)
        tmp = Ad[y]-Ad[y-D]
        S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        dp[x][y]=dp[x][y-D]+S
        y+=D
        

#print(np.array(dp))

Q=int(input())
for i in range(Q):
    l,r=acinput()
    print(dp[l][r])


#print(Ad)
#print(np.array(dp))



Fixed submission:

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)

Buggy python submission (verdict: Runtime Error):

import numpy as np


def acinput():
    return list(map(int, input().split(" ")))



directions=np.array([[1,0],[0,1],[-1,0],[0,-1]])
directions = list(map(np.array, directions))

mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact



def serch(x, count):
    #print("top", x, count)
            

    for d in directions:
        nx = d+x
        #print(nx)
        if np.all(0 <= nx) and np.all(nx < (H, W)):
            if field[nx[0]][nx[1]] == "E":
                count += 1 
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)  
                continue
            if field[nx[0]][nx[1]] == "#":
                field[nx[0]][nx[1]] = "V"
                count = serch(nx, count)    
                 
    return count

H,W,D=acinput()
#Ad={}
Ad=[0]*(H*W+1)
for i in range(H):
    ar=acinput()
    for j,sar in enumerate(ar):
        Ad[sar]=np.array((i,j))

dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]

for x in range(1, H*W+1):
    dp[x][x]=0

for x in range(1,H*W+1):
    y=x+D
    while True:
        if y > H*W:
            break
        #print(x,y)
        tmp = Ad[y]-Ad[y-D]
        S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        dp[x][y]=dp[x][y-D]+S
        y+=D
        

#print(np.array(dp))

Q=int(input())
for i in range(Q):
    l,r=acinput()
    print(dp[l][r])


Fixed submission:

#import numpy as np


def acinput():
    return list(map(int, input().split(" ")))


mod = 10**9+7


def factorial(n):
    fact = 1
    for integer in range(1, n + 1):
        fact *= integer
    return fact


H, W, D = acinput()
# Ad={}
Ad = [0]*(H*W+1)
for i in range(H):
    ar = acinput()
    for j, sar in enumerate(ar):
        # Ad[sar]=np.array((i,j))
        Ad[sar] = (i, j)

#dp = [[0 for j in range(H*W+1)]for i in range(H*W+1)]
d = [0]*(H*W+1)

# for x in range(1, H*W+1):
#    dp[x][x]=0

y = D+1
while True:
        if y > H*W:
            break
        # print(x,y)
        #tmp = Ad[y]-Ad[y-D]
        #S = np.sum(np.abs(tmp[0])+abs(tmp[1]))
        #tmp = Ad[y]-Ad[y-D]
        S = abs(Ad[y][0]-Ad[y-D][0])+abs(Ad[y][1]-Ad[y-D][1])

        # dp[x][y]=dp[x][y-D]+S
        d[y] = d[y-D]+S
        y += 1


# print(np.array(dp))

Q = int(input())
for i in range(Q):
    l, r = acinput()
    print(d[r]-d[l])


# print(Ad)
# print(np.array(dp))
#print(d)