#include <bits/stdc++.h>
using namespace std;
const int N = 15;
const int M = 100005;
struct Seg {
  int l, r;
  int ls[N], rs[N], sum;
  Seg() {
    l = r = sum = 0;
    memset(ls, 0, sizeof(ls));
    memset(rs, 0, sizeof(rs));
  }
} t[M << 2];
int a[N][M], fa[N * M], n, m, cnt;
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(Seg x, Seg y, Seg& z) {
  if (!z.l) {
    if (!y.l)
      z = x;
    else
      z = y;
    return;
  }
  z.l = x.l, z.r = y.r;
  z.sum = x.sum + y.sum;
  for (int i = 1; i <= n; i++) z.ls[i] = x.ls[i], z.rs[i] = y.rs[i];
  for (int i = 1; i <= n; i++) {
    fa[x.ls[i]] = x.ls[i], fa[y.ls[i]] = y.ls[i];
    fa[x.rs[i]] = x.rs[i], fa[y.rs[i]] = y.rs[i];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i][x.r] == a[i][y.l]) {
      int fx = find(x.rs[i]), fy = find(y.ls[i]);
      if (fx != fy) fa[fx] = fy, z.sum--;
    }
  }
  for (int i = 1; i <= n; i++) z.ls[i] = find(z.ls[i]), z.rs[i] = find(z.rs[i]);
}
void build(int l, int r, int p) {
  t[p].l = l, t[p].r = r;
  if (l == r) {
    int s = cnt;
    for (int i = 1; i <= n; i++) {
      if (a[i][l] == a[i - 1][l]) {
        t[p].ls[i] = t[p].rs[i] = t[p].ls[i - 1];
      } else
        t[p].ls[i] = t[p].rs[i] = ++cnt;
    }
    t[p].sum = cnt - s;
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, p * 2);
  build(mid + 1, r, p * 2 + 1);
  merge(t[p * 2], t[p * 2 + 1], t[p]);
}
Seg query(int l, int r, int p) {
  if (l <= t[p].l && r >= t[p].r) return t[p];
  Seg ans;
  int mid = (t[p].l + t[p].r) >> 1;
  if (l <= mid) merge(query(l, r, p * 2), ans, ans);
  if (r > mid) merge(ans, query(l, r, p * 2 + 1), ans);
  return ans;
}
int main() {
  int q;
  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", query(l, r, 1).sum);
  }
}

### Prompt

Create a solution in CPP for 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 = 15;
const int M = 100005;
struct Seg {
  int l, r;
  int ls[N], rs[N], sum;
  Seg() {
    l = r = sum = 0;
    memset(ls, 0, sizeof(ls));
    memset(rs, 0, sizeof(rs));
  }
} t[M << 2];
int a[N][M], fa[N * M], n, m, cnt;
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
void merge(Seg x, Seg y, Seg& z) {
  if (!z.l) {
    if (!y.l)
      z = x;
    else
      z = y;
    return;
  }
  z.l = x.l, z.r = y.r;
  z.sum = x.sum + y.sum;
  for (int i = 1; i <= n; i++) z.ls[i] = x.ls[i], z.rs[i] = y.rs[i];
  for (int i = 1; i <= n; i++) {
    fa[x.ls[i]] = x.ls[i], fa[y.ls[i]] = y.ls[i];
    fa[x.rs[i]] = x.rs[i], fa[y.rs[i]] = y.rs[i];
  }
  for (int i = 1; i <= n; i++) {
    if (a[i][x.r] == a[i][y.l]) {
      int fx = find(x.rs[i]), fy = find(y.ls[i]);
      if (fx != fy) fa[fx] = fy, z.sum--;
    }
  }
  for (int i = 1; i <= n; i++) z.ls[i] = find(z.ls[i]), z.rs[i] = find(z.rs[i]);
}
void build(int l, int r, int p) {
  t[p].l = l, t[p].r = r;
  if (l == r) {
    int s = cnt;
    for (int i = 1; i <= n; i++) {
      if (a[i][l] == a[i - 1][l]) {
        t[p].ls[i] = t[p].rs[i] = t[p].ls[i - 1];
      } else
        t[p].ls[i] = t[p].rs[i] = ++cnt;
    }
    t[p].sum = cnt - s;
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, p * 2);
  build(mid + 1, r, p * 2 + 1);
  merge(t[p * 2], t[p * 2 + 1], t[p]);
}
Seg query(int l, int r, int p) {
  if (l <= t[p].l && r >= t[p].r) return t[p];
  Seg ans;
  int mid = (t[p].l + t[p].r) >> 1;
  if (l <= mid) merge(query(l, r, p * 2), ans, ans);
  if (r > mid) merge(ans, query(l, r, p * 2 + 1), ans);
  return ans;
}
int main() {
  int q;
  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", query(l, r, 1).sum);
  }
}

```