#include <bits/stdc++.h>
using namespace std;
int n, m, q, v[100010][12], num[100010][12], father[1000010], cnt;
struct segtree {
  int l[12], r[12], s;
  segtree() {
    memset(l, 0, sizeof(l));
    memset(r, 0, sizeof(r));
    s = 0;
  }
} t[400010];
int find(int x) {
  if (x == father[x]) return father[x];
  father[x] = find(father[x]);
  return father[x];
}
int merge(int x, int y) {
  int r1 = find(x), r2 = find(y);
  if (r1 != r2) {
    father[r1] = r2;
    return 1;
  }
  return 0;
}
void clear(segtree &now, int p) {
  for (int i = 1; i <= n; i++) father[num[p][i]] = num[p][i];
  now.s = n;
  for (int i = 1; i < n; i++)
    if (v[p][i] == v[p][i + 1]) now.s -= merge(num[p][i], num[p][i + 1]);
  for (int i = 1; i <= n; i++) now.l[i] = now.r[i] = find(num[p][i]);
}
segtree update(segtree lc, segtree rc, int l, int p1, int p2, int r) {
  segtree now;
  now.s = lc.s + rc.s;
  for (int i = 1; i <= n; i++) {
    father[lc.l[i]] = lc.l[i];
    father[lc.r[i]] = lc.r[i];
    father[rc.l[i]] = rc.l[i];
    father[rc.r[i]] = rc.r[i];
  }
  for (int i = 1; i <= n; i++)
    if (v[p1][i] == v[p2][i]) now.s -= merge(rc.l[i], lc.r[i]);
  for (int i = 1; i <= n; i++) {
    now.l[i] = find(lc.l[i]);
    now.r[i] = find(rc.r[i]);
  }
  return now;
}
void build(int i, int l, int r) {
  if (l == r) {
    clear(t[i], l);
    return;
  }
  int mid = (l + r) >> 1;
  build(i << 1, l, mid);
  build((i << 1) + 1, mid + 1, r);
  t[i] = update(t[i << 1], t[(i << 1) + 1], l, mid, mid + 1, r);
}
segtree ask(int i, int l, int r, int left, int right) {
  if (left <= l && right >= r) return t[i];
  int mid = (l + r) >> 1;
  segtree lc, rc;
  lc.s = rc.s = -1;
  if (left <= mid) lc = ask(i << 1, l, mid, left, right);
  if (right > mid) rc = ask((i << 1) + 1, mid + 1, r, left, right);
  if (lc.s == -1) return rc;
  if (rc.s == -1) return lc;
  return update(lc, rc, max(l, left), mid, mid + 1, min(right, 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", &v[j][i]);
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) num[i][j] = ++cnt;
  build(1, 1, m);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", ask(1, 1, m, l, r).s);
  }
  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, v[100010][12], num[100010][12], father[1000010], cnt;
struct segtree {
  int l[12], r[12], s;
  segtree() {
    memset(l, 0, sizeof(l));
    memset(r, 0, sizeof(r));
    s = 0;
  }
} t[400010];
int find(int x) {
  if (x == father[x]) return father[x];
  father[x] = find(father[x]);
  return father[x];
}
int merge(int x, int y) {
  int r1 = find(x), r2 = find(y);
  if (r1 != r2) {
    father[r1] = r2;
    return 1;
  }
  return 0;
}
void clear(segtree &now, int p) {
  for (int i = 1; i <= n; i++) father[num[p][i]] = num[p][i];
  now.s = n;
  for (int i = 1; i < n; i++)
    if (v[p][i] == v[p][i + 1]) now.s -= merge(num[p][i], num[p][i + 1]);
  for (int i = 1; i <= n; i++) now.l[i] = now.r[i] = find(num[p][i]);
}
segtree update(segtree lc, segtree rc, int l, int p1, int p2, int r) {
  segtree now;
  now.s = lc.s + rc.s;
  for (int i = 1; i <= n; i++) {
    father[lc.l[i]] = lc.l[i];
    father[lc.r[i]] = lc.r[i];
    father[rc.l[i]] = rc.l[i];
    father[rc.r[i]] = rc.r[i];
  }
  for (int i = 1; i <= n; i++)
    if (v[p1][i] == v[p2][i]) now.s -= merge(rc.l[i], lc.r[i]);
  for (int i = 1; i <= n; i++) {
    now.l[i] = find(lc.l[i]);
    now.r[i] = find(rc.r[i]);
  }
  return now;
}
void build(int i, int l, int r) {
  if (l == r) {
    clear(t[i], l);
    return;
  }
  int mid = (l + r) >> 1;
  build(i << 1, l, mid);
  build((i << 1) + 1, mid + 1, r);
  t[i] = update(t[i << 1], t[(i << 1) + 1], l, mid, mid + 1, r);
}
segtree ask(int i, int l, int r, int left, int right) {
  if (left <= l && right >= r) return t[i];
  int mid = (l + r) >> 1;
  segtree lc, rc;
  lc.s = rc.s = -1;
  if (left <= mid) lc = ask(i << 1, l, mid, left, right);
  if (right > mid) rc = ask((i << 1) + 1, mid + 1, r, left, right);
  if (lc.s == -1) return rc;
  if (rc.s == -1) return lc;
  return update(lc, rc, max(l, left), mid, mid + 1, min(right, 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", &v[j][i]);
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) num[i][j] = ++cnt;
  build(1, 1, m);
  for (int i = 1; i <= q; i++) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", ask(1, 1, m, l, r).s);
  }
  return 0;
}

```