#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 12), MAXQ = (1 << 16), SZ = (1 << 22);
const int B = (1 << 8);
template <typename Tp>
void read(Tp &x) {
  x = 0;
  int op = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') op = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  x *= op;
}
template <typename Tp>
void CMax(Tp &x, Tp y) {
  if (y > x) x = y;
}
template <typename Tp>
void CMin(Tp &x, Tp y) {
  if (y < x) x = y;
}
int n, Q, a[N], pos[N];
int s1[SZ], s2[SZ], tot;
int ans[MAXQ];
int merge(int x, int y) {
  if (!x || !y) return x + y;
  s1[++tot] = x;
  s2[tot] = y;
  return tot;
}
struct Block {
  vector<int> val;
  vector<vector<int> > id;
  void Init(int sz) {
    val.resize(sz);
    id.resize(sz);
    for (int i = (0); i <= (sz - 1); ++i) id[i].resize(sz - i);
  }
  Block(int x = -1) {
    if (x >= 0) Init(1), val[0] = pos[x], id[0][0] = pos[x] + 1;
  }
  int ask(int l, int r) const {
    if (r < val.front() || l > val.back()) return 0;
    l = lower_bound(val.begin(), val.end(), l) - val.begin();
    r = upper_bound(val.begin(), val.end(), r) - val.begin() - 1;
    return l <= r ? id[l][r - l] : 0;
  }
  Block Merge(const Block &x, const Block &y) {
    Init(x.val.size() + y.val.size());
    merge(x.val.begin(), x.val.end(), y.val.begin(), y.val.end(), val.begin());
    for (int i = (0); i <= ((int)val.size() - 1); ++i)
      for (int j = (i); j <= ((int)val.size() - 1); ++j)
        id[i][j - i] = merge(x.ask(val[i], val[j]), y.ask(val[i], val[j]));
    return *this;
  }
} dat[N / B];
Block solve(int l, int r) {
  if (l == r) return (l);
  int mid = (l + r) >> 1;
  Block ret;
  return ret.Merge(solve(l, mid), solve(mid + 1, r));
}
int main() {
  read(n);
  read(Q);
  for (int i = (0); i <= (n - 1); ++i) read(a[i]), a[i]--, pos[a[i]] = i;
  tot = n;
  for (int i = (0); i <= ((n - 1) / B); ++i)
    dat[i] = solve(i * B, min(n - 1, (i + 1) * B - 1));
  int l, r;
  for (int i = (0); i <= (Q - 1); ++i) {
    read(l);
    read(r);
    l--;
    r--;
    for (int j = (0); j <= ((n - 1) / B); ++j)
      ans[i] = merge(ans[i], dat[j].ask(l, r));
  }
  printf("%d\n", tot);
  for (int i = (n + 1); i <= (tot); ++i) printf("%d %d\n", s1[i], s2[i]);
  for (int i = (0); i <= (Q - 1); ++i) printf("%d ", ans[i]);
  return 0;
}
