#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 1;
const int N = 10;
int n, m, a[N][M];
pair<int, int> par[N][M];
struct Node {
  pair<int, int> par[N][2];
  int l, r, cnt = 0;
} seg[M << 2];
void buildCol(int j, int id) {
  seg[id].cnt = 0;
  for (int i = 0; i < n; i++) {
    seg[id].par[i][0] = seg[id].par[i][1] = {i, j};
    seg[id].cnt++;
    if (i && a[i][j] == a[i - 1][j])
      seg[id].par[i][0] = seg[id].par[i][1] = seg[id].par[i - 1][0],
      seg[id].cnt--;
  }
  seg[id].l = seg[id].r = j;
}
ostream& operator<<(ostream& s, const pair<int, int>& p) {
  return s << "(" << p.first << ", " << p.second << ')';
}
pair<int, int> root(pair<int, int> p) {
  return par[p.first][p.second] == p
             ? p
             : par[p.first][p.second] = root(par[p.first][p.second]);
}
int merge(pair<int, int> p1, pair<int, int> p2) {
  if ((p1 = root(p1)) == (p2 = root(p2))) return 0;
  par[p1.first][p1.second] = p2;
  return -1;
};
void SS(pair<int, int> p) {
  pair<int, int> pp = par[p.first][p.second];
  par[pp.first][pp.second] = pp;
}
Node merge(Node A, Node B) {
  Node ans;
  ans.cnt = A.cnt + B.cnt;
  ans.l = A.l, ans.r = B.r;
  for (int i = 0; i < n; i++) {
    par[i][A.l] = A.par[i][0];
    par[i][A.r] = A.par[i][1];
    par[i][B.l] = B.par[i][0];
    par[i][B.r] = B.par[i][1];
    SS({i, A.l});
    SS({i, A.r});
    SS({i, B.r});
    SS({i, B.l});
  }
  for (int i = 0; i < n; i++)
    if (a[i][A.r] == a[i][B.l]) ans.cnt += merge({i, A.r}, {i, B.l});
  for (int i = 0; i < n; i++) {
    ans.par[i][0] = root({i, A.l});
    ans.par[i][1] = root({i, B.r});
  }
  return ans;
}
void build(int id = 1, int l = 0, int r = m - 1) {
  if (l == r) {
    buildCol(l, id);
    return;
  }
  int mid = l + r >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
  seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
Node get(int L, int R, int id = 1, int l = 0, int r = m - 1) {
  if (L <= l && r <= R) return seg[id];
  int mid = l + r >> 1;
  if (R <= mid) return get(L, R, id << 1, l, mid);
  if (L > mid) return get(L, R, id << 1 | 1, mid + 1, r);
  return merge(get(L, mid, id << 1, l, mid),
               get(mid + 1, R, id << 1 | 1, mid + 1, r));
}
int main() {
  int q;
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
  build();
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    --l, --r;
    printf("%d\n", get(l, r).cnt);
  }
}

### Prompt

Construct a cpp code solution to the problem outlined:

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 M = 1e5 + 1;
const int N = 10;
int n, m, a[N][M];
pair<int, int> par[N][M];
struct Node {
  pair<int, int> par[N][2];
  int l, r, cnt = 0;
} seg[M << 2];
void buildCol(int j, int id) {
  seg[id].cnt = 0;
  for (int i = 0; i < n; i++) {
    seg[id].par[i][0] = seg[id].par[i][1] = {i, j};
    seg[id].cnt++;
    if (i && a[i][j] == a[i - 1][j])
      seg[id].par[i][0] = seg[id].par[i][1] = seg[id].par[i - 1][0],
      seg[id].cnt--;
  }
  seg[id].l = seg[id].r = j;
}
ostream& operator<<(ostream& s, const pair<int, int>& p) {
  return s << "(" << p.first << ", " << p.second << ')';
}
pair<int, int> root(pair<int, int> p) {
  return par[p.first][p.second] == p
             ? p
             : par[p.first][p.second] = root(par[p.first][p.second]);
}
int merge(pair<int, int> p1, pair<int, int> p2) {
  if ((p1 = root(p1)) == (p2 = root(p2))) return 0;
  par[p1.first][p1.second] = p2;
  return -1;
};
void SS(pair<int, int> p) {
  pair<int, int> pp = par[p.first][p.second];
  par[pp.first][pp.second] = pp;
}
Node merge(Node A, Node B) {
  Node ans;
  ans.cnt = A.cnt + B.cnt;
  ans.l = A.l, ans.r = B.r;
  for (int i = 0; i < n; i++) {
    par[i][A.l] = A.par[i][0];
    par[i][A.r] = A.par[i][1];
    par[i][B.l] = B.par[i][0];
    par[i][B.r] = B.par[i][1];
    SS({i, A.l});
    SS({i, A.r});
    SS({i, B.r});
    SS({i, B.l});
  }
  for (int i = 0; i < n; i++)
    if (a[i][A.r] == a[i][B.l]) ans.cnt += merge({i, A.r}, {i, B.l});
  for (int i = 0; i < n; i++) {
    ans.par[i][0] = root({i, A.l});
    ans.par[i][1] = root({i, B.r});
  }
  return ans;
}
void build(int id = 1, int l = 0, int r = m - 1) {
  if (l == r) {
    buildCol(l, id);
    return;
  }
  int mid = l + r >> 1;
  build(id << 1, l, mid);
  build(id << 1 | 1, mid + 1, r);
  seg[id] = merge(seg[id << 1], seg[id << 1 | 1]);
}
Node get(int L, int R, int id = 1, int l = 0, int r = m - 1) {
  if (L <= l && r <= R) return seg[id];
  int mid = l + r >> 1;
  if (R <= mid) return get(L, R, id << 1, l, mid);
  if (L > mid) return get(L, R, id << 1 | 1, mid + 1, r);
  return merge(get(L, mid, id << 1, l, mid),
               get(mid + 1, R, id << 1 | 1, mid + 1, r));
}
int main() {
  int q;
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) scanf("%d", &a[i][j]);
  build();
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    --l, --r;
    printf("%d\n", get(l, r).cnt);
  }
}

```