#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 10;
struct TnT {
  int l, r, sum;
  int lsum[11], rsum[11];
} T[M << 2];
int f[M << 4], a[11][M];
int n, m, q, tot;
void init() {
  for (int i = 1; i <= n * m; i++) {
    f[i] = i;
  }
}
int find(int x) {
  if (x == f[x]) return x;
  int tmp = find(f[x]);
  return f[x] = tmp;
}
TnT push_up(int mid, TnT le, TnT re) {
  TnT ans;
  ans.sum = le.sum + re.sum;
  for (int j = 1; j <= n; j++) {
    f[le.lsum[j]] = le.lsum[j];
    f[le.rsum[j]] = le.rsum[j];
    f[re.lsum[j]] = re.lsum[j];
    f[re.rsum[j]] = re.rsum[j];
  }
  for (int j = 1; j <= n; j++) {
    if (a[j][mid] == a[j][mid + 1]) {
      int t1 = find(le.rsum[j]), t2 = find(re.lsum[j]);
      if (t1 != t2) {
        ans.sum--;
        f[t1] = t2;
      }
    }
  }
  for (int j = 1; j <= n; j++) {
    ans.lsum[j] = find(le.lsum[j]);
    ans.rsum[j] = find(re.rsum[j]);
  }
  ans.l = le.l, ans.r = re.r;
  return ans;
}
void build(int i, int l, int r) {
  int mid = (l + r) >> 1;
  T[i].l = l, T[i].r = r, T[i].sum = 0;
  if (l == r) {
    for (int j = 1; j <= n; j++) {
      if (a[j][l] == a[j - 1][l]) {
        T[i].lsum[j] = T[i].rsum[j] = T[i].lsum[j - 1];
      } else {
        T[i].lsum[j] = T[i].rsum[j] = ++tot;
        T[i].sum++;
      }
    }
    return;
  }
  build(i << 1, l, mid);
  build((i << 1) | 1, mid + 1, r);
  T[i] = push_up(mid, T[i << 1], T[(i << 1) | 1]);
}
TnT query(int i, int l, int r) {
  int mid = (T[i].l + T[i].r) >> 1;
  if (T[i].l == l && T[i].r == r) {
    return T[i];
  }
  T[i] = push_up(mid, T[i << 1], T[(i << 1) | 1]);
  if (mid < l)
    return query((i << 1) | 1, l, r);
  else if (mid >= r)
    return query(i << 1, l, r);
  else {
    return push_up(mid, query(i << 1, l, mid), query((i << 1) | 1, mid + 1, r));
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  tot = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  init();
  build(1, 1, m);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%d\n", query(1, x, y).sum);
  }
  return 0;
}

### Prompt

In cpp, your task is to solve 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 M = 1e5 + 10;
struct TnT {
  int l, r, sum;
  int lsum[11], rsum[11];
} T[M << 2];
int f[M << 4], a[11][M];
int n, m, q, tot;
void init() {
  for (int i = 1; i <= n * m; i++) {
    f[i] = i;
  }
}
int find(int x) {
  if (x == f[x]) return x;
  int tmp = find(f[x]);
  return f[x] = tmp;
}
TnT push_up(int mid, TnT le, TnT re) {
  TnT ans;
  ans.sum = le.sum + re.sum;
  for (int j = 1; j <= n; j++) {
    f[le.lsum[j]] = le.lsum[j];
    f[le.rsum[j]] = le.rsum[j];
    f[re.lsum[j]] = re.lsum[j];
    f[re.rsum[j]] = re.rsum[j];
  }
  for (int j = 1; j <= n; j++) {
    if (a[j][mid] == a[j][mid + 1]) {
      int t1 = find(le.rsum[j]), t2 = find(re.lsum[j]);
      if (t1 != t2) {
        ans.sum--;
        f[t1] = t2;
      }
    }
  }
  for (int j = 1; j <= n; j++) {
    ans.lsum[j] = find(le.lsum[j]);
    ans.rsum[j] = find(re.rsum[j]);
  }
  ans.l = le.l, ans.r = re.r;
  return ans;
}
void build(int i, int l, int r) {
  int mid = (l + r) >> 1;
  T[i].l = l, T[i].r = r, T[i].sum = 0;
  if (l == r) {
    for (int j = 1; j <= n; j++) {
      if (a[j][l] == a[j - 1][l]) {
        T[i].lsum[j] = T[i].rsum[j] = T[i].lsum[j - 1];
      } else {
        T[i].lsum[j] = T[i].rsum[j] = ++tot;
        T[i].sum++;
      }
    }
    return;
  }
  build(i << 1, l, mid);
  build((i << 1) | 1, mid + 1, r);
  T[i] = push_up(mid, T[i << 1], T[(i << 1) | 1]);
}
TnT query(int i, int l, int r) {
  int mid = (T[i].l + T[i].r) >> 1;
  if (T[i].l == l && T[i].r == r) {
    return T[i];
  }
  T[i] = push_up(mid, T[i << 1], T[(i << 1) | 1]);
  if (mid < l)
    return query((i << 1) | 1, l, r);
  else if (mid >= r)
    return query(i << 1, l, r);
  else {
    return push_up(mid, query(i << 1, l, mid), query((i << 1) | 1, mid + 1, r));
  }
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  tot = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  init();
  build(1, 1, m);
  while (q--) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%d\n", query(1, x, y).sum);
  }
  return 0;
}

```