#include <bits/stdc++.h>
using namespace std;
struct majik {
  int lef[13], rig[13], res = 0;
  void comb(int x, int y) {
    res--;
    for (int i = 1; i <= 10; i++) {
      if (lef[i] == x) {
        lef[i] = y;
      }
      if (rig[i] == x) {
        rig[i] = y;
      }
    }
  }
};
int n, m, q;
majik it[400007];
int x[13][100007];
majik connect(majik a, majik b, int l, int r) {
  majik c;
  for (int i = 1; i <= n; i++) {
    c.lef[i] = a.lef[i];
    c.rig[i] = b.rig[i];
  }
  c.res = a.res + b.res;
  for (int i = 1; i <= n; i++) {
    if (x[i][l] == x[i][r]) {
      if (a.rig[i] != b.lef[i]) {
        c.comb(a.rig[i], b.lef[i]);
        a.comb(a.rig[i], b.lef[i]);
      }
    }
  }
  return c;
}
void init(int k, int l, int r) {
  if (l == r) {
    for (int i = 1; i <= n; i++) {
      it[k].lef[i] = (l - 1) * n + i;
      it[k].rig[i] = (l - 1) * n + i;
    }
    it[k].res = n;
    for (int i = 2; i <= n; i++) {
      if (x[i][l] == x[i - 1][l]) {
        it[k].comb(it[k].lef[i], it[k].rig[i - 1]);
      }
    }
    return;
  }
  int mid = (l + r) / 2;
  init(k * 2, l, mid);
  init(k * 2 + 1, mid + 1, r);
  it[k] = connect(it[k * 2], it[k * 2 + 1], mid, mid + 1);
}
majik get(int k, int l, int r, int le, int ri) {
  if (l >= le && r <= ri) return it[k];
  int mid = (l + r) / 2;
  majik rem;
  if (mid >= ri) {
    rem = get(k * 2, l, mid, le, ri);
  } else if (mid < le) {
    rem = get(k * 2 + 1, mid + 1, r, le, ri);
  } else
    rem = connect(get(k * 2, l, mid, le, ri),
                  get(k * 2 + 1, mid + 1, r, le, ri), mid, mid + 1);
  return rem;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> x[i][j];
    }
  }
  init(1, 1, m);
  while (q--) {
    int u, v;
    cin >> u >> v;
    cout << get(1, 1, m, u, v).res << endl;
  }
}

### 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 majik {
  int lef[13], rig[13], res = 0;
  void comb(int x, int y) {
    res--;
    for (int i = 1; i <= 10; i++) {
      if (lef[i] == x) {
        lef[i] = y;
      }
      if (rig[i] == x) {
        rig[i] = y;
      }
    }
  }
};
int n, m, q;
majik it[400007];
int x[13][100007];
majik connect(majik a, majik b, int l, int r) {
  majik c;
  for (int i = 1; i <= n; i++) {
    c.lef[i] = a.lef[i];
    c.rig[i] = b.rig[i];
  }
  c.res = a.res + b.res;
  for (int i = 1; i <= n; i++) {
    if (x[i][l] == x[i][r]) {
      if (a.rig[i] != b.lef[i]) {
        c.comb(a.rig[i], b.lef[i]);
        a.comb(a.rig[i], b.lef[i]);
      }
    }
  }
  return c;
}
void init(int k, int l, int r) {
  if (l == r) {
    for (int i = 1; i <= n; i++) {
      it[k].lef[i] = (l - 1) * n + i;
      it[k].rig[i] = (l - 1) * n + i;
    }
    it[k].res = n;
    for (int i = 2; i <= n; i++) {
      if (x[i][l] == x[i - 1][l]) {
        it[k].comb(it[k].lef[i], it[k].rig[i - 1]);
      }
    }
    return;
  }
  int mid = (l + r) / 2;
  init(k * 2, l, mid);
  init(k * 2 + 1, mid + 1, r);
  it[k] = connect(it[k * 2], it[k * 2 + 1], mid, mid + 1);
}
majik get(int k, int l, int r, int le, int ri) {
  if (l >= le && r <= ri) return it[k];
  int mid = (l + r) / 2;
  majik rem;
  if (mid >= ri) {
    rem = get(k * 2, l, mid, le, ri);
  } else if (mid < le) {
    rem = get(k * 2 + 1, mid + 1, r, le, ri);
  } else
    rem = connect(get(k * 2, l, mid, le, ri),
                  get(k * 2 + 1, mid + 1, r, le, ri), mid, mid + 1);
  return rem;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> x[i][j];
    }
  }
  init(1, 1, m);
  while (q--) {
    int u, v;
    cin >> u >> v;
    cout << get(1, 1, m, u, v).res << endl;
  }
}

```