// import chie template :) {{{
import std.stdio,
       std.algorithm,
       std.array,
       std.string,
       std.math,
       std.conv,
       std.range,
       std.container,
       std.bigint,
       std.ascii,
       std.typecons;
// }}}
// tbh.scanner {{{
class Scanner {
  import std.stdio;
  import std.conv : to;
  import std.array : split;
  import std.string : chomp;

  private File file;
  private dchar[][] str;
  private uint idx;

  this(File file = stdin) {
    this.file = file;
    this.idx = 0;
  }

  private dchar[] next() {
    if (idx < str.length) {
      return str[idx++];
    }

    dchar[] s;
    while (s.length == 0) {
      s = file.readln.chomp.to!(dchar[]);
    }

    str = s.split;
    idx = 0;

    return str[idx++];
  }

  T next(T)() {
    return next.to!(T);
  }

  T[] nextArray(T)(uint len) {
    T[] ret = new T[len];

    foreach (ref c; ret) {
      c = next!(T);
    }

    return ret;
  }
}
// }}}

void main() {
  auto cin = new Scanner;
  int h = cin.next!int, w = cin.next!int, d = cin.next!int;
  auto imos = new long[][](d + 2);
  int[] y = new int[h * w + 1], x = new int[h * w + 1];
  foreach (i; 1 .. h + 1) {
    foreach (j; 1 .. w + 1) {
      int c = cin.next!int;
      y[c] = i;
      x[c] = j;
    }
  }
  foreach (i; 1 .. d + 1) {
    imos[i] ~= 0;
    int dy = y[i], dx = x[i];
    int idx = 0;
    for (int j = i + d; j < h * w + 1; j += d) {
      imos[i] ~= imos[i][idx++] + abs(dy - y[j]) + abs(dx - x[j]);
      dy = y[j];
      dx = x[j];
    }
  }
  int q = cin.next!int;
  foreach (i; 0 .. q) {
    int l = cin.next!int, r = cin.next!int;
    int idx = l % d == 0 ? d : l % d;
    if (idx == d) {
      writeln(imos[idx][r / d - 1] - imos[idx][l / d - 1]);
    } else {
      writeln(imos[idx][r / d] - imos[idx][l / d]);
    }
  }
}
