#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 10;
int par[4 * M], vl[4 * M];
int mp[M][N];
int n, m, q;
bool mark[4 * M];
int get_par(int v) {
  if (par[v] == -1) return v;
  return par[v] = get_par(par[v]);
}
bool merge(int v, int u) {
  v = get_par(v);
  u = get_par(u);
  if (u == v) return false;
  par[v] = u;
  return true;
}
struct node {
  int cnt = 0;
  int r[M], l[M];
  node() {
    cnt = 0;
    memset(r, 0, sizeof r);
    memset(l, 0, sizeof l);
  }
} seg[4 * N];
node merge(node a, node b, int mid) {
  if (a.cnt == -1) return b;
  if (b.cnt == -1) return a;
  node ans;
  ans.cnt = a.cnt + b.cnt;
  int mxv = -1, mx = -1;
  for (int i = 0; i < n; i++) mxv = max(mxv, max(a.r[i], a.l[i]));
  mxv++;
  for (int i = 0; i < n; i++)
    b.l[i] += mxv, b.r[i] += mxv, mx = max(mx, max(b.l[i], b.r[i]));
  memset(par, -1, sizeof par);
  memset(mark, 0, sizeof mark);
  for (int i = 0; i < n; i++)
    if (mp[i][mid - 1] == mp[i][mid])
      if (merge(a.r[i], b.l[i])) ans.cnt--;
  for (int i = 0; i < n; i++) {
    int p1 = get_par(a.l[i]), p2 = get_par(b.r[i]);
    mark[p1] = mark[p2] = true;
  }
  int t = 0;
  for (int i = 0; i < 4 * M; i++)
    if (mark[i]) vl[i] = t++;
  for (int i = 0; i < n; i++) {
    ans.l[i] = vl[get_par(a.l[i])];
    ans.r[i] = vl[get_par(b.r[i])];
  }
  return ans;
}
void build(int id = 1, int s = 0, int e = m) {
  if (e - s < 2) {
    int vl = 0;
    seg[id].cnt = n;
    seg[id].r[0] = seg[id].l[0] = vl;
    for (int i = 0; i < n - 1; i++)
      if (mp[i][s] == mp[i + 1][s]) {
        seg[id].cnt--;
        seg[id].r[i + 1] = seg[id].l[i + 1] = vl;
      } else {
        seg[id].r[i + 1] = seg[id].l[i + 1] = ++vl;
      }
    return;
  }
  int mid = (s + e) / 2;
  build(2 * id, s, mid);
  build(2 * id + 1, mid, e);
  seg[id] = merge(seg[2 * id], seg[2 * id + 1], mid);
}
node get(int id, int L, int R, int s = 0, int e = m) {
  if (L <= s && e <= R) return seg[id];
  node tmp;
  tmp.cnt = -1;
  if (L >= e || s >= R) return tmp;
  int mid = (s + e) / 2;
  return merge(get(2 * id, L, R, s, mid), get(2 * id + 1, L, R, mid, e), mid);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> mp[i][j];
  build();
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    cout << get(1, l, r).cnt << '\n';
  }
}

### 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 N = 1e5 + 10, M = 10;
int par[4 * M], vl[4 * M];
int mp[M][N];
int n, m, q;
bool mark[4 * M];
int get_par(int v) {
  if (par[v] == -1) return v;
  return par[v] = get_par(par[v]);
}
bool merge(int v, int u) {
  v = get_par(v);
  u = get_par(u);
  if (u == v) return false;
  par[v] = u;
  return true;
}
struct node {
  int cnt = 0;
  int r[M], l[M];
  node() {
    cnt = 0;
    memset(r, 0, sizeof r);
    memset(l, 0, sizeof l);
  }
} seg[4 * N];
node merge(node a, node b, int mid) {
  if (a.cnt == -1) return b;
  if (b.cnt == -1) return a;
  node ans;
  ans.cnt = a.cnt + b.cnt;
  int mxv = -1, mx = -1;
  for (int i = 0; i < n; i++) mxv = max(mxv, max(a.r[i], a.l[i]));
  mxv++;
  for (int i = 0; i < n; i++)
    b.l[i] += mxv, b.r[i] += mxv, mx = max(mx, max(b.l[i], b.r[i]));
  memset(par, -1, sizeof par);
  memset(mark, 0, sizeof mark);
  for (int i = 0; i < n; i++)
    if (mp[i][mid - 1] == mp[i][mid])
      if (merge(a.r[i], b.l[i])) ans.cnt--;
  for (int i = 0; i < n; i++) {
    int p1 = get_par(a.l[i]), p2 = get_par(b.r[i]);
    mark[p1] = mark[p2] = true;
  }
  int t = 0;
  for (int i = 0; i < 4 * M; i++)
    if (mark[i]) vl[i] = t++;
  for (int i = 0; i < n; i++) {
    ans.l[i] = vl[get_par(a.l[i])];
    ans.r[i] = vl[get_par(b.r[i])];
  }
  return ans;
}
void build(int id = 1, int s = 0, int e = m) {
  if (e - s < 2) {
    int vl = 0;
    seg[id].cnt = n;
    seg[id].r[0] = seg[id].l[0] = vl;
    for (int i = 0; i < n - 1; i++)
      if (mp[i][s] == mp[i + 1][s]) {
        seg[id].cnt--;
        seg[id].r[i + 1] = seg[id].l[i + 1] = vl;
      } else {
        seg[id].r[i + 1] = seg[id].l[i + 1] = ++vl;
      }
    return;
  }
  int mid = (s + e) / 2;
  build(2 * id, s, mid);
  build(2 * id + 1, mid, e);
  seg[id] = merge(seg[2 * id], seg[2 * id + 1], mid);
}
node get(int id, int L, int R, int s = 0, int e = m) {
  if (L <= s && e <= R) return seg[id];
  node tmp;
  tmp.cnt = -1;
  if (L >= e || s >= R) return tmp;
  int mid = (s + e) / 2;
  return merge(get(2 * id, L, R, s, mid), get(2 * id + 1, L, R, mid, e), mid);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> q;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++) cin >> mp[i][j];
  build();
  while (q--) {
    int l, r;
    cin >> l >> r;
    l--;
    cout << get(1, l, r).cnt << '\n';
  }
}

```