#include <bits/stdc++.h>
using namespace std;
struct node {
  int count;
  int lp[10], rp[10];
};
int R, C, q;
int v[10][100001];
node seg[4 * 100001];
int p[40];
int seen[40], now;
int mp[40];
int find(int u) {
  if (p[u] == u) return u;
  return p[u] = find(p[u]);
}
void merge(node &res, const node &a, const node &b, int at) {
  for (int cur = 0, i = 0; i < 4 * R; ++i) {
    if (i < R)
      p[i] = a.lp[cur];
    else if (i < 2 * R)
      p[i] = a.rp[cur];
    else if (i < 3 * R)
      p[i] = b.lp[cur] + 2 * R;
    else
      p[i] = b.rp[cur] + 2 * R;
    ++cur;
    if (cur == R) cur = 0;
  }
  res.count = a.count + b.count;
  for (int x, y, i = 0; i < R; ++i)
    if (v[i][at] == v[i][at + 1]) {
      x = find(i + R);
      y = find(i + 2 * R);
      if ((i + at) & 1) swap(x, y);
      if (x != y) {
        --res.count;
        p[x] = y;
      }
    }
  ++now;
  for (int i = 0; i < R; ++i) {
    int x = find(i);
    if (seen[x] != now) {
      seen[x] = now;
      mp[x] = i;
    }
    res.lp[i] = mp[x];
  }
  for (int i = 0; i < R; ++i) {
    int x = find(i + 3 * R);
    if (seen[x] != now) {
      seen[x] = now;
      mp[x] = i + R;
    }
    res.rp[i] = mp[x];
  }
}
void build(int n, int s, int e) {
  if (s == e) {
    seg[n].lp[0] = seg[n].rp[0] = 0;
    seg[n].count = 1;
    for (int i = 1; i < R; ++i)
      if (v[i][s] == v[i - 1][s])
        seg[n].lp[i] = seg[n].rp[i] = seg[n].lp[i - 1];
      else {
        seg[n].lp[i] = seg[n].rp[i] = i;
        ++seg[n].count;
      }
    return;
  }
  build(n * 2, s, (s + e) / 2);
  build(n * 2 + 1, (s + e) / 2 + 1, e);
  merge(seg[n], seg[n * 2], seg[n * 2 + 1], (s + e) / 2);
}
int l, r;
node get(int n, int s, int e) {
  if (s >= l && e <= r) return seg[n];
  if (r <= (s + e) / 2) return get(n * 2, s, (s + e) / 2);
  if (l > (s + e) / 2) return get(n * 2 + 1, (s + e) / 2 + 1, e);
  node a = get(n * 2, s, (s + e) / 2);
  node b = get(n * 2 + 1, (s + e) / 2 + 1, e);
  node ret;
  merge(ret, a, b, (s + e) / 2);
  return ret;
}
int main() {
  scanf("%d%d%d", &R, &C, &q);
  for (int i = 0; i < R; ++i)
    for (int j = 1; j <= C; ++j) scanf("%d", &v[i][j]);
  build(1, 1, C);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", get(1, 1, C).count);
  }
  return 0;
}

### Prompt

Generate 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;
struct node {
  int count;
  int lp[10], rp[10];
};
int R, C, q;
int v[10][100001];
node seg[4 * 100001];
int p[40];
int seen[40], now;
int mp[40];
int find(int u) {
  if (p[u] == u) return u;
  return p[u] = find(p[u]);
}
void merge(node &res, const node &a, const node &b, int at) {
  for (int cur = 0, i = 0; i < 4 * R; ++i) {
    if (i < R)
      p[i] = a.lp[cur];
    else if (i < 2 * R)
      p[i] = a.rp[cur];
    else if (i < 3 * R)
      p[i] = b.lp[cur] + 2 * R;
    else
      p[i] = b.rp[cur] + 2 * R;
    ++cur;
    if (cur == R) cur = 0;
  }
  res.count = a.count + b.count;
  for (int x, y, i = 0; i < R; ++i)
    if (v[i][at] == v[i][at + 1]) {
      x = find(i + R);
      y = find(i + 2 * R);
      if ((i + at) & 1) swap(x, y);
      if (x != y) {
        --res.count;
        p[x] = y;
      }
    }
  ++now;
  for (int i = 0; i < R; ++i) {
    int x = find(i);
    if (seen[x] != now) {
      seen[x] = now;
      mp[x] = i;
    }
    res.lp[i] = mp[x];
  }
  for (int i = 0; i < R; ++i) {
    int x = find(i + 3 * R);
    if (seen[x] != now) {
      seen[x] = now;
      mp[x] = i + R;
    }
    res.rp[i] = mp[x];
  }
}
void build(int n, int s, int e) {
  if (s == e) {
    seg[n].lp[0] = seg[n].rp[0] = 0;
    seg[n].count = 1;
    for (int i = 1; i < R; ++i)
      if (v[i][s] == v[i - 1][s])
        seg[n].lp[i] = seg[n].rp[i] = seg[n].lp[i - 1];
      else {
        seg[n].lp[i] = seg[n].rp[i] = i;
        ++seg[n].count;
      }
    return;
  }
  build(n * 2, s, (s + e) / 2);
  build(n * 2 + 1, (s + e) / 2 + 1, e);
  merge(seg[n], seg[n * 2], seg[n * 2 + 1], (s + e) / 2);
}
int l, r;
node get(int n, int s, int e) {
  if (s >= l && e <= r) return seg[n];
  if (r <= (s + e) / 2) return get(n * 2, s, (s + e) / 2);
  if (l > (s + e) / 2) return get(n * 2 + 1, (s + e) / 2 + 1, e);
  node a = get(n * 2, s, (s + e) / 2);
  node b = get(n * 2 + 1, (s + e) / 2 + 1, e);
  node ret;
  merge(ret, a, b, (s + e) / 2);
  return ret;
}
int main() {
  scanf("%d%d%d", &R, &C, &q);
  for (int i = 0; i < R; ++i)
    for (int j = 1; j <= C; ++j) scanf("%d", &v[i][j]);
  build(1, 1, C);
  while (q--) {
    scanf("%d%d", &l, &r);
    printf("%d\n", get(1, 1, C).count);
  }
  return 0;
}

```