#include <bits/stdc++.h>
using namespace std;
int n, m, q, a[11][100100], f[44], s[44], rk[44];
int getf(int x) {
  if (f[x] == x) return x;
  f[x] = getf(f[x]);
  return f[x];
}
struct node {
  int l[11], r[11], sum, L, R;
  friend node operator+(node A, node B) {
    node C;
    C.sum = A.sum + B.sum;
    C.L = A.L;
    C.R = B.R;
    for (int i = 1; i <= 40; i++) f[i] = i;
    for (int i = 1; i <= n; i++) {
      if (a[i][A.R] == a[i][B.L]) {
        if (getf(A.r[i]) != getf(B.l[i] + 20)) {
          C.sum--;
          f[getf(B.l[i] + 20)] = getf(getf(A.r[i]));
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      s[2 * i - 1] = getf(A.l[i]);
      s[2 * i] = getf(B.r[i] + 20);
    }
    sort(s + 1, s + n + n + 1);
    for (int i = 1; i <= 2 * n; i++) rk[s[i]] = i;
    for (int i = 1; i <= n; i++) {
      C.l[i] = rk[getf(A.l[i])];
      C.r[i] = rk[getf(B.r[i] + 20)];
    }
    return C;
  }
} t[800800], ans;
void build(int x, int l, int r) {
  if (l == r) {
    t[x].sum = 1;
    t[x].L = t[x].R = l;
    t[x].l[1] = t[x].r[1] = 1;
    for (int i = 2; i <= n; i++) {
      t[x].l[i] = t[x].l[i - 1];
      if (a[i][l] != a[i - 1][l]) {
        t[x].l[i]++;
        t[x].sum++;
      }
      t[x].r[i] = t[x].l[i];
    }
    return;
  }
  int mid = (l + r) / 2;
  build(2 * x, l, mid);
  build(2 * x + 1, mid + 1, r);
  t[x] = t[2 * x] + t[2 * x + 1];
}
void dfs(int x, int l, int r, int L, int R) {
  if (L <= l && R >= r) {
    if (ans.sum == 0)
      ans = t[x];
    else
      ans = ans + t[x];
    return;
  }
  int mid = (l + r) / 2;
  if (L <= mid) dfs(2 * x, l, mid, L, R);
  if (R > mid) dfs(2 * x + 1, mid + 1, r, L, R);
}
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, 1, m);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    ans.sum = 0;
    dfs(1, 1, m, l, r);
    printf("%d\n", ans.sum);
  }
  return 0;
}

### Prompt

Please provide a CPP coded solution to the problem described below:

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, a[11][100100], f[44], s[44], rk[44];
int getf(int x) {
  if (f[x] == x) return x;
  f[x] = getf(f[x]);
  return f[x];
}
struct node {
  int l[11], r[11], sum, L, R;
  friend node operator+(node A, node B) {
    node C;
    C.sum = A.sum + B.sum;
    C.L = A.L;
    C.R = B.R;
    for (int i = 1; i <= 40; i++) f[i] = i;
    for (int i = 1; i <= n; i++) {
      if (a[i][A.R] == a[i][B.L]) {
        if (getf(A.r[i]) != getf(B.l[i] + 20)) {
          C.sum--;
          f[getf(B.l[i] + 20)] = getf(getf(A.r[i]));
        }
      }
    }
    for (int i = 1; i <= n; i++) {
      s[2 * i - 1] = getf(A.l[i]);
      s[2 * i] = getf(B.r[i] + 20);
    }
    sort(s + 1, s + n + n + 1);
    for (int i = 1; i <= 2 * n; i++) rk[s[i]] = i;
    for (int i = 1; i <= n; i++) {
      C.l[i] = rk[getf(A.l[i])];
      C.r[i] = rk[getf(B.r[i] + 20)];
    }
    return C;
  }
} t[800800], ans;
void build(int x, int l, int r) {
  if (l == r) {
    t[x].sum = 1;
    t[x].L = t[x].R = l;
    t[x].l[1] = t[x].r[1] = 1;
    for (int i = 2; i <= n; i++) {
      t[x].l[i] = t[x].l[i - 1];
      if (a[i][l] != a[i - 1][l]) {
        t[x].l[i]++;
        t[x].sum++;
      }
      t[x].r[i] = t[x].l[i];
    }
    return;
  }
  int mid = (l + r) / 2;
  build(2 * x, l, mid);
  build(2 * x + 1, mid + 1, r);
  t[x] = t[2 * x] + t[2 * x + 1];
}
void dfs(int x, int l, int r, int L, int R) {
  if (L <= l && R >= r) {
    if (ans.sum == 0)
      ans = t[x];
    else
      ans = ans + t[x];
    return;
  }
  int mid = (l + r) / 2;
  if (L <= mid) dfs(2 * x, l, mid, L, R);
  if (R > mid) dfs(2 * x + 1, mid + 1, r, L, R);
}
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, 1, m);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    ans.sum = 0;
    dfs(1, 1, m, l, r);
    printf("%d\n", ans.sum);
  }
  return 0;
}

```