Buggy python submission (verdict: Runtime Error):
H, W, D, *L = map(int, open('0').read().split())
A = L[:W*H]
Q = L[H*W]
inf = []
for l,r in zip(*[iter(L[H*W+1:])]*2):
  inf += [(l,r)]
dic = {A[i*W+j]:(i,j) for i in range(H) for j in range(W)}
ls = [[] for i in range(D)]
for i in range(D):
  s = i if i!=0 else D
  ls[i].append(0)
  m = s
  n = s
  while True:
    n += D
    if H*W<n:
      n = s
    px, py = dic[m]
    x, y = dic[n]
    ls[i].append(ls[i][-1]+abs(px-x)+abs(py-y))
    m = n
    if m==s:
      break
for l, r in inf:
  i = l%D
  if i==0:
    il = (l-D)//D
    ir = (r-D)//D
  else:
    il = (l-i)//D
    ir = (r-i)//D
  if l<r:
    print(ls[i][ir] - ls[i][il])
  elif l>r:
    print(ls[i][-1]-ls[i][il]+ls[i][ir])
  else:
    print(0)

Fixed submission:
H, W, D = map(int, input().split())
px = [0]*(H*W+1)
py = [0]*(H*W+1)
dp = [0]*(H*W+1)
for i in range(H):
  inf = input().split()
  for j in range(W):
    a = int(inf[j])
    py[a] = i
    px[a] = j
for i in range(D+1, H*W+1):
  dp[i] = dp[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  print(dp[R]-dp[L])

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = map(int, input().split())
dp = [[0]*(H+1)*(W+1) for i in range(D)]
dic = {}
for i in range(H):
  a = [int(i) for i in input().split()]
  for j in range(W):
    # y, x
    dic[a[j]] = (i, j)

for i in range(D):
  ps = i
  if ps==0:
    ps = D
  count = 0
  y, x = dic[ps]
  while True:
    ps += D
    if H*W<ps:
      break
    count += 1
    ny, nx = dic[ps]
    dp[i][count] = dp[i][count-1]+abs(ny-y)+abs(nx-x)
    y = ny
    x = nx


Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  ID = L%D
  LI = L//D
  RI = R//D
  if ID!=0:
    print(dp[ID][RI]-dp[ID][LI])
  else:
    print(dp[ID][RI-1]-dp[ID][LI-1])



Fixed submission:
H, W, D = map(int, input().split())
px = [0]*(H*W+1)
py = [0]*(H*W+1)
dp = [0]*(H*W+1)
for i in range(H):
  inf = input().split()
  for j in range(W):
    a = int(inf[j])
    py[a] = i
    px[a] = j
for i in range(D+1, H*W+1):
  dp[i] = dp[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  print(dp[R]-dp[L])

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = map(int, input().split())
dp = [[0]*(H+1)*(W+1) for i in range(D)]
dic = {}
for i in range(H):
  a = [int(i) for i in input().split()]
  for j in range(W):
    # y, x
    dic[a[j]] = (i, j)

for i in range(D):
  ps = i
  if ps==0:
    ps = D
  count = 0
  y, x = dic[ps]
  while True:
    ps += D
    if H*W<ps:
      break
    count += 1
    ny, nx = dic[ps]
    dp[i][count] = dp[i][count-1]+abs(ny-y)+abs(nx-x)
    y = ny
    x = nx


Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  ID = L%D
  LI = L//D
  RI = R//D
  if ID!=0:
    print(dp[ID][RI]-dp[ID][LI])
  else:
    print(dp[ID][RI-1]-dp[ID][LI-1])



Fixed submission:
H, W, D = map(int, input().split())
px = [0]*(H*W+1)
py = [0]*(H*W+1)
dp = [0]*(H*W+1)
for i in range(H):
  inf = input().split()
  for j in range(W):
    a = int(inf[j])
    py[a] = i
    px[a] = j
for i in range(D+1, H*W+1):
  dp[i] = dp[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
  L, R = map(int, input().split())
  print(dp[R]-dp[L])