#include <bits/stdc++.h>
using namespace std;
struct node {
  int cnt;
  int lft[10], rt[10];
};
const int N = 1e5 + 5;
int n, m, q, l, r;
int grid[11][N];
int par[44];
int vis[44], mp[44], cur;
int get(int i) { return i == par[i] ? i : par[i] = get(par[i]); }
node tree[N << 2];
void merge(node& ret, const node& a, const node& b, int pos) {
  for (int tmp = 0, i = 0; i < 4 * n; i++, tmp = (tmp + 1) % n) {
    if (i < n)
      par[i] = a.lft[tmp];
    else if (i < 2 * n)
      par[i] = a.rt[tmp];
    else if (i < 3 * n)
      par[i] = b.lft[tmp] + 2 * n;
    else
      par[i] = b.rt[tmp] + 2 * n;
  }
  ret.cnt = a.cnt + b.cnt;
  for (int i = 0; i < n; i++) {
    if (grid[i][pos] == grid[i][pos + 1]) {
      int u = get(i + n);
      int v = get(i + 2 * n);
      if ((i + pos) & 1) swap(u, v);
      if (u != v) {
        ret.cnt--;
        par[u] = v;
      }
    }
  }
  cur++;
  for (int i = 0; i < n; i++) {
    int u = get(i);
    if (vis[u] != cur) {
      vis[u] = cur;
      mp[u] = i;
    }
    ret.lft[i] = mp[u];
  }
  for (int i = 0; i < n; i++) {
    int u = get(i + 3 * n);
    if (vis[u] != cur) {
      vis[u] = cur;
      mp[u] = i + n;
    }
    ret.rt[i] = mp[u];
  }
}
void build(int node, int s, int e) {
  if (s == e) {
    tree[node].lft[0] = tree[node].rt[0] = 0;
    tree[node].cnt = 1;
    for (int i = 1; i < n; i++)
      if (grid[i][s] == grid[i - 1][s])
        tree[node].lft[i] = tree[node].rt[i] = tree[node].lft[i - 1];
      else
        tree[node].lft[i] = tree[node].rt[i] = i, tree[node].cnt++;
    return;
  }
  int md = (s + e) >> 1;
  build(node << 1, s, md);
  build(node << 1 | 1, md + 1, e);
  merge(tree[node], tree[node << 1], tree[node << 1 | 1], md);
}
node qry(int nd, int s, int e) {
  if (l <= s && e <= r) return tree[nd];
  if (r <= (s + e) / 2) return qry(nd << 1, s, (s + e) / 2);
  if (l > (s + e) / 2) return qry(nd << 1 | 1, (s + e) / 2 + 1, e);
  node a = qry(nd << 1, s, (s + e) / 2);
  node b = qry(nd << 1 | 1, (s + e) / 2 + 1, e);
  node ret;
  merge(ret, a, b, (s + e) / 2);
  return ret;
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &grid[i][j]);
  build(1, 1, m);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", qry(1, 1, m).cnt);
  }
  return 0;
}

### 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;
struct node {
  int cnt;
  int lft[10], rt[10];
};
const int N = 1e5 + 5;
int n, m, q, l, r;
int grid[11][N];
int par[44];
int vis[44], mp[44], cur;
int get(int i) { return i == par[i] ? i : par[i] = get(par[i]); }
node tree[N << 2];
void merge(node& ret, const node& a, const node& b, int pos) {
  for (int tmp = 0, i = 0; i < 4 * n; i++, tmp = (tmp + 1) % n) {
    if (i < n)
      par[i] = a.lft[tmp];
    else if (i < 2 * n)
      par[i] = a.rt[tmp];
    else if (i < 3 * n)
      par[i] = b.lft[tmp] + 2 * n;
    else
      par[i] = b.rt[tmp] + 2 * n;
  }
  ret.cnt = a.cnt + b.cnt;
  for (int i = 0; i < n; i++) {
    if (grid[i][pos] == grid[i][pos + 1]) {
      int u = get(i + n);
      int v = get(i + 2 * n);
      if ((i + pos) & 1) swap(u, v);
      if (u != v) {
        ret.cnt--;
        par[u] = v;
      }
    }
  }
  cur++;
  for (int i = 0; i < n; i++) {
    int u = get(i);
    if (vis[u] != cur) {
      vis[u] = cur;
      mp[u] = i;
    }
    ret.lft[i] = mp[u];
  }
  for (int i = 0; i < n; i++) {
    int u = get(i + 3 * n);
    if (vis[u] != cur) {
      vis[u] = cur;
      mp[u] = i + n;
    }
    ret.rt[i] = mp[u];
  }
}
void build(int node, int s, int e) {
  if (s == e) {
    tree[node].lft[0] = tree[node].rt[0] = 0;
    tree[node].cnt = 1;
    for (int i = 1; i < n; i++)
      if (grid[i][s] == grid[i - 1][s])
        tree[node].lft[i] = tree[node].rt[i] = tree[node].lft[i - 1];
      else
        tree[node].lft[i] = tree[node].rt[i] = i, tree[node].cnt++;
    return;
  }
  int md = (s + e) >> 1;
  build(node << 1, s, md);
  build(node << 1 | 1, md + 1, e);
  merge(tree[node], tree[node << 1], tree[node << 1 | 1], md);
}
node qry(int nd, int s, int e) {
  if (l <= s && e <= r) return tree[nd];
  if (r <= (s + e) / 2) return qry(nd << 1, s, (s + e) / 2);
  if (l > (s + e) / 2) return qry(nd << 1 | 1, (s + e) / 2 + 1, e);
  node a = qry(nd << 1, s, (s + e) / 2);
  node b = qry(nd << 1 | 1, (s + e) / 2 + 1, e);
  node ret;
  merge(ret, a, b, (s + e) / 2);
  return ret;
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 0; i < n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &grid[i][j]);
  build(1, 1, m);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", qry(1, 1, m).cnt);
  }
  return 0;
}

```