#include <bits/stdc++.h>
using namespace std;
const int N = 10 + 2, M = 1e5 + 5;
int n, m, q, a[N][M], par[N << 2], col[N << 2];
int get_par(int u) { return ~par[u] ? par[u] = get_par(par[u]) : u; }
bool merge(int u, int v) {
  u = get_par(u), v = get_par(v);
  if (u == v) return false;
  par[u] = v;
  return true;
}
struct node {
  int cnt;
  int A[N], B[N];
  int L[N], R[N];
  node() {
    memset(A, 0, sizeof A);
    memset(B, 0, sizeof B);
    iota(L, L + n, 1);
    iota(R, R + n, 1);
    cnt = 0;
  }
  friend node merge(node a, node b) {
    if (!a.cnt || !b.cnt) return a.cnt ? a : b;
    node ans;
    ans.cnt = a.cnt + b.cnt;
    memset(par, -1, sizeof par);
    memset(col, -1, sizeof col);
    for (int i = 0; i < n; i++)
      if (a.B[i] == b.A[i]) ans.cnt -= merge(a.R[i], b.L[i] + 20);
    for (int i = 0; i < n; i++) {
      a.L[i] = get_par(a.L[i]);
      b.R[i] = get_par(b.R[i] + 20);
    }
    for (int i = 0, tmp = 0; i < n; i++) {
      ans.A[i] = a.A[i];
      ans.B[i] = b.B[i];
      if (!~col[a.L[i]]) col[a.L[i]] = tmp++;
      if (!~col[b.R[i]]) col[b.R[i]] = tmp++;
      ans.L[i] = col[a.L[i]];
      ans.R[i] = col[b.R[i]];
    }
    return ans;
  }
} seg[M << 2];
void build(int id = 1, int st = 0, int en = m) {
  if (en - st == 1) {
    for (int i = 0; i < n; i++) {
      seg[id].cnt += (!i || a[i][st] ^ a[i - 1][st]);
      seg[id].L[i] = seg[id].R[i] = seg[id].cnt - 1;
      seg[id].A[i] = seg[id].B[i] = a[i][st];
    }
    return;
  }
  int mid = st + en >> 1;
  build(id << 1, st, mid);
  build(id << 1 | 1, mid, en);
  seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
node get(int l, int r, int id = 1, int st = 0, int en = m) {
  if (r <= st || en <= l) return node();
  if (l <= st && en <= r) return seg[id];
  int mid = st + en >> 1;
  return merge(get(l, r, id << 1, st, mid), get(l, r, id << 1 | 1, mid, en));
}
void readInput() {
  cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
}
void writeOutput() {
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << get(l - 1, r).cnt << endl;
  }
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  readInput(), build(), writeOutput();
  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 = 10 + 2, M = 1e5 + 5;
int n, m, q, a[N][M], par[N << 2], col[N << 2];
int get_par(int u) { return ~par[u] ? par[u] = get_par(par[u]) : u; }
bool merge(int u, int v) {
  u = get_par(u), v = get_par(v);
  if (u == v) return false;
  par[u] = v;
  return true;
}
struct node {
  int cnt;
  int A[N], B[N];
  int L[N], R[N];
  node() {
    memset(A, 0, sizeof A);
    memset(B, 0, sizeof B);
    iota(L, L + n, 1);
    iota(R, R + n, 1);
    cnt = 0;
  }
  friend node merge(node a, node b) {
    if (!a.cnt || !b.cnt) return a.cnt ? a : b;
    node ans;
    ans.cnt = a.cnt + b.cnt;
    memset(par, -1, sizeof par);
    memset(col, -1, sizeof col);
    for (int i = 0; i < n; i++)
      if (a.B[i] == b.A[i]) ans.cnt -= merge(a.R[i], b.L[i] + 20);
    for (int i = 0; i < n; i++) {
      a.L[i] = get_par(a.L[i]);
      b.R[i] = get_par(b.R[i] + 20);
    }
    for (int i = 0, tmp = 0; i < n; i++) {
      ans.A[i] = a.A[i];
      ans.B[i] = b.B[i];
      if (!~col[a.L[i]]) col[a.L[i]] = tmp++;
      if (!~col[b.R[i]]) col[b.R[i]] = tmp++;
      ans.L[i] = col[a.L[i]];
      ans.R[i] = col[b.R[i]];
    }
    return ans;
  }
} seg[M << 2];
void build(int id = 1, int st = 0, int en = m) {
  if (en - st == 1) {
    for (int i = 0; i < n; i++) {
      seg[id].cnt += (!i || a[i][st] ^ a[i - 1][st]);
      seg[id].L[i] = seg[id].R[i] = seg[id].cnt - 1;
      seg[id].A[i] = seg[id].B[i] = a[i][st];
    }
    return;
  }
  int mid = st + en >> 1;
  build(id << 1, st, mid);
  build(id << 1 | 1, mid, en);
  seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
node get(int l, int r, int id = 1, int st = 0, int en = m) {
  if (r <= st || en <= l) return node();
  if (l <= st && en <= r) return seg[id];
  int mid = st + en >> 1;
  return merge(get(l, r, id << 1, st, mid), get(l, r, id << 1 | 1, mid, en));
}
void readInput() {
  cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> a[i][j];
}
void writeOutput() {
  for (int i = 0; i < q; i++) {
    int l, r;
    cin >> l >> r;
    cout << get(l - 1, r).cnt << endl;
  }
}
int main() {
  ios_base ::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  readInput(), build(), writeOutput();
  return 0;
}

```