#include <bits/stdc++.h>
using namespace std;
inline int read() {
  int f = 1, x = 0;
  char s = getchar();
  while (s < '0' || s > '9') {
    if (s == '-') f = -1;
    s = getchar();
  }
  while (s >= '0' && s <= '9') {
    x = x * 10 + s - 48;
    s = getchar();
  }
  return f * x;
}
const int N = 1e5 + 5;
pair<int, int> cz[N * 22];
int a[N], p[N], cnt, n, q, mx, ans[N];
struct seg {
  vector<int> num;
  map<pair<int, int>, int> ma;
} t[N << 2];
void build(int x, int l, int r) {
  for (int i = l; i <= r; i++) t[x].num.push_back(p[i]);
  sort(t[x].num.begin(), t[x].num.end());
  if (l == r) return;
  int mid = l + r >> 1;
  build(x << 1, l, mid);
  build(x << 1 | 1, mid + 1, r);
}
int ask(int x, int l, int r) {
  int L = lower_bound(t[x].num.begin(), t[x].num.end(), l) - t[x].num.begin();
  int R =
      upper_bound(t[x].num.begin(), t[x].num.end(), r) - t[x].num.begin() - 1;
  if (L > R) return 0;
  if (L == R) return t[x].num[L];
  int p = t[x].ma[make_pair(L, R)];
  if (p) return p;
  int ls = ask(x << 1, l, r), rs = ask(x << 1 | 1, l, r);
  if (!ls || !rs) return t[x].ma[make_pair(L, R)] = ls + rs;
  cz[++cnt] = make_pair(ls, rs);
  return t[x].ma[make_pair(L, R)] = cnt;
}
int main() {
  cnt = n = read(), q = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    p[a[i]] = i;
  }
  build(1, 1, n);
  for (int i = 1; i <= q; i++) {
    int l = read(), r = read();
    ans[i] = ask(1, l, r);
  }
  printf("%d\n", cnt);
  for (int i = n + 1; i <= cnt; i++)
    printf("%d %d\n", cz[i].first, cz[i].second);
  for (int i = 1; i <= q; i++) printf("%d ", ans[i]);
  return 0;
}
