#include <bits/stdc++.h>
using namespace std;
int n, m, a[11][100005];
struct node_t {
  int lcomp[11], rcomp[11], cnt;
  node_t() {
    memset(lcomp, 0, sizeof(lcomp));
    memset(rcomp, 0, sizeof(rcomp));
    cnt = 0;
  }
  void replace(int x, int y) {
    cnt--;
    for (int i = 1; i <= n; i++) {
      if (lcomp[i] == x) lcomp[i] = y;
      if (rcomp[i] == x) rcomp[i] = y;
    }
  }
};
node_t merge(node_t x, node_t y, int mid) {
  node_t res;
  res.cnt = x.cnt + y.cnt;
  for (int i = 1; i <= n; i++)
    res.lcomp[i] = x.lcomp[i], res.rcomp[i] = y.rcomp[i];
  for (int i = 1; i <= n; i++)
    if (a[i][mid] == a[i][mid + 1] && x.rcomp[i] != y.lcomp[i])
      res.replace(x.rcomp[i], y.lcomp[i]), x.replace(x.rcomp[i], y.lcomp[i]);
  return res;
}
node_t tree[100005 << 2];
void build(int node = 1, int l = 1, int r = m) {
  if (l == r) {
    tree[node].cnt = n;
    for (int i = 1; i <= n; i++)
      tree[node].lcomp[i] = tree[node].rcomp[i] = (l - 1) * n + i;
    for (int i = 2; i <= n; i++)
      if (a[i][l] == a[i - 1][l])
        tree[node].replace(tree[node].lcomp[i], tree[node].lcomp[i - 1]);
    return;
  }
  int mid = (l + r) >> 1;
  build((node << 1), l, mid);
  build(((node << 1) | 1), mid + 1, r);
  tree[node] = merge(tree[(node << 1)], tree[((node << 1) | 1)], mid);
}
node_t query(int ql, int qr, int node = 1, int l = 1, int r = m) {
  if (ql <= l && r <= qr) return tree[node];
  int mid = (l + r) >> 1;
  if (mid >= qr) return query(ql, qr, (node << 1), l, mid);
  if (mid < ql) return query(ql, qr, ((node << 1) | 1), mid + 1, r);
  return merge(query(ql, qr, (node << 1), l, mid),
               query(ql, qr, ((node << 1) | 1), mid + 1, r), mid);
}
int main() {
  int q;
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  build();
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    printf("%d\n", query(l, r).cnt);
  }
}

### Prompt

Your task is to create 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;
int n, m, a[11][100005];
struct node_t {
  int lcomp[11], rcomp[11], cnt;
  node_t() {
    memset(lcomp, 0, sizeof(lcomp));
    memset(rcomp, 0, sizeof(rcomp));
    cnt = 0;
  }
  void replace(int x, int y) {
    cnt--;
    for (int i = 1; i <= n; i++) {
      if (lcomp[i] == x) lcomp[i] = y;
      if (rcomp[i] == x) rcomp[i] = y;
    }
  }
};
node_t merge(node_t x, node_t y, int mid) {
  node_t res;
  res.cnt = x.cnt + y.cnt;
  for (int i = 1; i <= n; i++)
    res.lcomp[i] = x.lcomp[i], res.rcomp[i] = y.rcomp[i];
  for (int i = 1; i <= n; i++)
    if (a[i][mid] == a[i][mid + 1] && x.rcomp[i] != y.lcomp[i])
      res.replace(x.rcomp[i], y.lcomp[i]), x.replace(x.rcomp[i], y.lcomp[i]);
  return res;
}
node_t tree[100005 << 2];
void build(int node = 1, int l = 1, int r = m) {
  if (l == r) {
    tree[node].cnt = n;
    for (int i = 1; i <= n; i++)
      tree[node].lcomp[i] = tree[node].rcomp[i] = (l - 1) * n + i;
    for (int i = 2; i <= n; i++)
      if (a[i][l] == a[i - 1][l])
        tree[node].replace(tree[node].lcomp[i], tree[node].lcomp[i - 1]);
    return;
  }
  int mid = (l + r) >> 1;
  build((node << 1), l, mid);
  build(((node << 1) | 1), mid + 1, r);
  tree[node] = merge(tree[(node << 1)], tree[((node << 1) | 1)], mid);
}
node_t query(int ql, int qr, int node = 1, int l = 1, int r = m) {
  if (ql <= l && r <= qr) return tree[node];
  int mid = (l + r) >> 1;
  if (mid >= qr) return query(ql, qr, (node << 1), l, mid);
  if (mid < ql) return query(ql, qr, ((node << 1) | 1), mid + 1, r);
  return merge(query(ql, qr, (node << 1), l, mid),
               query(ql, qr, ((node << 1) | 1), mid + 1, r), mid);
}
int main() {
  int q;
  scanf("%d %d %d", &n, &m, &q);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) scanf("%d", &a[i][j]);
  build();
  while (q--) {
    int l, r;
    scanf("%d %d", &l, &r);
    printf("%d\n", query(l, r).cnt);
  }
}

```