#include <bits/stdc++.h>
using namespace std;
const int N = 12;
const int M = 1e5 + 5;
int n, m, q, a[N][M], fa[N * M], seg_tot;
struct node {
  int l[N], r[N], sum;
};
node seg[M << 2], ans;
int getfa(int x) {
  if (fa[x] != x) fa[x] = getfa(fa[x]);
  return fa[x];
}
void newseg(node &x, int pos) {
  int i;
  for (i = 1; i <= n; ++i)
    if (a[i][pos] == a[i - 1][pos])
      x.l[i] = x.r[i] = x.l[i - 1];
    else
      x.l[i] = x.r[i] = ++seg_tot, ++x.sum;
}
void merge(node &x, node lsn, node rsn, int rb, int lb) {
  int i;
  x.sum = lsn.sum + rsn.sum;
  for (i = 1; i <= n; ++i) x.l[i] = lsn.l[i], x.r[i] = rsn.r[i];
  for (i = 1; i <= n; ++i) {
    fa[lsn.l[i]] = lsn.l[i], fa[lsn.r[i]] = lsn.r[i];
    fa[rsn.l[i]] = rsn.l[i], fa[rsn.r[i]] = rsn.r[i];
  }
  for (i = 1; i <= n; ++i)
    if (a[i][rb] == a[i][lb]) {
      int fx = getfa(lsn.r[i]);
      int fy = getfa(rsn.l[i]);
      if (fx != fy) --x.sum, fa[fx] = fy;
    }
  for (i = 1; i <= n; ++i) x.l[i] = getfa(x.l[i]), x.r[i] = getfa(x.r[i]);
}
void build(int l, int r, int rt) {
  if (l == r) {
    newseg(seg[rt], l);
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
  merge(seg[rt], seg[rt << 1], seg[rt << 1 | 1], mid, mid + 1);
}
void query(int l, int r, int rt, int L, int R) {
  if (L <= l && r <= R) {
    if (!ans.l[1])
      ans = seg[rt];
    else
      merge(ans, ans, seg[rt], l - 1, l);
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) query(l, mid, rt << 1, L, R);
  if (R > mid) query(mid + 1, r, rt << 1 | 1, L, R);
}
void init() {
  int i, j;
  scanf("%d%d%d", &n, &m, &q);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) scanf("%d", &a[i][j]);
}
void work() {
  int i, l, r;
  build(1, m, 1);
  while (q--) {
    for (i = 1; i <= n; ++i) ans.l[i] = 0;
    scanf("%d%d", &l, &r);
    query(1, m, 1, l, r);
    printf("%d\n", ans.sum);
  }
}
int main() {
  init();
  work();
  return 0;
}

### Prompt

Please create a solution in cpp 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 N = 12;
const int M = 1e5 + 5;
int n, m, q, a[N][M], fa[N * M], seg_tot;
struct node {
  int l[N], r[N], sum;
};
node seg[M << 2], ans;
int getfa(int x) {
  if (fa[x] != x) fa[x] = getfa(fa[x]);
  return fa[x];
}
void newseg(node &x, int pos) {
  int i;
  for (i = 1; i <= n; ++i)
    if (a[i][pos] == a[i - 1][pos])
      x.l[i] = x.r[i] = x.l[i - 1];
    else
      x.l[i] = x.r[i] = ++seg_tot, ++x.sum;
}
void merge(node &x, node lsn, node rsn, int rb, int lb) {
  int i;
  x.sum = lsn.sum + rsn.sum;
  for (i = 1; i <= n; ++i) x.l[i] = lsn.l[i], x.r[i] = rsn.r[i];
  for (i = 1; i <= n; ++i) {
    fa[lsn.l[i]] = lsn.l[i], fa[lsn.r[i]] = lsn.r[i];
    fa[rsn.l[i]] = rsn.l[i], fa[rsn.r[i]] = rsn.r[i];
  }
  for (i = 1; i <= n; ++i)
    if (a[i][rb] == a[i][lb]) {
      int fx = getfa(lsn.r[i]);
      int fy = getfa(rsn.l[i]);
      if (fx != fy) --x.sum, fa[fx] = fy;
    }
  for (i = 1; i <= n; ++i) x.l[i] = getfa(x.l[i]), x.r[i] = getfa(x.r[i]);
}
void build(int l, int r, int rt) {
  if (l == r) {
    newseg(seg[rt], l);
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, rt << 1), build(mid + 1, r, rt << 1 | 1);
  merge(seg[rt], seg[rt << 1], seg[rt << 1 | 1], mid, mid + 1);
}
void query(int l, int r, int rt, int L, int R) {
  if (L <= l && r <= R) {
    if (!ans.l[1])
      ans = seg[rt];
    else
      merge(ans, ans, seg[rt], l - 1, l);
    return;
  }
  int mid = (l + r) >> 1;
  if (L <= mid) query(l, mid, rt << 1, L, R);
  if (R > mid) query(mid + 1, r, rt << 1 | 1, L, R);
}
void init() {
  int i, j;
  scanf("%d%d%d", &n, &m, &q);
  for (i = 1; i <= n; ++i)
    for (j = 1; j <= m; ++j) scanf("%d", &a[i][j]);
}
void work() {
  int i, l, r;
  build(1, m, 1);
  while (q--) {
    for (i = 1; i <= n; ++i) ans.l[i] = 0;
    scanf("%d%d", &l, &r);
    query(1, m, 1, l, r);
    printf("%d\n", ans.sum);
  }
}
int main() {
  init();
  work();
  return 0;
}

```