#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 7;
struct node {
  int ls[11], rs[11], sum;
} tree[maxn * 4];
int n, m, q, x, y, mp[11][maxn], tot;
int fa[maxn * 10];
int find_set(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = find_set(fa[x]);
}
void union_set(int x, int y) {
  int fx = find_set(x), fy = find_set(y);
  if (fx != fy) {
    fa[fx] = fy;
  }
}
node pushup(node a, node b, int mid) {
  node res;
  res.sum = a.sum + b.sum;
  for (int i = 1; i <= n; i++) {
    fa[a.ls[i]] = a.ls[i];
    fa[b.ls[i]] = b.ls[i];
    fa[a.rs[i]] = a.rs[i];
    fa[b.rs[i]] = b.rs[i];
  }
  for (int i = 1; i <= n; i++) {
    if (mp[i][mid] == mp[i][mid + 1]) {
      if (find_set(a.rs[i]) != find_set(b.ls[i])) {
        res.sum--;
        union_set(a.rs[i], b.ls[i]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    res.ls[i] = find_set(a.ls[i]);
    res.rs[i] = find_set(b.rs[i]);
  }
  return res;
}
void build(int l, int r, int rt) {
  if (l == r) {
    for (int i = 1; i <= n; i++) {
      if (mp[i - 1][l] == mp[i][l])
        tree[rt].ls[i] = tree[rt].rs[i] = tree[rt].ls[i - 1];
      else
        tree[rt].ls[i] = tree[rt].rs[i] = ++tot, tree[rt].sum++;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
  tree[rt] = pushup(tree[rt << 1], tree[rt << 1 | 1], mid);
}
node query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) {
    return tree[rt];
  }
  int mid = (l + r) / 2;
  if (R <= mid)
    return query(L, R, l, mid, rt << 1);
  else if (L > mid)
    return query(L, R, mid + 1, r, rt << 1 | 1);
  else
    return pushup(query(L, R, l, mid, rt << 1),
                  query(L, R, mid + 1, r, rt << 1 | 1), mid);
}
int main() {
  memset(tree, 0, sizeof(tree));
  tot = 0;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &mp[i][j]);
    }
  }
  build(1, m, 1);
  while (q--) {
    scanf("%d %d", &x, &y);
    int ans = query(x, y, 1, m, 1).sum;
    printf("%d\n", ans);
  }
  return 0;
}

### 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;
const int maxn = 1e5 + 7;
struct node {
  int ls[11], rs[11], sum;
} tree[maxn * 4];
int n, m, q, x, y, mp[11][maxn], tot;
int fa[maxn * 10];
int find_set(int x) {
  if (x == fa[x])
    return x;
  else
    return fa[x] = find_set(fa[x]);
}
void union_set(int x, int y) {
  int fx = find_set(x), fy = find_set(y);
  if (fx != fy) {
    fa[fx] = fy;
  }
}
node pushup(node a, node b, int mid) {
  node res;
  res.sum = a.sum + b.sum;
  for (int i = 1; i <= n; i++) {
    fa[a.ls[i]] = a.ls[i];
    fa[b.ls[i]] = b.ls[i];
    fa[a.rs[i]] = a.rs[i];
    fa[b.rs[i]] = b.rs[i];
  }
  for (int i = 1; i <= n; i++) {
    if (mp[i][mid] == mp[i][mid + 1]) {
      if (find_set(a.rs[i]) != find_set(b.ls[i])) {
        res.sum--;
        union_set(a.rs[i], b.ls[i]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    res.ls[i] = find_set(a.ls[i]);
    res.rs[i] = find_set(b.rs[i]);
  }
  return res;
}
void build(int l, int r, int rt) {
  if (l == r) {
    for (int i = 1; i <= n; i++) {
      if (mp[i - 1][l] == mp[i][l])
        tree[rt].ls[i] = tree[rt].rs[i] = tree[rt].ls[i - 1];
      else
        tree[rt].ls[i] = tree[rt].rs[i] = ++tot, tree[rt].sum++;
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(l, mid, rt << 1);
  build(mid + 1, r, rt << 1 | 1);
  tree[rt] = pushup(tree[rt << 1], tree[rt << 1 | 1], mid);
}
node query(int L, int R, int l, int r, int rt) {
  if (L <= l && r <= R) {
    return tree[rt];
  }
  int mid = (l + r) / 2;
  if (R <= mid)
    return query(L, R, l, mid, rt << 1);
  else if (L > mid)
    return query(L, R, mid + 1, r, rt << 1 | 1);
  else
    return pushup(query(L, R, l, mid, rt << 1),
                  query(L, R, mid + 1, r, rt << 1 | 1), mid);
}
int main() {
  memset(tree, 0, sizeof(tree));
  tot = 0;
  scanf("%d%d%d", &n, &m, &q);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &mp[i][j]);
    }
  }
  build(1, m, 1);
  while (q--) {
    scanf("%d %d", &x, &y);
    int ans = query(x, y, 1, m, 1).sum;
    printf("%d\n", ans);
  }
  return 0;
}

```