Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline
def main() :
    H,W,D = map(int,input().split())
    Point = {}
    for y in range(H) :
        line = list(map(int,input().split()))
        for x,p in enumerate(line) :
            Point[p] = (y,x)

    Q = int(input())

    def dist(ny,nx,py,px) :
        return abs(ny-py) + abs(nx-px)

    for i in range(Q) :
        l,r = map(int,input().split())
        ny,nx = Point[l]
        res = 0
        while l != r :
            l += D
            py,px = ny,nx
            ny,nx = Point[l]
            res += dist(ny,nx,py,px)
        print(res)
main()


Fixed submission:
H,W,D = map(int,input().split())
Point = {}
for y in range(H) :
    line = list(map(int,input().split()))
    for x,p in enumerate(line) :
        Point[p] = (y,x)

Q = int(input())

def dist(ny,nx,py,px) :
    return abs(ny-py) + abs(nx-px)

Cost = [0]*(H*W+1)
for i in range(D+1, H*W+1) :
    yi,xi = Point[i-D]
    yj,xj = Point[i]
    Cost[i] = Cost[i-D] + dist(yi,xi,yj,xj)


for i in range(Q) :
    l,r = map(int,input().split())
    print(Cost[r] - Cost[l])

Buggy python submission (verdict: Time Limit Exceeded):
H,W,D = map(int,input().split())
Point = {}
for y in range(H) :
    line = list(map(int,input().split()))
    for x,p in enumerate(line) :
        Point[p] = (y,x)

Q = int(input())

def dist(ny,nx,py,px) :
    return abs(ny-py) + abs(nx-px)

for i in range(Q) :
    l,r = map(int,input().split())
    ny,nx = Point[l]
    res = 0
    while l != r :
        l += D
        py,px = ny,nx
        ny,nx = Point[l]
        res += dist(ny,nx,py,px)
    print(res)


Fixed submission:
H,W,D = map(int,input().split())
Point = {}
for y in range(H) :
    line = list(map(int,input().split()))
    for x,p in enumerate(line) :
        Point[p] = (y,x)

Q = int(input())

def dist(ny,nx,py,px) :
    return abs(ny-py) + abs(nx-px)

Cost = [0]*(H*W+1)
for i in range(D+1, H*W+1) :
    yi,xi = Point[i-D]
    yj,xj = Point[i]
    Cost[i] = Cost[i-D] + dist(yi,xi,yj,xj)


for i in range(Q) :
    l,r = map(int,input().split())
    print(Cost[r] - Cost[l])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

H,W,D = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H)]
Q = int(input())
LR = [list(map(int,input().split())) for i in range(Q)]

Point = {}
for y in range(H) :
    for x in range(W) :
        Point[A[y][x]] = (y, x)

def dist (y1,x1,y2,x2) :
    return abs(y1-y2) + abs(x1-x2)

for i in range(Q) :
    res = 0
    l,r = LR[i][0], LR[i][1]
    while l != r :
        l += D
        res += dist(Point[l][0], Point[l][1], Point[l-D][0], Point[l-D][1])
    print(res)


Fixed submission:
H,W,D = map(int,input().split())
Point = {}
for y in range(H) :
    line = list(map(int,input().split()))
    for x,p in enumerate(line) :
        Point[p] = (y,x)

Q = int(input())

def dist(ny,nx,py,px) :
    return abs(ny-py) + abs(nx-px)

Cost = [0]*(H*W+1)
for i in range(D+1, H*W+1) :
    yi,xi = Point[i-D]
    yj,xj = Point[i]
    Cost[i] = Cost[i-D] + dist(yi,xi,yj,xj)


for i in range(Q) :
    l,r = map(int,input().split())
    print(Cost[r] - Cost[l])

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

H,W,D = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H)]
Q = int(input())
LR = [list(map(int,input().split())) for i in range(Q)]

Point = {}
for y in range(H) :
    for x in range(W) :
        Point[A[y][x]] = (y, x)

def dist (y1,x1,y2,x2) :
    return abs(y1-y2) + abs(x1-x2)

for i in range(Q) :
    res = 0
    l,r = LR[i][0], LR[i][1]
    while l != r :
        l += D
        res += dist(Point[l][0], Point[l][1], Point[l-D][0], Point[l-D][1])
    print(res)


Fixed submission:
H,W,D = map(int,input().split())
Point = {}
for y in range(H) :
    line = list(map(int,input().split()))
    for x,p in enumerate(line) :
        Point[p] = (y,x)

Q = int(input())

def dist(ny,nx,py,px) :
    return abs(ny-py) + abs(nx-px)

Cost = [0]*(H*W+1)
for i in range(D+1, H*W+1) :
    yi,xi = Point[i-D]
    yj,xj = Point[i]
    Cost[i] = Cost[i-D] + dist(yi,xi,yj,xj)


for i in range(Q) :
    l,r = map(int,input().split())
    print(Cost[r] - Cost[l])

Buggy python submission (verdict: Time Limit Exceeded):
H,W,D = map(int,input().split())
A = [list(map(int,input().split())) for i in range(H)]
Q = int(input())
LR = [list(map(int,input().split())) for i in range(Q)]

Point = {}
for y in range(H) :
    for x in range(W) :
        Point[A[y][x]] = (y, x)

def dist (y1,x1,y2,x2) :
    return abs(y1-y2) + abs(x1-x2)

for i in range(Q) :
    res = 0
    l,r = LR[i][0], LR[i][1]
    while l != r :
        l += D
        res += dist(Point[l][0], Point[l][1], Point[l-D][0], Point[l-D][1])
    print(res)


Fixed submission:
H,W,D = map(int,input().split())
Point = {}
for y in range(H) :
    line = list(map(int,input().split()))
    for x,p in enumerate(line) :
        Point[p] = (y,x)

Q = int(input())

def dist(ny,nx,py,px) :
    return abs(ny-py) + abs(nx-px)

Cost = [0]*(H*W+1)
for i in range(D+1, H*W+1) :
    yi,xi = Point[i-D]
    yj,xj = Point[i]
    Cost[i] = Cost[i-D] + dist(yi,xi,yj,xj)


for i in range(Q) :
    l,r = map(int,input().split())
    print(Cost[r] - Cost[l])