#include <bits/stdc++.h>
const int MAXN = 1 << 12;
const int MAXM = 1 << 16;
const int B = 1 << 8;
int n, q, a[MAXN + 5], bel[MAXN + 5], ps[MAXN + 5];
std::vector<std::pair<int, int> > v;
inline int merge(int x, int y) {
  if (!x) return y;
  if (!y) return x;
  v.push_back(std::make_pair(x, y));
  return v.size() + n;
}
struct Node {
  std::vector<int> S;
  int ps[B][B];
  Node(int x = -1) {
    if (x != -1) S.push_back(::ps[x]), ps[0][0] = ::ps[x] + 1;
  }
  inline int query(int l, int r) {
    l = std::lower_bound(S.begin(), S.end(), l) - S.begin();
    r = std::upper_bound(S.begin(), S.end(), r) - S.begin() - 1;
    if (l > r) return 0;
    return ps[l][r];
  }
  friend Node operator+(Node A, Node B) {
    Node C;
    C.S.resize(A.S.size() + B.S.size());
    merge(A.S.begin(), A.S.end(), B.S.begin(), B.S.end(), C.S.begin());
    for (int i = 0; i <= (int)C.S.size() - 1; ++i) {
      for (int j = i; j <= (int)C.S.size() - 1; ++j) {
        int l = C.S[i], r = C.S[j];
        C.ps[i][j] = merge(A.query(l, r), B.query(l, r));
      }
    }
    return C;
  }
} b[(MAXN / B) + 5];
inline Node work(int l, int r) {
  if (l == r) return Node(l);
  int mid = (l + r) >> 1;
  return work(l, mid) + work(mid + 1, r);
}
int ans[MAXM + 5];
int main() {
  scanf("%d%d", &n, &q);
  for (int i = 0; i <= n - 1; ++i) scanf("%d", a + i), a[i]--, ps[a[i]] = i;
  for (int i = 0; i <= n - 1; ++i) bel[i] = i / B;
  int m = bel[n - 1];
  for (int i = 0; i <= m; ++i)
    b[i] = work(i * B, std::min(n - 1, (i + 1) * B - 1));
  for (int i = 1; i <= q; ++i) {
    int l, r;
    scanf("%d%d", &l, &r);
    --l;
    --r;
    int res = 0;
    for (int i = 0; i <= m; ++i) res = merge(res, b[i].query(l, r));
    ans[i] = res;
  }
  printf("%d\n", n + (int)v.size());
  for (auto x : v) printf("%d %d\n", x.first, x.second);
  for (int i = 1; i <= q; ++i) printf("%d ", ans[i]);
  puts("");
  return 0;
}
