Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = map(int, input().split())

# W_max = (W)
fields = [0] * (W*H+1)
# 一列にして、保存しておこうか。
for i in range(H):
    rows = list(map(int, input().split()))
    for j,r in enumerate(rows):
        ind = i * W + j
        fields[r] = ind 
# 各Dにおいて累積和
def calc(l,r):
    cnt = 0
    for i in range(l+D,r+1,D):
        indl = fields[i-D]
        hl = indl // W
        wl = indl % W
        indr = fields[i]
        hr = indr // W
        wr = indr % W
        d = abs(hl-hr) + abs(wl-wr)
        cnt += d
        # print(hl,wl,hr,wr,d)
    return cnt
### query
Q = int(input())
for _ in range(Q):
    L,R = map(int, input().split())
    print(calc(L,R))

Fixed submission:
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W//D+1) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# あまりごとに累積和を取っておく
acc = [[0] * (H*W//D+1) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y
# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W , D = map(int, input().split())
fields = []
X = []
for i in range(H):
    # fields.append(list(map(int, input().split())))
    X.extend(list(map(int, input().split())))
pairs = [(x-1,i) for i,x in enumerate(X)]
pairs.sort()
# print(pairs)
def calc_dist(a,b):
    return abs(a//W-b//W) + abs(a%W-b%W)

Q = int(input())
for i in range(Q):
    L1,R1 = map(int, input().split())
    ans = 0
    for i in range(L1,R1,D):
        a = pairs[i-1][1]
        b = pairs[i+D-1][1]
        ans += calc_dist(a,b)
        # print(i,a,b,ans)
    print(ans)
        


Fixed submission:
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W//D+1) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# あまりごとに累積和を取っておく
acc = [[0] * (H*W//D+1) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y
# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)

Buggy python submission (verdict: Runtime Error):
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W)//D+1 for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# print(amaris[:10])
# あまりごとに累積和を取っておく
acc = [[0] * (H*W)//D+1 for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y

# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)



Fixed submission:
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W//D+1) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# あまりごとに累積和を取っておく
acc = [[0] * (H*W//D+1) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y
# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)

Buggy python submission (verdict: Runtime Error):
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# print(amaris[:10])
# あまりごとに累積和を取っておく
acc = [[0] * (H*W) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y


Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)



Fixed submission:
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W//D+1) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# あまりごとに累積和を取っておく
acc = [[0] * (H*W//D+1) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y
# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# print(amaris[:10])

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    coords = amaris[m][s:g+1]
    ans = 0
    x_pre,y_pre = coords[0]
    for x, y in coords[1:]:
        ans += abs(x-x_pre)+abs(y-y_pre)        
        x_pre = x
        y_pre = y
    print(ans)


Fixed submission:
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W//D+1) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# あまりごとに累積和を取っておく
acc = [[0] * (H*W//D+1) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y
# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# print(amaris[:10])

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    coords = amaris[m][s:g+1]
    ans = 0
    x_pre,y_pre = coords[0]
    for x, y in coords[1:]:
        ans += abs(x-x_pre)+abs(y-y_pre)        
        x_pre = x
        y_pre = y
    print(ans)


Fixed submission:
H,W,D = map(int, input().split())
fields = []
amaris = [[0] * (H*W//D+1) for i in range(D)] # あまり
for i in range(H):
    row = list(map(int, input().split()))
    # あまりがいくつかで分類
    for j,e in enumerate(row):
        n = (e-1) // D
        m = (e-1) % D
        amaris[m][n] = (i,j)

# あまりごとに累積和を取っておく
acc = [[0] * (H*W//D+1) for i in range(D)]
for i in range(D):
    cnt = 0
    x_pre, y_pre = amaris[i][0]
    for j in range(1,(H*W)//D+1):
        coords = amaris[i][j]
        if isinstance(coords,int): break
        x = coords[0]
        y = coords[1]
        cnt += abs(x-x_pre)+abs(y-y_pre)
        acc[i][j] = cnt
        x_pre = x
        y_pre = y
# print(acc)

Q = int(input())
for i in range(Q):
    l,r  = map(int, input().split())
    m = (l-1) % D
    s = (l-1) // D
    g = (r-1) // D
    ans = acc[m][g] - acc[m][s]
    print(ans)