#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-8;
const int mod = 1e9 + 7;
inline int sign(double x) { return (x > eps) - (x < -eps); }
inline int add(int a, int b) {
  a += b;
  return a < 0 ? a + mod : a >= mod ? a - mod : a;
}
inline int mul(long long a, long long b) { return a * b % mod; }
const int maxn = 1e5 + 10, INF = 0x3f3f3f3f;
int fa[maxn * 11];
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int n, a[20][maxn];
struct node {
  int l, r, v, L[11], R[11];
} tr[maxn << 2];
node merge(const node& x, const node& y) {
  node res;
  res.l = x.l, res.r = y.r, res.v = x.v + y.v;
  for (int i = 1; i <= n; ++i)
    fa[x.L[i]] = x.L[i], fa[x.R[i]] = x.R[i], fa[y.L[i]] = y.L[i],
    fa[y.R[i]] = y.R[i];
  for (int i = 1; i <= n; ++i)
    if (a[i][x.r] == a[i][y.l]) {
      int fx = find(x.R[i]), fy = find(y.L[i]);
      if (fx != fy) fa[fx] = fy, res.v--;
    }
  for (int i = 1; i <= n; ++i) res.L[i] = find(x.L[i]), res.R[i] = find(y.R[i]);
  return res;
}
void build(int l, int r, int rt) {
  if (l == r) {
    tr[rt].l = tr[rt].r = l, tr[rt].v = n;
    for (int i = 1; i <= n; ++i) {
      int id = (l - 1) * n + i;
      if (a[i][l] == a[i - 1][l])
        tr[rt].L[i] = tr[rt].R[i] = tr[rt].L[i - 1], tr[rt].v--;
      else
        tr[rt].L[i] = tr[rt].R[i] = id;
    }
    return;
  }
  int m = l + r >> 1;
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  tr[rt] = merge(tr[rt << 1], tr[rt << 1 | 1]);
}
node qry(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return tr[rt];
  int m = l + r >> 1;
  if (L <= m && R > m)
    return merge(qry(L, R, l, m, rt << 1), qry(L, R, m + 1, r, rt << 1 | 1));
  if (L <= m) return qry(L, R, l, m, rt << 1);
  return qry(L, R, m + 1, r, rt << 1 | 1);
}
int main() {
  int m, q;
  cin >> 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);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", qry(l, r, 1, m, 1).v);
  }
  return 0;
}

### 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 double eps = 1e-8;
const int mod = 1e9 + 7;
inline int sign(double x) { return (x > eps) - (x < -eps); }
inline int add(int a, int b) {
  a += b;
  return a < 0 ? a + mod : a >= mod ? a - mod : a;
}
inline int mul(long long a, long long b) { return a * b % mod; }
const int maxn = 1e5 + 10, INF = 0x3f3f3f3f;
int fa[maxn * 11];
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
int n, a[20][maxn];
struct node {
  int l, r, v, L[11], R[11];
} tr[maxn << 2];
node merge(const node& x, const node& y) {
  node res;
  res.l = x.l, res.r = y.r, res.v = x.v + y.v;
  for (int i = 1; i <= n; ++i)
    fa[x.L[i]] = x.L[i], fa[x.R[i]] = x.R[i], fa[y.L[i]] = y.L[i],
    fa[y.R[i]] = y.R[i];
  for (int i = 1; i <= n; ++i)
    if (a[i][x.r] == a[i][y.l]) {
      int fx = find(x.R[i]), fy = find(y.L[i]);
      if (fx != fy) fa[fx] = fy, res.v--;
    }
  for (int i = 1; i <= n; ++i) res.L[i] = find(x.L[i]), res.R[i] = find(y.R[i]);
  return res;
}
void build(int l, int r, int rt) {
  if (l == r) {
    tr[rt].l = tr[rt].r = l, tr[rt].v = n;
    for (int i = 1; i <= n; ++i) {
      int id = (l - 1) * n + i;
      if (a[i][l] == a[i - 1][l])
        tr[rt].L[i] = tr[rt].R[i] = tr[rt].L[i - 1], tr[rt].v--;
      else
        tr[rt].L[i] = tr[rt].R[i] = id;
    }
    return;
  }
  int m = l + r >> 1;
  build(l, m, rt << 1);
  build(m + 1, r, rt << 1 | 1);
  tr[rt] = merge(tr[rt << 1], tr[rt << 1 | 1]);
}
node qry(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) return tr[rt];
  int m = l + r >> 1;
  if (L <= m && R > m)
    return merge(qry(L, R, l, m, rt << 1), qry(L, R, m + 1, r, rt << 1 | 1));
  if (L <= m) return qry(L, R, l, m, rt << 1);
  return qry(L, R, m + 1, r, rt << 1 | 1);
}
int main() {
  int m, q;
  cin >> 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);
  while (q--) {
    int l, r;
    scanf("%d%d", &l, &r);
    printf("%d\n", qry(l, r, 1, m, 1).v);
  }
  return 0;
}

```