#include <bits/stdc++.h>
using namespace std;
const int maxk = 10 + 2;
const int maxn = 1e5 + 17;
int N, M, Q;
int in[maxk][maxn];
int msz;
int mp[4 * maxk];
int par[4 * maxk];
inline int find(int x) { return par[x] = x == par[x] ? x : find(par[x]); }
struct Node {
  int l, r;
  int lv[maxk], rv[maxk];
  int size;
  Node() {}
  Node(int j) {
    l = r = j;
    size = 0;
    for (int i = 0; i < N; i++)
      lv[i] = rv[i] = !i || in[i][j] != in[i - 1][j] ? ++size : size;
  }
  Node operator+(const Node &rn) const {
    Node ret = Node();
    ret.l = l;
    ret.r = rn.r;
    ret.size = size + rn.size;
    int offset = 20;
    for (int i = 0; i < 4 * maxk; i++) par[i] = i;
    for (int i = 0; i < N; i++)
      if (in[i][r] == in[i][rn.l]) {
        int x = rv[i], y = rn.lv[i] + offset;
        if (find(x) == find(y)) continue;
        par[find(x)] = find(y);
        ret.size--;
      }
    msz = 0;
    for (int i = 0; i < 4 * maxk; i++) mp[i] = 0;
    for (int i = 0; i < N; i++) {
      int li = find(lv[i]), ri = find(rn.rv[i] + offset);
      if (!mp[li]) mp[li] = ++msz;
      if (!mp[ri]) mp[ri] = ++msz;
      ret.lv[i] = mp[li];
      ret.rv[i] = mp[ri];
    }
    return ret;
  }
};
vector<Node> QR;
Node seg[4 * maxn];
void build(int x, int l, int r) {
  if (l > r) return;
  if (l == r) {
    seg[x] = Node(l);
    return;
  }
  build(((x) << 1), l, ((l) + (r) >> 1));
  build(((x) << 1 | 1), ((l) + (r) >> 1) + 1, r);
  seg[x] = seg[((x) << 1)] + seg[((x) << 1 | 1)];
}
void _query(int x, int l, int r, int i, int j) {
  if (l > r || l > j || r < i) return;
  if (i <= l && r <= j) {
    QR.push_back(seg[x]);
    return;
  }
  _query(((x) << 1), l, ((l) + (r) >> 1), i, j);
  _query(((x) << 1 | 1), ((l) + (r) >> 1) + 1, r, i, j);
}
int query(int l, int r) {
  QR.clear();
  _query(1, 1, M, l, r);
  for (int i = 1; i < QR.size(); i++) QR[0] = QR[0] + QR[i];
  return QR[0].size;
}
void print(int i) {
  cout << i << endl;
  auto x = seg[i];
  cout << "SZ: " << x.size << endl;
  cout << "L: " << x.l << endl;
  cout << "R: " << x.r << endl;
  cout << "LV: ";
  for (int i = 0; i < N; i++) cout << x.lv[i] << " ";
  cout << endl;
  cout << "RV: ";
  for (int i = 0; i < N; i++) cout << x.rv[i] << " ";
  cout << endl;
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M >> Q;
  for (int i = 0; i < N; i++)
    for (int j = 1; j <= M; j++) cin >> in[i][j];
  build(1, 1, M);
  while (Q--) {
    int l, r;
    cin >> l >> r;
    cout << query(l, r) << " ";
  }
  return 0;
}

### Prompt

Generate 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;
const int maxk = 10 + 2;
const int maxn = 1e5 + 17;
int N, M, Q;
int in[maxk][maxn];
int msz;
int mp[4 * maxk];
int par[4 * maxk];
inline int find(int x) { return par[x] = x == par[x] ? x : find(par[x]); }
struct Node {
  int l, r;
  int lv[maxk], rv[maxk];
  int size;
  Node() {}
  Node(int j) {
    l = r = j;
    size = 0;
    for (int i = 0; i < N; i++)
      lv[i] = rv[i] = !i || in[i][j] != in[i - 1][j] ? ++size : size;
  }
  Node operator+(const Node &rn) const {
    Node ret = Node();
    ret.l = l;
    ret.r = rn.r;
    ret.size = size + rn.size;
    int offset = 20;
    for (int i = 0; i < 4 * maxk; i++) par[i] = i;
    for (int i = 0; i < N; i++)
      if (in[i][r] == in[i][rn.l]) {
        int x = rv[i], y = rn.lv[i] + offset;
        if (find(x) == find(y)) continue;
        par[find(x)] = find(y);
        ret.size--;
      }
    msz = 0;
    for (int i = 0; i < 4 * maxk; i++) mp[i] = 0;
    for (int i = 0; i < N; i++) {
      int li = find(lv[i]), ri = find(rn.rv[i] + offset);
      if (!mp[li]) mp[li] = ++msz;
      if (!mp[ri]) mp[ri] = ++msz;
      ret.lv[i] = mp[li];
      ret.rv[i] = mp[ri];
    }
    return ret;
  }
};
vector<Node> QR;
Node seg[4 * maxn];
void build(int x, int l, int r) {
  if (l > r) return;
  if (l == r) {
    seg[x] = Node(l);
    return;
  }
  build(((x) << 1), l, ((l) + (r) >> 1));
  build(((x) << 1 | 1), ((l) + (r) >> 1) + 1, r);
  seg[x] = seg[((x) << 1)] + seg[((x) << 1 | 1)];
}
void _query(int x, int l, int r, int i, int j) {
  if (l > r || l > j || r < i) return;
  if (i <= l && r <= j) {
    QR.push_back(seg[x]);
    return;
  }
  _query(((x) << 1), l, ((l) + (r) >> 1), i, j);
  _query(((x) << 1 | 1), ((l) + (r) >> 1) + 1, r, i, j);
}
int query(int l, int r) {
  QR.clear();
  _query(1, 1, M, l, r);
  for (int i = 1; i < QR.size(); i++) QR[0] = QR[0] + QR[i];
  return QR[0].size;
}
void print(int i) {
  cout << i << endl;
  auto x = seg[i];
  cout << "SZ: " << x.size << endl;
  cout << "L: " << x.l << endl;
  cout << "R: " << x.r << endl;
  cout << "LV: ";
  for (int i = 0; i < N; i++) cout << x.lv[i] << " ";
  cout << endl;
  cout << "RV: ";
  for (int i = 0; i < N; i++) cout << x.rv[i] << " ";
  cout << endl;
  cout << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> N >> M >> Q;
  for (int i = 0; i < N; i++)
    for (int j = 1; j <= M; j++) cin >> in[i][j];
  build(1, 1, M);
  while (Q--) {
    int l, r;
    cin >> l >> r;
    cout << query(l, r) << " ";
  }
  return 0;
}

```