#include <bits/stdc++.h>
inline char gc() {
  static char *S, *T, now[1 << 16];
  if (S == T) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0;
  char ch = gc();
  while (ch < '0' || ch > '9') ch = gc();
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = gc();
  }
  return x;
}
int map[11][110000], fa[11 * 110000], cnt, num, n, m, q, root;
struct node {
  int l, r, left, right, sum, ls[11], rs[11];
} tree[110000 << 2];
struct node1 {
  int ls[11], rs[11], sum;
};
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void update(int x) {
  int l = tree[x].left, r = tree[x].right;
  tree[x].sum = tree[l].sum + tree[r].sum;
  for (int i = 1; i <= n; ++i) {
    fa[tree[l].ls[i]] = tree[l].ls[i];
    fa[tree[l].rs[i]] = tree[l].rs[i];
    fa[tree[r].ls[i]] = tree[r].ls[i];
    fa[tree[r].rs[i]] = tree[r].rs[i];
  }
  int mid = tree[x].l + tree[x].r >> 1;
  for (int i = 1; i <= n; ++i) {
    if (map[i][mid] == map[i][mid + 1]) {
      int xx = find(tree[l].rs[i]), yy = find(tree[r].ls[i]);
      if (xx != yy) fa[xx] = yy, tree[x].sum--;
    }
  }
  for (int i = 1; i <= n; ++i)
    tree[x].ls[i] = find(tree[tree[x].left].ls[i]),
    tree[x].rs[i] = find(tree[tree[x].right].rs[i]);
}
inline node1 update1(node1 a, node1 b, int mid) {
  node1 tmp;
  tmp.sum = a.sum + b.sum;
  for (int i = 1; i <= n; ++i) {
    fa[a.ls[i]] = a.ls[i];
    fa[a.rs[i]] = a.rs[i];
    fa[b.ls[i]] = b.ls[i];
    fa[b.rs[i]] = b.rs[i];
  }
  for (int i = 1; i <= n; ++i) {
    if (map[i][mid] == map[i][mid + 1]) {
      int xx = find(a.rs[i]), yy = find(b.ls[i]);
      if (xx != yy) fa[xx] = yy, tmp.sum--;
    }
  }
  for (int i = 1; i <= n; ++i)
    tmp.ls[i] = find(a.ls[i]), tmp.rs[i] = find(b.rs[i]);
  return tmp;
}
void build(int &x, int l, int r) {
  x = ++num;
  tree[x].l = l;
  tree[x].r = r;
  if (l == r) {
    for (int i = 1; i <= n; ++i)
      if (map[i][l] == map[i - 1][l])
        tree[x].ls[i] = tree[x].rs[i] = tree[x].ls[i - 1];
      else
        tree[x].ls[i] = tree[x].rs[i] = ++cnt, tree[x].sum++;
    return;
  }
  int mid = l + r >> 1;
  build(tree[x].left, l, mid);
  build(tree[x].right, mid + 1, r);
  update(x);
}
node1 query(int x, int l, int r) {
  if (l <= tree[x].l && r >= tree[x].r) {
    node1 tmp;
    tmp.sum = tree[x].sum;
    for (int i = 1; i <= n; ++i)
      tmp.ls[i] = tree[x].ls[i], tmp.rs[i] = tree[x].rs[i];
    return tmp;
  }
  int mid = tree[x].l + tree[x].r >> 1;
  if (r <= mid) return query(tree[x].left, l, r);
  if (l > mid) return query(tree[x].right, l, r);
  return update1(query(tree[x].left, l, r), query(tree[x].right, l, r), mid);
}
void print(int x) {
  if (tree[x].left) print(tree[x].left);
  printf("%d %d %d\n", tree[x].l, tree[x].r, tree[x].sum);
  if (tree[x].right) print(tree[x].right);
}
int main() {
  n = read();
  m = read();
  q = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) map[i][j] = read();
  build(root, 1, m);
  for (int i = 1; i <= q; ++i) {
    int l = read(), r = read();
    printf("%d\n", query(root, l, r).sum);
  }
  return 0;
}

### Prompt

Develop a solution in CPP to the problem described below:

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>
inline char gc() {
  static char *S, *T, now[1 << 16];
  if (S == T) {
    T = (S = now) + fread(now, 1, 1 << 16, stdin);
    if (S == T) return EOF;
  }
  return *S++;
}
inline int read() {
  int x = 0;
  char ch = gc();
  while (ch < '0' || ch > '9') ch = gc();
  while (ch <= '9' && ch >= '0') {
    x = x * 10 + ch - '0';
    ch = gc();
  }
  return x;
}
int map[11][110000], fa[11 * 110000], cnt, num, n, m, q, root;
struct node {
  int l, r, left, right, sum, ls[11], rs[11];
} tree[110000 << 2];
struct node1 {
  int ls[11], rs[11], sum;
};
inline int find(int x) { return fa[x] == x ? x : fa[x] = find(fa[x]); }
inline void update(int x) {
  int l = tree[x].left, r = tree[x].right;
  tree[x].sum = tree[l].sum + tree[r].sum;
  for (int i = 1; i <= n; ++i) {
    fa[tree[l].ls[i]] = tree[l].ls[i];
    fa[tree[l].rs[i]] = tree[l].rs[i];
    fa[tree[r].ls[i]] = tree[r].ls[i];
    fa[tree[r].rs[i]] = tree[r].rs[i];
  }
  int mid = tree[x].l + tree[x].r >> 1;
  for (int i = 1; i <= n; ++i) {
    if (map[i][mid] == map[i][mid + 1]) {
      int xx = find(tree[l].rs[i]), yy = find(tree[r].ls[i]);
      if (xx != yy) fa[xx] = yy, tree[x].sum--;
    }
  }
  for (int i = 1; i <= n; ++i)
    tree[x].ls[i] = find(tree[tree[x].left].ls[i]),
    tree[x].rs[i] = find(tree[tree[x].right].rs[i]);
}
inline node1 update1(node1 a, node1 b, int mid) {
  node1 tmp;
  tmp.sum = a.sum + b.sum;
  for (int i = 1; i <= n; ++i) {
    fa[a.ls[i]] = a.ls[i];
    fa[a.rs[i]] = a.rs[i];
    fa[b.ls[i]] = b.ls[i];
    fa[b.rs[i]] = b.rs[i];
  }
  for (int i = 1; i <= n; ++i) {
    if (map[i][mid] == map[i][mid + 1]) {
      int xx = find(a.rs[i]), yy = find(b.ls[i]);
      if (xx != yy) fa[xx] = yy, tmp.sum--;
    }
  }
  for (int i = 1; i <= n; ++i)
    tmp.ls[i] = find(a.ls[i]), tmp.rs[i] = find(b.rs[i]);
  return tmp;
}
void build(int &x, int l, int r) {
  x = ++num;
  tree[x].l = l;
  tree[x].r = r;
  if (l == r) {
    for (int i = 1; i <= n; ++i)
      if (map[i][l] == map[i - 1][l])
        tree[x].ls[i] = tree[x].rs[i] = tree[x].ls[i - 1];
      else
        tree[x].ls[i] = tree[x].rs[i] = ++cnt, tree[x].sum++;
    return;
  }
  int mid = l + r >> 1;
  build(tree[x].left, l, mid);
  build(tree[x].right, mid + 1, r);
  update(x);
}
node1 query(int x, int l, int r) {
  if (l <= tree[x].l && r >= tree[x].r) {
    node1 tmp;
    tmp.sum = tree[x].sum;
    for (int i = 1; i <= n; ++i)
      tmp.ls[i] = tree[x].ls[i], tmp.rs[i] = tree[x].rs[i];
    return tmp;
  }
  int mid = tree[x].l + tree[x].r >> 1;
  if (r <= mid) return query(tree[x].left, l, r);
  if (l > mid) return query(tree[x].right, l, r);
  return update1(query(tree[x].left, l, r), query(tree[x].right, l, r), mid);
}
void print(int x) {
  if (tree[x].left) print(tree[x].left);
  printf("%d %d %d\n", tree[x].l, tree[x].r, tree[x].sum);
  if (tree[x].right) print(tree[x].right);
}
int main() {
  n = read();
  m = read();
  q = read();
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= m; ++j) map[i][j] = read();
  build(root, 1, m);
  for (int i = 1; i <= q; ++i) {
    int l = read(), r = read();
    printf("%d\n", query(root, l, r).sum);
  }
  return 0;
}

```