#include <bits/stdc++.h>
using namespace std;
int n, m, Q, cnt, f[100100 * 20], A[15][100100];
struct node {
  int sum, L, R, l[15], r[15];
  node() { sum = 0; }
} tr[100100 << 3];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
inline node operator+(node a, node b) {
  if (!a.sum) return b;
  if (!b.sum) return a;
  node c;
  c.sum = a.sum + b.sum;
  c.L = a.L;
  c.R = b.R;
  for (int i = 1; i <= n; ++i) {
    f[a.l[i]] = a.l[i];
    f[a.r[i]] = a.r[i];
    f[b.l[i]] = b.l[i];
    f[b.r[i]] = b.r[i];
  }
  for (int i = 1; i <= n; ++i)
    if (A[i][a.R] == A[i][b.L]) {
      int p = find(a.r[i]), q = find(b.l[i]);
      if (p == q) continue;
      c.sum--;
      f[p] = q;
    }
  for (int i = 1; i <= n; ++i) {
    c.l[i] = find(a.l[i]);
    c.r[i] = find(b.r[i]);
  }
  return c;
}
void build(int p, int l, int r) {
  if (l == r) {
    for (int i = 1; i <= n; ++i) {
      if (A[i][l] == A[i - 1][l])
        tr[p].l[i] = tr[p].r[i] = tr[p].l[i - 1];
      else
        tr[p].l[i] = tr[p].r[i] = ++cnt, tr[p].sum++;
    }
    tr[p].L = l;
    tr[p].R = r;
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  tr[p] = tr[p << 1] + tr[p << 1 | 1];
}
node query(int p, int l, int r, int x, int y) {
  if (x <= l && y >= r) return tr[p];
  int mid = (l + r) >> 1;
  node ret;
  if (x <= mid) ret = ret + query(p << 1, l, mid, x, y);
  if (y > mid) ret = ret + query(p << 1 | 1, mid + 1, r, x, y);
  return ret;
}
int main() {
  n = read();
  m = read();
  Q = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) A[i][j] = read();
  build(1, 1, m);
  for (int i = 1; i <= Q; ++i) {
    int l = read(), r = read();
    printf("%d\n", query(1, 1, m, l, r).sum);
  }
  return 0;
}

### Prompt

Construct a CPP code solution to the problem outlined:

In his spare time Vladik estimates beauty of the flags.

Every flag could be represented as the matrix n × m which consists of positive integers.

Let's define the beauty of the flag as number of components in its matrix. We call component a set of cells with same numbers and between any pair of cells from that set there exists a path through adjacent cells from same component. Here is the example of the partitioning some flag matrix into components:

<image>

But this time he decided to change something in the process. Now he wants to estimate not the entire flag, but some segment. Segment of flag can be described as a submatrix of the flag matrix with opposite corners at (1, l) and (n, r), where conditions 1 ≤ l ≤ r ≤ m are satisfied.

Help Vladik to calculate the beauty for some segments of the given flag.

Input

First line contains three space-separated integers n, m, q (1 ≤ n ≤ 10, 1 ≤ m, q ≤ 105) — dimensions of flag matrix and number of segments respectively.

Each of next n lines contains m space-separated integers — description of flag matrix. All elements of flag matrix is positive integers not exceeding 106.

Each of next q lines contains two space-separated integers l, r (1 ≤ l ≤ r ≤ m) — borders of segment which beauty Vladik wants to know.

Output

For each segment print the result on the corresponding line.

Example

Input

4 5 4
1 1 1 1 1
1 2 2 3 3
1 1 1 2 5
4 4 5 5 5
1 5
2 5
1 2
4 5


Output

6
7
3
4

Note

Partitioning on components for every segment from first test case:

<image>

### Response

```cpp
#include <bits/stdc++.h>
using namespace std;
int n, m, Q, cnt, f[100100 * 20], A[15][100100];
struct node {
  int sum, L, R, l[15], r[15];
  node() { sum = 0; }
} tr[100100 << 3];
inline int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
int find(int x) { return f[x] == x ? x : f[x] = find(f[x]); }
inline node operator+(node a, node b) {
  if (!a.sum) return b;
  if (!b.sum) return a;
  node c;
  c.sum = a.sum + b.sum;
  c.L = a.L;
  c.R = b.R;
  for (int i = 1; i <= n; ++i) {
    f[a.l[i]] = a.l[i];
    f[a.r[i]] = a.r[i];
    f[b.l[i]] = b.l[i];
    f[b.r[i]] = b.r[i];
  }
  for (int i = 1; i <= n; ++i)
    if (A[i][a.R] == A[i][b.L]) {
      int p = find(a.r[i]), q = find(b.l[i]);
      if (p == q) continue;
      c.sum--;
      f[p] = q;
    }
  for (int i = 1; i <= n; ++i) {
    c.l[i] = find(a.l[i]);
    c.r[i] = find(b.r[i]);
  }
  return c;
}
void build(int p, int l, int r) {
  if (l == r) {
    for (int i = 1; i <= n; ++i) {
      if (A[i][l] == A[i - 1][l])
        tr[p].l[i] = tr[p].r[i] = tr[p].l[i - 1];
      else
        tr[p].l[i] = tr[p].r[i] = ++cnt, tr[p].sum++;
    }
    tr[p].L = l;
    tr[p].R = r;
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  tr[p] = tr[p << 1] + tr[p << 1 | 1];
}
node query(int p, int l, int r, int x, int y) {
  if (x <= l && y >= r) return tr[p];
  int mid = (l + r) >> 1;
  node ret;
  if (x <= mid) ret = ret + query(p << 1, l, mid, x, y);
  if (y > mid) ret = ret + query(p << 1 | 1, mid + 1, r, x, y);
  return ret;
}
int main() {
  n = read();
  m = read();
  Q = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) A[i][j] = read();
  build(1, 1, m);
  for (int i = 1; i <= Q; ++i) {
    int l = read(), r = read();
    printf("%d\n", query(1, 1, m, l, r).sum);
  }
  return 0;
}

```