#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 8;
const int M = 20;
struct node {
  int sum;
  int a[M];
  int b[M];
} T[N << 2];
int a[M][N];
int f[N], id[N];
int n, m, q;
int find(int x) {
  if (f[x] == x) return x;
  return f[x] = find(f[x]);
}
void merge(node &rt, node &l, node &r, int lpos, int rpos) {
  rt.sum = l.sum + r.sum;
  for (int i = 0; i < m; ++i) {
    f[i] = l.a[i];
    f[i + m] = l.b[i];
    f[i + m * 2] = r.a[i] + m * 2;
    f[i + m * 3] = r.b[i] + m * 2;
  }
  for (int i = 0; i < m; ++i) {
    if (a[i][lpos] == a[i][rpos] && f[find(i + m * 2)] != f[find(i + m)]) {
      f[find(i + m * 2)] = f[find(i + m)];
      rt.sum--;
    }
  }
  for (int i = 0; i < m * 4; ++i) f[i] = find(f[i]), id[f[i]] = -1;
  for (int i = 0; i < m; ++i) {
    if (id[f[i]] == -1) id[f[i]] = i;
    rt.a[i] = id[f[i]];
  }
  for (int i = m * 3; i < m * 4; ++i) {
    if (id[f[i]] == -1) id[f[i]] = i - m * 2;
    rt.b[i - m * 3] = id[f[i]];
  }
}
void query(int L, int R, int l, int r, int rt, node &ans) {
  if (L <= l && r <= R) {
    ans = T[rt];
    return;
  }
  int mid = l + r >> 1;
  if (R <= mid) {
    query(L, R, l, mid, rt << 1, ans);
    return;
  }
  if (mid < L) {
    query(L, R, mid + 1, r, rt << 1 | 1, ans);
    return;
  }
  node p, q;
  query(L, R, l, mid, rt << 1, p);
  query(L, R, mid + 1, r, rt << 1 | 1, q);
  merge(ans, p, q, mid, mid + 1);
}
void build(int l, int r, int rt) {
  if (l == r) {
    T[rt].sum = 0;
    for (int i = 0; i < m; ++i)
      if (i == 0 || a[i][l] != a[i - 1][l]) {
        T[rt].a[i] = T[rt].b[i] = i;
        T[rt].sum++;
      } else {
        T[rt].a[i] = T[rt].b[i] = T[rt].a[i - 1];
      }
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
  merge(T[rt], T[rt << 1], T[rt << 1 | 1], mid, mid + 1);
}
int main() {
  cin.sync_with_stdio(0);
  cin >> m >> n >> q;
  for (int i = 0; i < m; ++i)
    for (int j = 1; j <= n; ++j) cin >> a[i][j];
  build(1, n, 1);
  node ans;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    query(l, r, 1, n, 1, ans);
    cout << ans.sum << endl;
  }
}

### Prompt

Please create a solution in Cpp 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;
const int N = 1e5 + 8;
const int M = 20;
struct node {
  int sum;
  int a[M];
  int b[M];
} T[N << 2];
int a[M][N];
int f[N], id[N];
int n, m, q;
int find(int x) {
  if (f[x] == x) return x;
  return f[x] = find(f[x]);
}
void merge(node &rt, node &l, node &r, int lpos, int rpos) {
  rt.sum = l.sum + r.sum;
  for (int i = 0; i < m; ++i) {
    f[i] = l.a[i];
    f[i + m] = l.b[i];
    f[i + m * 2] = r.a[i] + m * 2;
    f[i + m * 3] = r.b[i] + m * 2;
  }
  for (int i = 0; i < m; ++i) {
    if (a[i][lpos] == a[i][rpos] && f[find(i + m * 2)] != f[find(i + m)]) {
      f[find(i + m * 2)] = f[find(i + m)];
      rt.sum--;
    }
  }
  for (int i = 0; i < m * 4; ++i) f[i] = find(f[i]), id[f[i]] = -1;
  for (int i = 0; i < m; ++i) {
    if (id[f[i]] == -1) id[f[i]] = i;
    rt.a[i] = id[f[i]];
  }
  for (int i = m * 3; i < m * 4; ++i) {
    if (id[f[i]] == -1) id[f[i]] = i - m * 2;
    rt.b[i - m * 3] = id[f[i]];
  }
}
void query(int L, int R, int l, int r, int rt, node &ans) {
  if (L <= l && r <= R) {
    ans = T[rt];
    return;
  }
  int mid = l + r >> 1;
  if (R <= mid) {
    query(L, R, l, mid, rt << 1, ans);
    return;
  }
  if (mid < L) {
    query(L, R, mid + 1, r, rt << 1 | 1, ans);
    return;
  }
  node p, q;
  query(L, R, l, mid, rt << 1, p);
  query(L, R, mid + 1, r, rt << 1 | 1, q);
  merge(ans, p, q, mid, mid + 1);
}
void build(int l, int r, int rt) {
  if (l == r) {
    T[rt].sum = 0;
    for (int i = 0; i < m; ++i)
      if (i == 0 || a[i][l] != a[i - 1][l]) {
        T[rt].a[i] = T[rt].b[i] = i;
        T[rt].sum++;
      } else {
        T[rt].a[i] = T[rt].b[i] = T[rt].a[i - 1];
      }
    return;
  }
  int mid = l + r >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
  merge(T[rt], T[rt << 1], T[rt << 1 | 1], mid, mid + 1);
}
int main() {
  cin.sync_with_stdio(0);
  cin >> m >> n >> q;
  for (int i = 0; i < m; ++i)
    for (int j = 1; j <= n; ++j) cin >> a[i][j];
  build(1, n, 1);
  node ans;
  for (int i = 0; i < q; ++i) {
    int l, r;
    cin >> l >> r;
    query(l, r, 1, n, 1, ans);
    cout << ans.sum << endl;
  }
}

```