#include <bits/stdc++.h>
using namespace std;
void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
}
const int MAXN = 11;
const int MAXM = 1e5 + 2;
struct node {
  int lft[MAXN];
  int rgt[MAXN];
  int lft_com[MAXN];
  int rgt_com[MAXN];
  int comps;
};
int n, m, q;
node st[MAXM * 3];
int A[MAXN][MAXM];
int pool;
node merge(node l, node r) {
  node ret;
  ret.comps = l.comps + r.comps;
  for (int i = 1; i <= n; i++) {
    if (l.rgt[i] == r.lft[i] && l.rgt_com[i] != r.lft_com[i]) {
      ret.comps -= 1;
      int cr = r.lft_com[i];
      int cl = l.rgt_com[i];
      for (int j = 1; j <= n; j++) {
        if (l.lft_com[j] == cr) {
          l.lft_com[j] = cl;
        }
        if (l.rgt_com[j] == cr) {
          l.rgt_com[j] = cl;
        }
        if (r.lft_com[j] == cr) {
          r.lft_com[j] = cl;
        }
        if (r.rgt_com[j] == cr) {
          r.rgt_com[j] = cl;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ret.lft[i] = l.lft[i];
    ret.rgt[i] = r.rgt[i];
    ret.lft_com[i] = l.lft_com[i];
    ret.rgt_com[i] = r.rgt_com[i];
  }
  return ret;
}
void build(int idx = 1, int l = 1, int r = m) {
  if (l == r) {
    st[idx].comps = 0;
    for (int i = 1; i <= n; i++) {
      st[idx].lft[i] = st[idx].rgt[i] = A[i][l];
      if (A[i][l] != A[i - 1][l]) {
        st[idx].lft_com[i] = st[idx].rgt_com[i] = ++pool;
        st[idx].comps += 1;
      } else {
        st[idx].lft_com[i] = st[idx].rgt_com[i] = st[idx].lft_com[i - 1];
      }
    }
    return;
  }
  int m = l + (r - l) / 2;
  build(idx << 1, l, m);
  build(idx << 1 | 1, m + 1, r);
  st[idx] = merge(st[idx << 1], st[idx << 1 | 1]);
}
node fans;
void query(int idx, int l, int r, int ql, int qr) {
  if (ql > r || qr < l) {
    return;
  }
  if (ql <= l && r <= qr) {
    fans = merge(fans, st[idx]);
    return;
  }
  int m = l + (r - l) / 2;
  query(idx << 1, l, m, ql, qr);
  query(idx << 1 | 1, m + 1, r, ql, qr);
}
void init_node(node& ret) {
  memset(ret.lft, 0, sizeof(ret.lft));
  memset(ret.rgt, 0, sizeof(ret.rgt));
  memset(ret.lft_com, 0, sizeof(ret.lft_com));
  memset(ret.rgt_com, 0, sizeof(ret.rgt_com));
  ret.comps = 0;
}
int main(int argc, char* argv[]) {
  io();
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> A[i][j];
    }
  }
  build();
  int l, r;
  while (q--) {
    cin >> l >> r;
    init_node(fans);
    query(1, 1, m, l, r);
    cout << fans.comps << '\n';
  }
  return 0;
}

### Prompt

Create a solution in cpp for 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;
void io() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cout.precision(15);
}
const int MAXN = 11;
const int MAXM = 1e5 + 2;
struct node {
  int lft[MAXN];
  int rgt[MAXN];
  int lft_com[MAXN];
  int rgt_com[MAXN];
  int comps;
};
int n, m, q;
node st[MAXM * 3];
int A[MAXN][MAXM];
int pool;
node merge(node l, node r) {
  node ret;
  ret.comps = l.comps + r.comps;
  for (int i = 1; i <= n; i++) {
    if (l.rgt[i] == r.lft[i] && l.rgt_com[i] != r.lft_com[i]) {
      ret.comps -= 1;
      int cr = r.lft_com[i];
      int cl = l.rgt_com[i];
      for (int j = 1; j <= n; j++) {
        if (l.lft_com[j] == cr) {
          l.lft_com[j] = cl;
        }
        if (l.rgt_com[j] == cr) {
          l.rgt_com[j] = cl;
        }
        if (r.lft_com[j] == cr) {
          r.lft_com[j] = cl;
        }
        if (r.rgt_com[j] == cr) {
          r.rgt_com[j] = cl;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ret.lft[i] = l.lft[i];
    ret.rgt[i] = r.rgt[i];
    ret.lft_com[i] = l.lft_com[i];
    ret.rgt_com[i] = r.rgt_com[i];
  }
  return ret;
}
void build(int idx = 1, int l = 1, int r = m) {
  if (l == r) {
    st[idx].comps = 0;
    for (int i = 1; i <= n; i++) {
      st[idx].lft[i] = st[idx].rgt[i] = A[i][l];
      if (A[i][l] != A[i - 1][l]) {
        st[idx].lft_com[i] = st[idx].rgt_com[i] = ++pool;
        st[idx].comps += 1;
      } else {
        st[idx].lft_com[i] = st[idx].rgt_com[i] = st[idx].lft_com[i - 1];
      }
    }
    return;
  }
  int m = l + (r - l) / 2;
  build(idx << 1, l, m);
  build(idx << 1 | 1, m + 1, r);
  st[idx] = merge(st[idx << 1], st[idx << 1 | 1]);
}
node fans;
void query(int idx, int l, int r, int ql, int qr) {
  if (ql > r || qr < l) {
    return;
  }
  if (ql <= l && r <= qr) {
    fans = merge(fans, st[idx]);
    return;
  }
  int m = l + (r - l) / 2;
  query(idx << 1, l, m, ql, qr);
  query(idx << 1 | 1, m + 1, r, ql, qr);
}
void init_node(node& ret) {
  memset(ret.lft, 0, sizeof(ret.lft));
  memset(ret.rgt, 0, sizeof(ret.rgt));
  memset(ret.lft_com, 0, sizeof(ret.lft_com));
  memset(ret.rgt_com, 0, sizeof(ret.rgt_com));
  ret.comps = 0;
}
int main(int argc, char* argv[]) {
  io();
  cin >> n >> m >> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      cin >> A[i][j];
    }
  }
  build();
  int l, r;
  while (q--) {
    cin >> l >> r;
    init_node(fans);
    query(1, 1, m, l, r);
    cout << fans.comps << '\n';
  }
  return 0;
}

```