#include <bits/stdc++.h>
const long long N = 100100;
const long long MX = 10010;
const long long MAGIC = 1000;
const long long mod = 1e9 + 7;
using namespace std;
struct node {
  int cnt;
  pair<int, int> l[11], r[11];
};
int n;
int m;
int q;
int a[11][N];
node ans;
node t[4 * N];
node merg(node x, node y) {
  node v;
  v.cnt = x.cnt + y.cnt;
  for (int i = 1; i <= n; i++) {
    v.l[i] = x.l[i];
    v.r[i] = y.r[i];
  }
  for (int i = 1; i <= n; i++) {
    if (x.r[i].first == y.l[i].first && x.r[i].second != y.l[i].second) {
      v.cnt--;
      int nch = y.l[i].second;
      for (int j = 1; j <= n; j++) {
        if (v.r[j].second == nch) {
          v.r[j].second = x.r[i].second;
        }
        if (v.l[j].second == nch) {
          v.l[j].second = x.r[i].second;
        }
        if (x.r[j].second == nch) {
          x.r[j].second = x.r[i].second;
        }
        if (y.l[j].second == nch) {
          y.l[j].second = x.r[i].second;
        }
      }
    }
  }
  return v;
}
void build(int x, int l, int r) {
  if (l == r) {
    t[x].cnt = n;
    for (int i = 1; i <= n; i++) {
      int p = i * m + l;
      if (i > 1 && a[i][l] == a[i - 1][l]) {
        p = t[x].l[i - 1].second;
        t[x].cnt--;
      }
      t[x].l[i] = t[x].r[i] = {a[i][l], p};
    }
    return;
  }
  int m = (l + r) / 2;
  build(x * 2, l, m);
  build(x * 2 + 1, m + 1, r);
  t[x] = merg(t[x * 2], t[x * 2 + 1]);
}
void get(int x, int l, int r, int tl, int tr) {
  if (tl > tr) {
    return;
  }
  if (l == tl && r == tr) {
    if (ans.cnt == 0) {
      ans = t[x];
    } else {
      ans = merg(ans, t[x]);
    }
    return;
  }
  int m = (l + r) / 2;
  get(x * 2, l, m, tl, min(m, tr));
  get(x * 2 + 1, m + 1, r, max(m + 1, tl), tr);
}
void solve() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  build(1, 1, m);
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    ans.cnt = 0;
    get(1, 1, m, l, r);
    cout << ans.cnt << endl;
  }
}
bool mtest = false;
int main() {
  ios_base::sync_with_stdio(0);
  int TE = 1;
  if (mtest) cin >> TE;
  while (TE--) solve();
  return 0;
}

### Prompt

Please provide a Cpp coded solution to the problem described below:

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>
const long long N = 100100;
const long long MX = 10010;
const long long MAGIC = 1000;
const long long mod = 1e9 + 7;
using namespace std;
struct node {
  int cnt;
  pair<int, int> l[11], r[11];
};
int n;
int m;
int q;
int a[11][N];
node ans;
node t[4 * N];
node merg(node x, node y) {
  node v;
  v.cnt = x.cnt + y.cnt;
  for (int i = 1; i <= n; i++) {
    v.l[i] = x.l[i];
    v.r[i] = y.r[i];
  }
  for (int i = 1; i <= n; i++) {
    if (x.r[i].first == y.l[i].first && x.r[i].second != y.l[i].second) {
      v.cnt--;
      int nch = y.l[i].second;
      for (int j = 1; j <= n; j++) {
        if (v.r[j].second == nch) {
          v.r[j].second = x.r[i].second;
        }
        if (v.l[j].second == nch) {
          v.l[j].second = x.r[i].second;
        }
        if (x.r[j].second == nch) {
          x.r[j].second = x.r[i].second;
        }
        if (y.l[j].second == nch) {
          y.l[j].second = x.r[i].second;
        }
      }
    }
  }
  return v;
}
void build(int x, int l, int r) {
  if (l == r) {
    t[x].cnt = n;
    for (int i = 1; i <= n; i++) {
      int p = i * m + l;
      if (i > 1 && a[i][l] == a[i - 1][l]) {
        p = t[x].l[i - 1].second;
        t[x].cnt--;
      }
      t[x].l[i] = t[x].r[i] = {a[i][l], p};
    }
    return;
  }
  int m = (l + r) / 2;
  build(x * 2, l, m);
  build(x * 2 + 1, m + 1, r);
  t[x] = merg(t[x * 2], t[x * 2 + 1]);
}
void get(int x, int l, int r, int tl, int tr) {
  if (tl > tr) {
    return;
  }
  if (l == tl && r == tr) {
    if (ans.cnt == 0) {
      ans = t[x];
    } else {
      ans = merg(ans, t[x]);
    }
    return;
  }
  int m = (l + r) / 2;
  get(x * 2, l, m, tl, min(m, tr));
  get(x * 2 + 1, m + 1, r, max(m + 1, tl), tr);
}
void solve() {
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> a[i][j];
    }
  }
  build(1, 1, m);
  for (int i = 1; i <= q; i++) {
    int l, r;
    cin >> l >> r;
    ans.cnt = 0;
    get(1, 1, m, l, r);
    cout << ans.cnt << endl;
  }
}
bool mtest = false;
int main() {
  ios_base::sync_with_stdio(0);
  int TE = 1;
  if (mtest) cin >> TE;
  while (TE--) solve();
  return 0;
}

```