#include <bits/stdc++.h>
using namespace std;
const int inf = (int)1e9;
const long long linf = (long long)1e18;
const double eps = (double)1e-8;
const int mod = (int)1e9 + 7;
const int maxn = (int)1e5 + 5;
struct stripe {
  int cnt;
  int l[11];
  int r[11];
  stripe() {
    cnt = 0;
    for (int i = (0); i <= (int)(10); ++i) {
      l[i] = 0;
      r[i] = 0;
    }
  }
};
int p[41];
int n, m, T;
stripe t[4 * maxn];
int a[11][maxn];
int get(int x) { return (p[x] == x ? x : p[x] = get(p[x])); }
int unio(int x, int y) {
  x = get(x);
  y = get(y);
  p[y] = x;
  return (x != y);
}
void comb(stripe ls, stripe rs, stripe& res, int m) {
  if (ls.cnt == 0) {
    res = rs;
    return;
  }
  if (rs.cnt == 0) {
    res = ls;
    return;
  }
  res.cnt = ls.cnt + rs.cnt;
  for (int i = (1); i <= (int)(n); ++i) {
    rs.l[i] += 2 * n;
    rs.r[i] += 2 * n;
    p[ls.l[i]] = ls.l[i];
    p[ls.r[i]] = ls.r[i];
    p[rs.l[i]] = rs.l[i];
    p[rs.r[i]] = rs.r[i];
  }
  for (int i = (1); i <= (int)(n); ++i) {
    if (a[i][m] == a[i][m + 1]) {
      res.cnt -= unio(ls.r[i], rs.l[i]);
    }
  }
  for (int i = (1); i <= (int)(n); ++i) {
    res.l[i] = get(ls.l[i]);
    res.r[i] = get(rs.r[i]);
  }
  for (int i = (1); i <= (int)(4 * n); ++i) {
    p[i] = 0;
  }
  int cnt = 0;
  for (int i = (1); i <= (int)(n); ++i) {
    if (p[res.l[i]] == 0) {
      p[res.l[i]] = ++cnt;
      res.l[i] = cnt;
    } else {
      res.l[i] = p[res.l[i]];
    }
    if (p[res.r[i]] == 0) {
      p[res.r[i]] = ++cnt;
      res.r[i] = cnt;
    } else {
      res.r[i] = p[res.r[i]];
    }
  }
  return;
}
void build(int v, int l, int r) {
  if (l > r) return;
  if (l == r) {
    t[v].cnt = 1;
    t[v].l[1] = 1;
    t[v].r[1] = 1;
    for (int i = (2); i <= (int)(n); ++i) {
      if (a[i][l] == a[i - 1][l])
        t[v].l[i] = t[v].l[i - 1];
      else
        t[v].l[i] = ++t[v].cnt;
      t[v].r[i] = t[v].l[i];
    }
    return;
  }
  int m = (l + r) / 2;
  build(2 * v, l, m);
  build(2 * v + 1, m + 1, r);
  comb(t[2 * v], t[2 * v + 1], t[v], m);
  return;
}
stripe dzen(int v, int l, int r, int tl, int tr) {
  if (l > r || tl > tr) return stripe();
  if (l == tl && r == tr) {
    return t[v];
  }
  int m = (l + r) / 2;
  stripe ls = dzen(2 * v, l, m, tl, min(tr, m));
  stripe rs = dzen(2 * v + 1, m + 1, r, max(tl, m + 1), tr);
  stripe res;
  comb(ls, rs, res, m);
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> T;
  for (int i = (1); i <= (int)(n); ++i) {
    for (int j = (1); j <= (int)(m); ++j) {
      cin >> a[i][j];
    }
  }
  build(1, 1, m);
  for (int(numt) = (1); (numt) <= (int)(T); ++(numt)) {
    int l, r;
    cin >> l >> r;
    cout << dzen(1, 1, m, l, r).cnt << "\n";
  }
  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;
const int inf = (int)1e9;
const long long linf = (long long)1e18;
const double eps = (double)1e-8;
const int mod = (int)1e9 + 7;
const int maxn = (int)1e5 + 5;
struct stripe {
  int cnt;
  int l[11];
  int r[11];
  stripe() {
    cnt = 0;
    for (int i = (0); i <= (int)(10); ++i) {
      l[i] = 0;
      r[i] = 0;
    }
  }
};
int p[41];
int n, m, T;
stripe t[4 * maxn];
int a[11][maxn];
int get(int x) { return (p[x] == x ? x : p[x] = get(p[x])); }
int unio(int x, int y) {
  x = get(x);
  y = get(y);
  p[y] = x;
  return (x != y);
}
void comb(stripe ls, stripe rs, stripe& res, int m) {
  if (ls.cnt == 0) {
    res = rs;
    return;
  }
  if (rs.cnt == 0) {
    res = ls;
    return;
  }
  res.cnt = ls.cnt + rs.cnt;
  for (int i = (1); i <= (int)(n); ++i) {
    rs.l[i] += 2 * n;
    rs.r[i] += 2 * n;
    p[ls.l[i]] = ls.l[i];
    p[ls.r[i]] = ls.r[i];
    p[rs.l[i]] = rs.l[i];
    p[rs.r[i]] = rs.r[i];
  }
  for (int i = (1); i <= (int)(n); ++i) {
    if (a[i][m] == a[i][m + 1]) {
      res.cnt -= unio(ls.r[i], rs.l[i]);
    }
  }
  for (int i = (1); i <= (int)(n); ++i) {
    res.l[i] = get(ls.l[i]);
    res.r[i] = get(rs.r[i]);
  }
  for (int i = (1); i <= (int)(4 * n); ++i) {
    p[i] = 0;
  }
  int cnt = 0;
  for (int i = (1); i <= (int)(n); ++i) {
    if (p[res.l[i]] == 0) {
      p[res.l[i]] = ++cnt;
      res.l[i] = cnt;
    } else {
      res.l[i] = p[res.l[i]];
    }
    if (p[res.r[i]] == 0) {
      p[res.r[i]] = ++cnt;
      res.r[i] = cnt;
    } else {
      res.r[i] = p[res.r[i]];
    }
  }
  return;
}
void build(int v, int l, int r) {
  if (l > r) return;
  if (l == r) {
    t[v].cnt = 1;
    t[v].l[1] = 1;
    t[v].r[1] = 1;
    for (int i = (2); i <= (int)(n); ++i) {
      if (a[i][l] == a[i - 1][l])
        t[v].l[i] = t[v].l[i - 1];
      else
        t[v].l[i] = ++t[v].cnt;
      t[v].r[i] = t[v].l[i];
    }
    return;
  }
  int m = (l + r) / 2;
  build(2 * v, l, m);
  build(2 * v + 1, m + 1, r);
  comb(t[2 * v], t[2 * v + 1], t[v], m);
  return;
}
stripe dzen(int v, int l, int r, int tl, int tr) {
  if (l > r || tl > tr) return stripe();
  if (l == tl && r == tr) {
    return t[v];
  }
  int m = (l + r) / 2;
  stripe ls = dzen(2 * v, l, m, tl, min(tr, m));
  stripe rs = dzen(2 * v + 1, m + 1, r, max(tl, m + 1), tr);
  stripe res;
  comb(ls, rs, res, m);
  return res;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> m >> T;
  for (int i = (1); i <= (int)(n); ++i) {
    for (int j = (1); j <= (int)(m); ++j) {
      cin >> a[i][j];
    }
  }
  build(1, 1, m);
  for (int(numt) = (1); (numt) <= (int)(T); ++(numt)) {
    int l, r;
    cin >> l >> r;
    cout << dzen(1, 1, m, l, r).cnt << "\n";
  }
  return 0;
}

```