Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 11:18:48 2018

@author: natsukiiwano
"""

_in_ = list(map(int,input().split(" ")))
H,W,D = _in_[0],_in_[1],_in_[2]
dic = {}
for h in range(H):
    _in_ = list(map(int,input().split(" ")))
    for w in range(W):
        dic[_in_[w]]=h+w*1j
dic2={}    

for q in range(int(input())):
    _in_ = list(map(int,input().split(" ")))
    (_from_,_to_) = (_in_[0],_in_[1])
    if (_from_,_to_) not in dic2:
        _sum_=0
        for j in range(int((_to_-_from_)/D)):
            _sum_ = _sum_ + int(abs(dic[_from_+j*D].real-dic[_from_+j*D+D].real)+abs(dic[_from_+j*D].imag-dic[_from_+j*D+D].imag))
        print(_sum_)
        dic2[(_from_,_to_)] = _sum_
    else:
        print(dic2[(_from_,_to_)])

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 12:24:26 2018

@author: natsukiiwano
"""
import numpy as np

_in_ = input().split(" ")
H,W,D = int(_in_[0]),int(_in_[1]),int(_in_[2])

px,py= np.zeros(90001),np.zeros(90001)
d=np.zeros(90001)
for i in range(H):
    _in_ = list(map(int,input().split(" ")))
    for j in range(W):
        px[_in_[j]]=i
        py[_in_[j]]=j

for i in range(D+1,H*W+1):
    d[i]=d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
    _in_ = input().split(" ")
    L,R = int(_in_[0]),int(_in_[1])
    print(int(d[R]-d[L]))

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 11:18:48 2018

@author: natsukiiwano
"""

_in_ = list(map(int,input().split(" ")))
H,W,D = _in_[0],_in_[1],_in_[2]
dic = {}
for h in range(H):
    _in_ = list(map(int,input().split(" ")))
    for w in range(W):
        dic[_in_[w]]=h+w*1j

dic3={}
dic2={}    

for q in range(int(input())):
    _in_ = list(map(int,input().split(" ")))
    (_from_,_to_) = (_in_[0],_in_[1])
    if (_from_,_to_) not in dic2:
        _sum_=0
        for j in range(int((_to_-_from_)/D)):
            if _from_+j*D not in dic3:
                n=_from_+j*D-1
                dic3[n+1] = int(abs(dic[n+1].real-dic[n+1+D].real)+abs(dic[n+1].imag-dic[n+1+D].imag))
            _sum_ = _sum_ + dic3[_from_+j*D]
        print(_sum_)
        dic2[(_from_,_to_)] = _sum_
    else:
        print(dic2[(_from_,_to_)])

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 12:24:26 2018

@author: natsukiiwano
"""
import numpy as np

_in_ = input().split(" ")
H,W,D = int(_in_[0]),int(_in_[1]),int(_in_[2])

px,py= np.zeros(90001),np.zeros(90001)
d=np.zeros(90001)
for i in range(H):
    _in_ = list(map(int,input().split(" ")))
    for j in range(W):
        px[_in_[j]]=i
        py[_in_[j]]=j

for i in range(D+1,H*W+1):
    d[i]=d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
    _in_ = input().split(" ")
    L,R = int(_in_[0]),int(_in_[1])
    print(int(d[R]-d[L]))

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 11:18:48 2018

@author: natsukiiwano
"""

_in_ = list(map(int,input().split(" ")))
H,W,D = _in_[0],_in_[1],_in_[2]
dic = {}
for h in range(H):
    _in_ = list(map(int,input().split(" ")))
    for w in range(W):
        dic[_in_[w]]=h+w*1j


for n in range(H*W-D):
    dic[n+1] = int(abs(dic[n+1].real-dic[n+1+D].real)+abs(dic[n+1].imag-dic[n+1+D].imag))

dic2={}    

for q in range(int(input())):
    _in_ = list(map(int,input().split(" ")))
    (_from_,_to_) = (_in_[0],_in_[1])
    if (_from_,_to_) not in dic2:
        _sum_=0
        for j in range(int((_to_-_from_)/D)):
            _sum_ = _sum_ + dic[_from_+j*D]
        print(_sum_)
        dic2[(_from_,_to_)] = _sum_
    else:
        print(dic2[(_from_,_to_)])

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 12:24:26 2018

@author: natsukiiwano
"""
import numpy as np

_in_ = input().split(" ")
H,W,D = int(_in_[0]),int(_in_[1]),int(_in_[2])

px,py= np.zeros(90001),np.zeros(90001)
d=np.zeros(90001)
for i in range(H):
    _in_ = list(map(int,input().split(" ")))
    for j in range(W):
        px[_in_[j]]=i
        py[_in_[j]]=j

for i in range(D+1,H*W+1):
    d[i]=d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
    _in_ = input().split(" ")
    L,R = int(_in_[0]),int(_in_[1])
    print(int(d[R]-d[L]))

Buggy python submission (verdict: Time Limit Exceeded):
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 11:18:48 2018

@author: natsukiiwano
"""

_in_ = list(map(int,input().split(" ")))
H,W,D = _in_[0],_in_[1],_in_[2]
dic = {}
for h in range(H):
    _in_ = list(map(int,input().split(" ")))
    for w in range(W):
        dic[_in_[w]]=h+w*1j


for n in range(H*W-D):
    dic[n+1] = int(abs(dic[n+1].real-dic[n+1+D].real)+abs(dic[n+1].imag-dic[n+1+D].imag))
        
for q in range(int(input())):
    _in_ = list(map(int,input().split(" ")))
    _from_,_to_ = _in_[0],_in_[1]
    _sum_=0
    while _to_ != _from_:
        _sum_ = _sum_ + dic[_from_]
        _from_ = _from_ + D
    print(_sum_)

Fixed submission:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 23 12:24:26 2018

@author: natsukiiwano
"""
import numpy as np

_in_ = input().split(" ")
H,W,D = int(_in_[0]),int(_in_[1]),int(_in_[2])

px,py= np.zeros(90001),np.zeros(90001)
d=np.zeros(90001)
for i in range(H):
    _in_ = list(map(int,input().split(" ")))
    for j in range(W):
        px[_in_[j]]=i
        py[_in_[j]]=j

for i in range(D+1,H*W+1):
    d[i]=d[i-D]+abs(px[i]-px[i-D])+abs(py[i]-py[i-D])

Q = int(input())
for i in range(Q):
    _in_ = input().split(" ")
    L,R = int(_in_[0]),int(_in_[1])
    print(int(d[R]-d[L]))