#include <bits/stdc++.h>
using namespace std;
const int N = 1 << 12;
const int M = 1 << 16;
const int SIZ = 2.2e6;
const int B = 1 << 8;
inline int read() {
  int X = 0;
  bool flag = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-') flag = 0;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    X = (X << 1) + (X << 3) + ch - '0';
    ch = getchar();
  }
  if (flag) return X;
  return ~(X - 1);
}
struct Merge {
  int x, y;
  void add(int A, int B) { x = A, y = B; }
} rt[SIZ + 5];
int n, Q, block, tot, num, rnk[N + 5], a[N + 5], ans[M + 5];
int f[B * 2 + 5][B + 5][B + 5];
int merge(int u, int v) {
  if (!u || !v) return u + v;
  rt[++num].add(u, v);
  return ++tot;
}
struct Block {
  int len, al, ar;
  void solve(int u, int l, int r, vector<int> vec) {
    if (l == r) {
      f[u][1][1] = rnk[vec[1]];
      return;
    }
    int mid = (l + r) >> 1;
    vector<int> vecl(1), vecr(1);
    for (int i = 1; i < vec.size(); i++)
      if (vec[i] <= mid)
        vecl.push_back(vec[i]);
      else
        vecr.push_back(vec[i]);
    solve(u << 1, l, mid, vecl);
    solve(u << 1 | 1, mid + 1, r, vecr);
    for (int i = 1, xl = 1, xr = 1; i < vec.size();
         xl += (vec[i] <= mid), xr += (vec[i] > mid), i++)
      for (int j = i - 1, yl = xl - 1, yr = xr - 1; j < vec.size();
           j++, yl += (vec[j] <= mid), yr += (vec[j] > mid)) {
        if (j == i - 1) continue;
        f[u][i][j] = merge(f[u << 1][xl][yl], f[u << 1 | 1][xr][yr]);
      }
  }
  int id[N + 5], qwq[B + 5][B + 5];
  void build() {
    vector<int> vec(1);
    for (int i = 1; i <= n; i++)
      if (a[i] >= al && a[i] <= ar) {
        vec.push_back(a[i]);
        id[i] = id[i - 1] + 1;
      } else
        id[i] = id[i - 1];
    solve(1, al, ar, vec);
    for (int i = 1; i < vec.size(); i++)
      for (int j = 1; j < vec.size(); j++) qwq[i][j] = f[1][i][j];
  }
  int query(int l, int r) { return qwq[id[l - 1] + 1][id[r]]; }
} blo[18];
int main() {
  n = read(), Q = read();
  tot = n;
  block = min(n, B);
  for (int i = 1; i <= n; i++) a[i] = read(), rnk[a[i]] = i;
  for (int i = 1; (i - 1) * block + 1 <= n; i++) {
    blo[i].al = (i - 1) * block + 1, blo[i].ar = min(i * block, n);
    blo[i].build();
  }
  for (int q = 1; q <= Q; q++) {
    int l = read(), r = read();
    ans[q] = 0;
    for (int i = 1; (i - 1) * block + 1 <= n; i++)
      ans[q] = merge(ans[q], blo[i].query(l, r));
  }
  printf("%d\n", tot);
  for (int i = 1; i <= num; i++) printf("%d %d\n", rt[i].x, rt[i].y);
  for (int i = 1; i <= Q; i++) printf("%d ", ans[i]);
}
