Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [[-1] * (H * W + 1) for x in range(H * W + 1)]

def calc(table, l, r):
    if l == r:
        return 0
    if table[l][r] != -1:
        return table[l][r]
    m_ = abs(A[l+D][0] - A[l][0]) + abs(A[l+D][1] - A[l][1])
    ret = m_ + calc(table, l + D, r)
    table[l][r] = ret
    return ret

Q = int(input())
for q in range(Q):
    l, r = [int(x) for x in input().split()]
    print(calc(table, l, r))


Fixed submission:
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [0] * (H * W + 1)
for i in range(D):
    x = H * W - i - D
    r = 0
    while x > 0:
        table[x] = abs(A[x][0] - A[x+D][0]) + abs(A[x][1] - A[x+D][1]) \
                   + r
        r = table[x]
        x -= D
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    print(table[l] - table[r])

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [-1] * (H * W + 1)
# def calc(table, l, r, m):
#     if table[l] != -1:
#         return table[l][r]
#     m_ = abs(A[l+D][0] - A[l][0]) + abs(A[l+D][1] - A[l][1])
#     table[l][l+D] = m_
# print(table)

Q = int(input())
for q in range(Q):
    l, r = [int(x) for x in input().split()]
    m = 0
    while l != r:
        if table[l] == -1:
            table[l] = abs(A[l+D][0] - A[l][0]) + \
                       abs(A[l+D][1] - A[l][1])
        m += table[l]
        l = l + D
    print(m)


Fixed submission:
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [0] * (H * W + 1)
for i in range(D):
    x = H * W - i - D
    r = 0
    while x > 0:
        table[x] = abs(A[x][0] - A[x+D][0]) + abs(A[x][1] - A[x+D][1]) \
                   + r
        r = table[x]
        x -= D
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    print(table[l] - table[r])

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)
Q = int(input())
for q in range(Q):
    l, r = [int(x) for x in input().split()]
    m = 0
    while l != r:
        m += abs(A[l+D][0] - A[l][0]) + abs(A[l+D][1] - A[l][1])
        l = l + D
    print(m)


Fixed submission:
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [0] * (H * W + 1)
for i in range(D):
    x = H * W - i - D
    r = 0
    while x > 0:
        table[x] = abs(A[x][0] - A[x+D][0]) + abs(A[x][1] - A[x+D][1]) \
                   + r
        r = table[x]
        x -= D
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    print(table[l] - table[r])

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    m = 0
    while l != r:
        m += abs(A[l+D][0] - A[l][0]) + abs(A[l+D][1] - A[l][1])
        l = l + D
    print(m)


Fixed submission:
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [0] * (H * W + 1)
for i in range(D):
    x = H * W - i - D
    r = 0
    while x > 0:
        table[x] = abs(A[x][0] - A[x+D][0]) + abs(A[x][1] - A[x+D][1]) \
                   + r
        r = table[x]
        x -= D
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    print(table[l] - table[r])

Buggy python submission (verdict: Time Limit Exceeded):
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    m = 0
    while l != r:
        m += abs(A[l+D][0] - A[l][0]) + abs(A[l+D][1] - A[l][1])
        l = l + D
    print(m)


Fixed submission:
H, W, D = [int(x) for x in input().split()]
A = [0] * (H * W + 1)
for h in range(H):
    inp = [int(x) for x in input().split()]
    for i, a in enumerate(inp):
        A[a] = (i + 1, h + 1)

table = [0] * (H * W + 1)
for i in range(D):
    x = H * W - i - D
    r = 0
    while x > 0:
        table[x] = abs(A[x][0] - A[x+D][0]) + abs(A[x][1] - A[x+D][1]) \
                   + r
        r = table[x]
        x -= D
for q in range(int(input())):
    l, r = [int(x) for x in input().split()]
    print(table[l] - table[r])