program main
   integer h,w,d,i,j,fie(1:2,0:100000),dist(0:100000),q,l,r
   integer,ALLOCATABLE :: a(:,:)
   read(*,*)h,w,d
   allocate(a(1:h,1:w))
   do i=1,h
      read(*,*)a(i,1:w)
   enddo
   
   do i=1,h
      do j=1,w
         fie(1,a(i,j))=i
         fie(2,a(i,j))=j
      enddo
   enddo
   dist(1)=0
   dist(0)=0
   do i=2,H*W+100
      if(i-d<0)then
         dist(i)=0
      else
         dist(i)=dist(i-d)+max(fie(1,i)-fie(1,i-d),-fie(1,i)+fie(1,i-d))+max(fie(2,i)-fie(2,i-d),-fie(2,i)+fie(2,i-d))
      endif
   enddo
   
   read(*,*)q
   do i=1,q
      read(*,*)l,r
      write(*,*)dist(r)-dist(l)
   enddo

end program main

