#include <bits/stdc++.h>
using namespace std;
const int maxn = 11;
const int maxm = 100050;
int n, m, q, cnt, fa[maxn * maxm * 8];
struct node {
  int lef[maxn], rig[maxn];
  int shit;
} t[maxm * 4];
int A[maxn][maxm];
void init(int a) { fa[a] = a; }
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
void merge(node a, node b, int mid, node &c) {
  for (int i = 1; i <= n; ++i) {
    init(a.lef[i]);
    init(a.rig[i]);
    init(b.lef[i]);
    init(b.rig[i]);
  }
  c.shit = a.shit + b.shit;
  for (int i = 1; i <= n; ++i)
    if (A[i][mid] == A[i][mid + 1]) {
      int x = getfa(a.rig[i]), y = getfa(b.lef[i]);
      if (x == y) continue;
      --c.shit;
      fa[x] = y;
    }
  for (int i = 1; i <= n; ++i)
    c.lef[i] = getfa(a.lef[i]), c.rig[i] = getfa(b.rig[i]);
}
node ask(int l, int r, int L, int R, int k) {
  if (l <= L && R <= r) return t[k];
  int mid = L + R >> 1;
  if (r <= mid)
    return ask(l, r, L, mid, k << 1);
  else if (l > mid)
    return ask(l, r, mid + 1, R, k << 1 | 1);
  else {
    node bullshit;
    merge(ask(l, mid, L, mid, k << 1), ask(mid + 1, r, mid + 1, R, k << 1 | 1),
          mid, bullshit);
    return bullshit;
  }
}
void build(int l, int r, int k) {
  if (l == r) {
    for (int i = 1; i <= n; ++i) {
      if (A[i][l] != A[i - 1][l]) {
        t[k].lef[i] = t[k].rig[i] = ++cnt;
        t[k].shit++;
      } else
        t[k].lef[i] = t[k].rig[i] = t[k].lef[i - 1];
    }
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, k << 1);
  build(mid + 1, r, k << 1 | 1);
  merge(t[k << 1], t[k << 1 | 1], mid, t[k]);
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) scanf("%d", &A[i][j]);
  build(1, m, 1);
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", ask(l, r, 1, m, 1).shit);
  }
  return 0;
}

### Prompt

Please formulate a Cpp solution to the following problem:

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;
const int maxn = 11;
const int maxm = 100050;
int n, m, q, cnt, fa[maxn * maxm * 8];
struct node {
  int lef[maxn], rig[maxn];
  int shit;
} t[maxm * 4];
int A[maxn][maxm];
void init(int a) { fa[a] = a; }
int getfa(int x) { return fa[x] == x ? x : fa[x] = getfa(fa[x]); }
void merge(node a, node b, int mid, node &c) {
  for (int i = 1; i <= n; ++i) {
    init(a.lef[i]);
    init(a.rig[i]);
    init(b.lef[i]);
    init(b.rig[i]);
  }
  c.shit = a.shit + b.shit;
  for (int i = 1; i <= n; ++i)
    if (A[i][mid] == A[i][mid + 1]) {
      int x = getfa(a.rig[i]), y = getfa(b.lef[i]);
      if (x == y) continue;
      --c.shit;
      fa[x] = y;
    }
  for (int i = 1; i <= n; ++i)
    c.lef[i] = getfa(a.lef[i]), c.rig[i] = getfa(b.rig[i]);
}
node ask(int l, int r, int L, int R, int k) {
  if (l <= L && R <= r) return t[k];
  int mid = L + R >> 1;
  if (r <= mid)
    return ask(l, r, L, mid, k << 1);
  else if (l > mid)
    return ask(l, r, mid + 1, R, k << 1 | 1);
  else {
    node bullshit;
    merge(ask(l, mid, L, mid, k << 1), ask(mid + 1, r, mid + 1, R, k << 1 | 1),
          mid, bullshit);
    return bullshit;
  }
}
void build(int l, int r, int k) {
  if (l == r) {
    for (int i = 1; i <= n; ++i) {
      if (A[i][l] != A[i - 1][l]) {
        t[k].lef[i] = t[k].rig[i] = ++cnt;
        t[k].shit++;
      } else
        t[k].lef[i] = t[k].rig[i] = t[k].lef[i - 1];
    }
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, k << 1);
  build(mid + 1, r, k << 1 | 1);
  merge(t[k << 1], t[k << 1 | 1], mid, t[k]);
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) scanf("%d", &A[i][j]);
  build(1, m, 1);
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", ask(l, r, 1, m, 1).shit);
  }
  return 0;
}

```