Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [[]for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d].append(i[0])

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum].index(i[0])+1
    r = dinds[dnum].index(i[1])+1
    print(stims[dnum][r][0]-stims[dnum][l][0])


Fixed submission:
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [{}for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d][i[0]]=len(stims[d])-1

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum][i[0]]
    r = dinds[dnum][i[1]]
    print(stims[dnum][r][0]-stims[dnum][l][0])

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [[]for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d].append(i[0])

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum].index(i[0])+1
    r = dinds[dnum].index(i[1])+1
    print(stims[dnum][r][0]-stims[dnum][l][0])


Fixed submission:
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [{}for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d][i[0]]=len(stims[d])-1

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum][i[0]]
    r = dinds[dnum][i[1]]
    print(stims[dnum][r][0]-stims[dnum][l][0])

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
dli = [[] for _ in range(D)]
for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        dli[j%D].append([j,i,ind])
        dli[j%D].sort()
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = []
dinds = []
for d in dli:
    #total dist,now pos
    #inds = num
    stim = [[0,0,0]]
    inds = []
    for i in d:
        stim.append([stim[-1][0]+abs(i[1]-stim[-1][1])+abs(i[2]-stim[-1][2]),i[1],i[2]])
        inds.append(i[0])
    stims.append(stim)
    dinds.append(inds)

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum].index(i[0])+1
    r = dinds[dnum].index(i[1])+1
    print(stims[dnum][r][0]-stims[dnum][l][0])


Fixed submission:
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [{}for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d][i[0]]=len(stims[d])-1

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum][i[0]]
    r = dinds[dnum][i[1]]
    print(stims[dnum][r][0]-stims[dnum][l][0])

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
dli = [[] for _ in range(D)]
for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        dli[j%D].append([j,i,ind])
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = []
dinds = []
for d in dli:
    #total dist,now pos
    #inds = num
    stim = [[0,0,0]]
    inds = []
    d.sort(key=itemgetter(0))
    for i in d:
        stim.append([stim[-1][0]+abs(i[1]-stim[-1][1])+abs(i[2]-stim[-1][2]),i[1],i[2]])
        inds.append(i[0])
    stims.append(stim)
    dinds.append(inds)

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum].index(i[0])+1
    r = dinds[dnum].index(i[1])+1
    print(stims[dnum][r][0]-stims[dnum][l][0])


Fixed submission:
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [{}for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d][i[0]]=len(stims[d])-1

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum][i[0]]
    r = dinds[dnum][i[1]]
    print(stims[dnum][r][0]-stims[dnum][l][0])

Buggy python submission (verdict: Time Limit Exceeded):
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
dli = [[] for _ in range(D)]
for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        dli[j%D].append([j,i,ind])
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = []
dinds = []
for d in dli:
    #total dist,now pos
    #inds = num
    stim = [[0,0,0]]
    inds = []
    d.sort(key=itemgetter(0))
    for i in d:
        stim.append([stim[-1][0]+abs(i[1]-stim[-1][1])+abs(i[2]-stim[-1][2]),i[1],i[2]])
        inds.append(i[0])
    stims.append(stim)
    dinds.append(inds)

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum].index(i[0])+1
    r = dinds[dnum].index(i[1])+1
    print(stims[dnum][r][0]-stims[dnum][l][0])


Fixed submission:
from operator import itemgetter

H,W,D = [int(x) for x in input().split()]
a = []

for i in range(H):
    x = [int(x) for x in input().split()]
    for ind,j in enumerate(x):
        a.append([j,i,ind])
a.sort(key=itemgetter(0))
Q = int(input())
LR = [[int(x) for x in input().split()]for _ in range(Q)]
#%%
stims = [[[0,0,0]]for _ in range(D)]
dinds = [{}for _ in range(D)]
for i in a:
    d = i[0]%D
    stims[d].append([stims[d][-1][0]+abs(i[1]-stims[d][-1][1])+abs(i[2]-stims[d][-1][2]),i[1],i[2]])
    dinds[d][i[0]]=len(stims[d])-1

for i in LR:
    dnum = i[0]%D
    l = dinds[dnum][i[0]]
    r = dinds[dnum][i[1]]
    print(stims[dnum][r][0]-stims[dnum][l][0])