#include <bits/stdc++.h>
using namespace std;
int n, m, q, l, r, f[11][100001], fa[41];
struct rua {
  int l, r, w, l_fa[11], r_fa[11];
} t[100001 << 2];
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
void Union(int x, int y) { fa[Find(x)] = Find(y); }
void update(rua &x, rua L, rua R) {
  x.l = L.l, x.r = R.r, x.w = L.w + R.w;
  for (int i = 1; i <= n; i++)
    fa[i] = L.l_fa[i], fa[i + n] = L.r_fa[i], fa[i + 2 * n] = R.l_fa[i] + 2 * n,
    fa[i + 3 * n] = R.r_fa[i] + 2 * n;
  for (int i = 1; i <= n; i++)
    if (f[i][L.r] == f[i][R.l])
      if (Find(i + n) != Find(i + 2 * n)) Union(i + n, i + 2 * n), x.w--;
  int tmp[41] = {};
  for (int i = 1; i <= n; i++) {
    x.l_fa[i] = Find(i);
    if (!tmp[x.l_fa[i]]) tmp[x.l_fa[i]] = i;
    x.l_fa[i] = tmp[x.l_fa[i]];
    x.r_fa[i] = Find(i + 3 * n);
    if (!tmp[x.r_fa[i]]) tmp[x.r_fa[i]] = i + n;
    x.r_fa[i] = tmp[x.r_fa[i]];
  }
}
void Build(int l, int r, int x) {
  t[x].l = l, t[x].r = r, t[x].w = (r - l + 1) * n;
  if (l == r) {
    t[x].l_fa[1] = t[x].r_fa[1] = 1;
    for (int i = 2; i <= n; i++)
      if (f[i][l] == f[i - 1][l])
        t[x].l_fa[i] = t[x].r_fa[i] = t[x].l_fa[i - 1], t[x].w--;
      else
        t[x].l_fa[i] = t[x].r_fa[i] = i;
    return;
  }
  int mid = l + r >> 1;
  Build(l, mid, x * 2);
  Build(mid + 1, r, x * 2 + 1);
  update(t[x], t[2 * x], t[2 * x + 1]);
}
rua ask(int L, int R, int x) {
  int l = t[x].l, r = t[x].r;
  if (L == l && r == R) return t[x];
  int mid = l + r >> 1;
  if (R <= mid) return ask(L, R, x * 2);
  if (L > mid) return ask(L, R, x * 2 + 1);
  rua res;
  res.l = L, res.r = R, res.w = (R - L + 1) * n;
  update(res, ask(L, mid, x * 2), ask(mid + 1, R, x * 2 + 1));
  return res;
}
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", &f[i][j]);
  Build(1, m, 1);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", ask(l, r, 1).w);
  }
  return 0;
}

### Prompt

Your task is to create a cpp solution 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;
int n, m, q, l, r, f[11][100001], fa[41];
struct rua {
  int l, r, w, l_fa[11], r_fa[11];
} t[100001 << 2];
int Find(int x) { return fa[x] == x ? x : fa[x] = Find(fa[x]); }
void Union(int x, int y) { fa[Find(x)] = Find(y); }
void update(rua &x, rua L, rua R) {
  x.l = L.l, x.r = R.r, x.w = L.w + R.w;
  for (int i = 1; i <= n; i++)
    fa[i] = L.l_fa[i], fa[i + n] = L.r_fa[i], fa[i + 2 * n] = R.l_fa[i] + 2 * n,
    fa[i + 3 * n] = R.r_fa[i] + 2 * n;
  for (int i = 1; i <= n; i++)
    if (f[i][L.r] == f[i][R.l])
      if (Find(i + n) != Find(i + 2 * n)) Union(i + n, i + 2 * n), x.w--;
  int tmp[41] = {};
  for (int i = 1; i <= n; i++) {
    x.l_fa[i] = Find(i);
    if (!tmp[x.l_fa[i]]) tmp[x.l_fa[i]] = i;
    x.l_fa[i] = tmp[x.l_fa[i]];
    x.r_fa[i] = Find(i + 3 * n);
    if (!tmp[x.r_fa[i]]) tmp[x.r_fa[i]] = i + n;
    x.r_fa[i] = tmp[x.r_fa[i]];
  }
}
void Build(int l, int r, int x) {
  t[x].l = l, t[x].r = r, t[x].w = (r - l + 1) * n;
  if (l == r) {
    t[x].l_fa[1] = t[x].r_fa[1] = 1;
    for (int i = 2; i <= n; i++)
      if (f[i][l] == f[i - 1][l])
        t[x].l_fa[i] = t[x].r_fa[i] = t[x].l_fa[i - 1], t[x].w--;
      else
        t[x].l_fa[i] = t[x].r_fa[i] = i;
    return;
  }
  int mid = l + r >> 1;
  Build(l, mid, x * 2);
  Build(mid + 1, r, x * 2 + 1);
  update(t[x], t[2 * x], t[2 * x + 1]);
}
rua ask(int L, int R, int x) {
  int l = t[x].l, r = t[x].r;
  if (L == l && r == R) return t[x];
  int mid = l + r >> 1;
  if (R <= mid) return ask(L, R, x * 2);
  if (L > mid) return ask(L, R, x * 2 + 1);
  rua res;
  res.l = L, res.r = R, res.w = (R - L + 1) * n;
  update(res, ask(L, mid, x * 2), ask(mid + 1, R, x * 2 + 1));
  return res;
}
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", &f[i][j]);
  Build(1, m, 1);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", ask(l, r, 1).w);
  }
  return 0;
}

```