#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
int n, m, q;
int a[11][maxn];
int dx[5] = {0, 1, 0, -1};
int dy[5] = {1, 0, -1, 0};
struct p {
  int l, r;
  int c[22];
  int x;
  void init() {
    x = 0;
    return;
  }
} val[maxn << 2];
vector<int> tmp[11 * 4];
int f[44];
int getf(int x) {
  if (x == f[x])
    return x;
  else
    return f[x] = getf(f[x]);
}
int merg(int x, int y) {
  int a = getf(x), b = getf(y);
  if (a > b) swap(a, b);
  if (a != b) {
    f[b] = a;
    return 1;
  } else
    return 0;
}
void init(int n) {
  for (int i = 1; i <= n; i++) f[i] = i;
  return;
}
p operator+(const p &A, const p &B) {
  p res;
  res.l = A.l, res.r = B.r;
  res.x = A.x + B.x;
  int x, y;
  for (int i = 1; i <= 2 * n; i++) {
    f[i] = A.c[i];
    f[i + 2 * n] = B.c[i] + 2 * n;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i][A.r] == a[i][B.l]) {
      res.x -= merg(i + 2 * n, i + n);
    }
  }
  for (int i = 1; i <= 4 * n; i++) tmp[i].clear();
  for (int i = 1; i <= n; i++) {
    tmp[getf(i)].push_back(i);
    tmp[getf(i + 3 * n)].push_back(i + n);
  }
  for (int i = 1; i <= 4 * n; i++) {
    for (int j = 0; j < (int)tmp[i].size(); j++) {
      res.c[tmp[i][j]] = tmp[i][0];
    }
  }
  return res;
}
void build(int o, int l, int r) {
  val[o].l = l, val[o].r = r;
  if (l == r) {
    val[o].x = n;
    init(n);
    for (int i = 2; i <= n; i++) {
      if (a[i][l] == a[i - 1][l]) {
        val[o].x -= merg(i, i - 1);
      }
    }
    for (int i = 1; i <= n; i++) {
      val[o].c[i] = val[o].c[i + n] = getf(i);
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(o << 1, l, mid);
  build(o << 1 | 1, mid + 1, r);
  val[o] = val[o << 1] + val[o << 1 | 1];
  return;
}
p query(int o, int l, int r, int ll, int rr) {
  if (l == ll && r == rr) {
    return val[o];
  }
  int mid = (l + r) >> 1;
  if (rr <= mid) {
    return query(o << 1, l, mid, ll, rr);
  }
  if (ll > mid) {
    return query(o << 1 | 1, mid + 1, r, ll, rr);
  }
  return query(o << 1, l, mid, ll, mid) +
         query(o << 1 | 1, mid + 1, r, mid + 1, rr);
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  int i, j, k, x, y;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  build(1, 1, m);
  int l, r;
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", query(1, 1, m, l, r).x);
  }
}

### Prompt

Your challenge is to write 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 + 5;
int n, m, q;
int a[11][maxn];
int dx[5] = {0, 1, 0, -1};
int dy[5] = {1, 0, -1, 0};
struct p {
  int l, r;
  int c[22];
  int x;
  void init() {
    x = 0;
    return;
  }
} val[maxn << 2];
vector<int> tmp[11 * 4];
int f[44];
int getf(int x) {
  if (x == f[x])
    return x;
  else
    return f[x] = getf(f[x]);
}
int merg(int x, int y) {
  int a = getf(x), b = getf(y);
  if (a > b) swap(a, b);
  if (a != b) {
    f[b] = a;
    return 1;
  } else
    return 0;
}
void init(int n) {
  for (int i = 1; i <= n; i++) f[i] = i;
  return;
}
p operator+(const p &A, const p &B) {
  p res;
  res.l = A.l, res.r = B.r;
  res.x = A.x + B.x;
  int x, y;
  for (int i = 1; i <= 2 * n; i++) {
    f[i] = A.c[i];
    f[i + 2 * n] = B.c[i] + 2 * n;
  }
  for (int i = 1; i <= n; i++) {
    if (a[i][A.r] == a[i][B.l]) {
      res.x -= merg(i + 2 * n, i + n);
    }
  }
  for (int i = 1; i <= 4 * n; i++) tmp[i].clear();
  for (int i = 1; i <= n; i++) {
    tmp[getf(i)].push_back(i);
    tmp[getf(i + 3 * n)].push_back(i + n);
  }
  for (int i = 1; i <= 4 * n; i++) {
    for (int j = 0; j < (int)tmp[i].size(); j++) {
      res.c[tmp[i][j]] = tmp[i][0];
    }
  }
  return res;
}
void build(int o, int l, int r) {
  val[o].l = l, val[o].r = r;
  if (l == r) {
    val[o].x = n;
    init(n);
    for (int i = 2; i <= n; i++) {
      if (a[i][l] == a[i - 1][l]) {
        val[o].x -= merg(i, i - 1);
      }
    }
    for (int i = 1; i <= n; i++) {
      val[o].c[i] = val[o].c[i + n] = getf(i);
    }
    return;
  }
  int mid = (l + r) >> 1;
  build(o << 1, l, mid);
  build(o << 1 | 1, mid + 1, r);
  val[o] = val[o << 1] + val[o << 1 | 1];
  return;
}
p query(int o, int l, int r, int ll, int rr) {
  if (l == ll && r == rr) {
    return val[o];
  }
  int mid = (l + r) >> 1;
  if (rr <= mid) {
    return query(o << 1, l, mid, ll, rr);
  }
  if (ll > mid) {
    return query(o << 1 | 1, mid + 1, r, ll, rr);
  }
  return query(o << 1, l, mid, ll, mid) +
         query(o << 1 | 1, mid + 1, r, mid + 1, rr);
}
int main() {
  scanf("%d%d%d", &n, &m, &q);
  int i, j, k, x, y;
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  build(1, 1, m);
  int l, r;
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", query(1, 1, m, l, r).x);
  }
}

```