#include <bits/stdc++.h>
#include <cmath>
#include <cstddef>
#include <functional>
#include <iostream>
#include <string>
#include <type_traits>

// lib/vector2.hpp
#ifndef VECTOR2_HPP
#define VECTOR2_HPP


/*
usage:

tools::vector2<int>(6, 5) // => (6, 5)
*/

namespace tools {
  template <typename T>
  class vector2 {
  public:
    T x;
    T y;

    vector2() :
      vector2(T(), T()) {
    }

    vector2(const T& x, const T& y) :
      x(x),
      y(y) {
    }

    double norm() const {
      return std::sqrt(this->squaredNorm());
    }

    T squaredNorm() const {
      return this->inner_product(*this);
    }

    template <typename CHECK = T, typename std::enable_if<std::is_same<CHECK, double>::value, std::nullptr_t>::type = nullptr>
    double normalize() const {
      return *this / this->norm();
    }

    vector2<T> operator+() const {
      return *this;
    }

    vector2<T> operator-() const {
      return vector2<T>(-this->x, -this->y);
    }

    friend vector2<T> operator+(const vector2<T>& lhs, const vector2<T>& rhs) {
      return vector2<T>(lhs.x + rhs.x, lhs.y + rhs.y);
    }

    friend vector2<T> operator-(const vector2<T>& lhs, const vector2<T>& rhs) {
      return vector2<T>(lhs.x - rhs.x, lhs.y - rhs.y);
    }

    template <typename T2, typename std::enable_if<!std::is_same<T2, vector2<T>>::value, std::nullptr_t>::type = nullptr>
    friend vector2<T> operator*(const vector2<T>& lhs, const T2& rhs) {
      return vector2<T>(lhs.x * rhs, lhs.y * rhs);
    }
    template <typename T2, typename std::enable_if<!std::is_same<T2, vector2<T>>::value, std::nullptr_t>::type = nullptr>
    friend vector2<T> operator*(const T2& lhs, const vector2<T>& rhs) {
      return vector2<T>(lhs * rhs.x, lhs * rhs.y);
    }

    template <typename T2, typename std::enable_if<!std::is_same<T2, vector2<T>>::value, std::nullptr_t>::type = nullptr>
    friend vector2<T> operator/(const vector2<T>& lhs, const T2& rhs) {
      return vector2<T>(lhs.x / rhs, lhs.y / rhs);
    }

    T inner_product(const vector2<T>& other) const {
      return this->x * other.x + this->y * other.y;
    }

    vector2<T>& operator+=(const vector2<T>& other) {
      return *this = *this + other;
    }

    vector2<T>& operator-=(const vector2<T>& other) {
      return *this = *this - other;
    }

    friend bool operator==(const vector2<T>& lhs, const vector2<T>& rhs) {
      return lhs.x == rhs.x && lhs.y == rhs.y;
    }

    friend bool operator!=(const vector2<T>& lhs, const vector2<T>& rhs) {
      return lhs.x != rhs.x || lhs.y != rhs.y;
    }
  };
}

namespace std {
  template <typename T>
  string to_string(const tools::vector2<T>& f) {
    return '(' + to_string(f.x) + ", " + to_string(f.y) + ')';
  }

  template <typename T>
  struct hash<tools::vector2<T>> {
    typedef std::size_t result_type;
    typedef tools::vector2<T> argument_type;
    std::size_t operator()(const tools::vector2<T>& key) const {
      static const std::hash<T> hash = std::hash<T>();
      std::size_t result = 17;
      result = 31 * result + hash(key.x);
      result = 31 * result + hash(key.y);
      return result;
    }
  };
}

template <typename T>
std::ostream& operator<<(std::ostream& os, const tools::vector2<T>& self) {
  return os << std::to_string(self);
}

#endif

// main.cpp

int main() {
  std::int_fast64_t H, W, D;
  std::cin >> H >> W >> D;

  std::vector<tools::vector2<std::int_fast64_t>> phase1(H * W);
  for (std::int_fast64_t y = 0; y < H; ++y) {
    for (std::int_fast64_t x = 0; x < W; ++x) {
      std::int_fast64_t A_yx;
      std::cin >> A_yx;
      phase1[A_yx - 1] = tools::vector2<std::int_fast64_t>(x, y);
    }
  }

  std::vector<std::vector<std::int_fast64_t>> phase2;
  for (std::int_fast64_t i = 0; i < D; ++i) {
    phase2.emplace_back();
    phase2.back().push_back(0);
    for (std::int_fast64_t j = i + D; j < H * W; j += D) {
      phase2.back().push_back(phase2.back().back() + std::abs(phase1[j].x - phase1[j - D].x) + std::abs(phase1[j].y - phase1[j - D].y));
    }
  }

  std::int_fast64_t Q;
  std::cin >> Q;
  for (std::int_fast64_t i = 0; i < Q; ++i) {
    std::int_fast64_t L_i, R_i;
    std::cin >> L_i >> R_i;
    const std::vector<std::int_fast64_t>& cache = phase2[(L_i - 1) % D];
    std::cout << cache[(R_i - 1) / D] - cache[(L_i - 1) / D] << std::endl;
  }
  return 0;
}

Measured memory_bytes: 6836