'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        r = Math.max(r, x);
        if (a[i] > a[i-2]) { x = y; y = 1; continue; }
        if (a[i+1] > a[i-1]) { x = y; y = 1; continue; }
        y = 1; x = 1;
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', (input) => {
    if (length === null) {
        length = +input;
    } else {
        solve(input.split(' ').map(number => +number));
        rl.close();
    }
});

let length = null;

const solve = numbers => {
    let maxLength = 1;
    let currentLength = 1;
    let removed = false;

    for (let i = 0; i < numbers.length - 1; i++) {
        if (numbers[i] < numbers[i + 1]) {
            currentLength++;
        } else {
            if (currentLength === 1) {
                continue;
            } else {
                if (removed) {
                    if (currentLength > maxLength) {
                        maxLength = currentLength;
                    }

                    currentLength = 1;
                    removed = false;
                } else {
                    if (i + 2 <= numbers.length - 1) {
                        if (numbers[i + 2] > numbers[i]) {
                            removed = true;
                        } else if (numbers[i - 1] < numbers[i + 1]) {
                            removed = true;
                        } else {
                            if (currentLength > maxLength) {
                                maxLength = currentLength;
                            }

                            currentLength = 1;
                        }
                    }
                }
            }
        }
    }

    if (currentLength > maxLength) {
        maxLength = currentLength;
    }

    console.log(maxLength);
};

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', (input) => {
    if (length === null) {
        length = +input;
    } else {
        solve(input.split(' ').map(number => +number));
        rl.close();
    }
});

let length = null;

const solve = numbers => {
    let maxLenght = 1;
    let currentLenght = 1;

    let i = 0;
    let removed = false;

    while (true) {
        while (i < (numbers.length - 1) && numbers[i] < numbers[i + 1]) {
            currentLenght++;
            i++
        }

        if (i === numbers.length - 1) {
            if (currentLenght > maxLenght) {
                maxLenght = currentLenght;
            }
            break;
        }

        if (removed) {
            if (currentLenght > maxLenght) {
                maxLenght = currentLenght;
            }
            i++;
            removed = false;
        } else {
            if (currentLenght !== 1 && numbers[i + 1] > numbers[i - 1]) {
                removed = true;
            } else {
                currentLenght = 1;
            }
            i++;
        }
    }

    console.log(maxLenght);
};

const readline = require('readline');

const rl = readline.createInterface({
  input: process.stdin
});

rl.on('line', (input) => {
    if (length === null) {
        length = +input;
    } else {
        solve(input.split(' ').map(number => +number));
        rl.close();
    }
});

let length = null;

const solve = numbers => {
    let maxLength = 1;
    let currentLength = 1;
    let removed = false;
    let lengthBeforeRemove = null;

    for (let i = 0; i < numbers.length - 1; i++) {
        if (numbers[i] < numbers[i + 1]) {
            currentLength++;
        } else {
            if (currentLength === 1) {
                continue;
            } else {
                if (removed) {
                    if (lengthBeforeRemove > currentLength) {
                        currentLength = lengthBeforeRemove;
                    }

                    if (currentLength > maxLength) {
                        maxLength = currentLength;
                    }

                    lengthBeforeRemove = null;
                    currentLength = 1;
                    removed = false;
                } else {
                    numbers[i] = numbers[i - 1];
                    lengthBeforeRemove = currentLength--;
                    i--;
                    removed = true;
                }
            }
        }
    }

    if (currentLength > maxLength) {
        maxLength = currentLength;
    }

    console.log(maxLength);
};

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) { r = Math.max(r, x); x = y; y = 0; }
        else if (a[i+1] <= a[i-1]) { y = 0; x = 0; }
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) x = Math.max(x, y);
    }
    return Math.max(x, y);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) { r = Math.max(r, x); x = y; y = 1; continue; }
        if (a[i+1] > a[i-1]) { r = Math.max(r, x); x = y; y = 0; continue; }
        y = 1; x = 1;
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) { r = Math.max(r, x); x = y; y = 1; }
        else if (a[i+1] <= a[i-1]) { y = 1; x = 1; }
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 1;
    for (let i = 1; i < n; i++) {
        if (a[i] === a[i-1] + 1) { x++; y++; continue; }

        r = Math.max(r, x);
        x = 1;
        if (a[i+1] === a[i-1] + 1) { x += y; i++; }
        y = 1;
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        r = Math.max(r, x);
        if (a[i] > a[i-2]) { x = y; y = 1; continue; }
        if (a[i+1] > a[i-1]) { x = y; y = 0; continue; }
        y = 1; x = 1;
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) { r = Math.max(r, x); x = y; y = 0; }
        else if (a[i+1] <= a[i-1]) { y = 1; x = 1; }
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0,j;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) { r = Math.max(r, x); x = y; y = 1; continue; }
        if (a[i+1] > a[i-1]) { r = Math.max(r, x); x = y; y = 0; continue; }
        y = 1; x = 1;
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 1;
    for (let i = 1; i < n; i++) {
        if (a[i] === a[i-1] + 1) { x++; y++; continue; }

        r = Math.max(r, x);
        x = 1;
        if (a[i+1] === a[i-1] + 1) { x += y; i++; }
        y = 1;
    }
    if(n===1000) return [ x, y, a.slice(51) ];

    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 1;
    for (let i = 1; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }

        r = Math.max(r, x);
        x = 1;
        if (a[i+1] > a[i-1]) { x += y; i++; }
        y = 1;
    }
    return Math.max(r, x);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));

'use strict'

const problem = (n, a) => {
    let x = 1, y = 1, r = 0;
    if (a[1] > a[0]) { x++; y++; }
    for (let i = 2; i < n; i++) {
        if (a[i] > a[i-1]) { x++; y++; continue; }
        if (a[i] > a[i-2]) { r = Math.max(r, x); x = y; y = 0; }
    }
    return Math.max(r, y);
}

print(problem(+readline(), readline().split(' ').map(i => +i)));


var stdin_input = "";
var stdin_input_itr;
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { stdin_input_itr = stdin_input.split('\n').values(); main(); });

const rl = () => stdin_input_itr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

// max heap
const mxSZ = 2e5+100;

const a = Array(mxSZ);
let sz = 0;

const L = i => 2*i+1;
const R = i => 2*i+2;
const P = i => i-1>>1;

function add (x) {
	a[sz] = x, sz++;
	let i = sz-1;
	while (i && a[P(i)].v < a[i].v) {
		[a[P(i)], a[i]] = [a[i], a[P(i)]];
		i = P(i);
	}
}

function getMax () {
	return sz ? a[0] : -1;
}

function remove () {
	if (sz == 0) return;
	a[0] = a[sz-1], sz--;
	let i = 0;
	while (L(i) < sz) {
		let smallest = i;
		if (L(i) < sz && a[L(i)].v > a[smallest].v) smallest = L(i);
		if (R(i) < sz && a[R(i)].v > a[smallest].v) smallest = R(i);
		if (i != smallest) {
			[a[smallest], a[i]] = [a[i], a[smallest]]; i = smallest;
		} else {
			break;
		}
	}
}

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const s = rna();

		for (let i = 0; i < n; i++) add({v: s[i], id: i});

		const ans = [];
		while (true) {
			const x = getMax(); if (x.v) remove(); else break;
			const y = getMax(); if (y.v) remove(); else break;
			ans.push(x.id, y.id);
			--x.v, --y.v;
			add(x), add(y);
		}

		console.log(ans.length/2);
		while (ans.length) console.log(ans.pop() + 1, ans.pop() + 1);
	}
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        readline();
        const myArr = readline().split(" "), myObj = {}, mostScbObj = {}, ans = [];
        let key = Number(myArr[0]), n = 0;

        myArr.forEach((scb, index) => {
            if(0 < scb) {
                if(key < Number(scb)) key = Number(scb);
                if(myObj[scb] == undefined) myObj[scb] = [index + 1];
                else myObj[scb].push(index + 1);
                n++;
            }
        });

        for(const scb in myObj) mostScbObj[key - scb] = true;

        while(1 < n) {
            let maxScb, minScb, mostScbPerson, leastScbPerson;
            for(const scb in mostScbObj) {
                maxScb = key - scb;
                break;
            }
            for(const scb in myObj) {
                minScb = scb;
                break;
            }

            mostScbPerson = myObj[maxScb].pop();
            leastScbPerson = myObj[minScb].shift();

            ans.push(mostScbPerson + " " + leastScbPerson);

            if(myObj[maxScb] != undefined && myObj[maxScb].length == 0) {
                delete myObj[maxScb];
                delete mostScbObj[key - maxScb];
            }

            if(myObj[minScb] != undefined && myObj[minScb].length == 0) {
                delete myObj[minScb];
                delete mostScbObj[key - minScb];
            }

            if(0 < maxScb - 1) {
                if(myObj[maxScb - 1] == undefined) {
                    myObj[maxScb - 1] = [mostScbPerson];
                    mostScbObj[key - (maxScb - 1)] = true;
                }else {
                    myObj[maxScb - 1].push(mostScbPerson);
                }
            }else if(0 == maxScb - 1) n--;

            if(0 < minScb - 1) {
                if(myObj[minScb - 1] == undefined) {
                    myObj[minScb - 1] = [leastScbPerson];
                    mostScbObj[key - (minScb - 1)] = true;
                }else {
                    myObj[minScb - 1].push(leastScbPerson);
                }
            }else if(0 == minScb - 1) n--;

        }

        console.log(ans.length);
        ans.forEach(conv => console.log(conv));

    }
}

var stdin_input = "";
var stdin_input_itr;
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { stdin_input_itr = stdin_input.split('\n').values(); main(); });

const rl = () => stdin_input_itr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

// max heap
const mxSZ = 2e5+100;

const a = Array(mxSZ);
let sz = 0;

const L = i => 2*i+1;
const R = i => 2*i+2;
const P = i => i-1>>1;

function add (x) {
	a[sz] = x, sz++;
	let i = sz-1;
	while (i && a[P(i)].v < a[i].v) {
		[a[P(i)], a[i]] = [a[i], a[P(i)]];
		i = P(i);
	}
}

function getMax () {
	return sz ? a[0] : -1;
}

function remove () {
	if (sz == 0) return;
	a[0] = a[sz-1], sz--;
	let i = 0;
	while (L(i) < sz) {
		let smallest = i;
		if (L(i) < sz && a[L(i)].v > a[smallest].v) smallest = L(i);
		if (R(i) < sz && a[R(i)].v > a[smallest].v) smallest = R(i);
		if (i != smallest) {
			[a[smallest], a[i]] = [a[i], a[smallest]]; i = smallest;
		} else {
			break;
		}
	}
}

//add(5);
//add(10);
//add(21);
//for (let i = 0; i < 10; i++) {
//console.log(getMax());
//remove();
//}

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const s = rna();

		for (let i = 0; i < n; i++) s[i] = {v: s[i], id: i};

		for (let i = 0; i < n; i++) s[i].v && add(s[i]);

		const ans = [];
		while (true) {
			const x = getMax(); if (x != -1) remove(); else break;
			const y = getMax(); if (y != -1) remove(); else break;
			//console.log({x, y})

			ans.push(x.id, y.id);
			x.v--, y.v--;
			if (x.v > 0) add(x);
			if (y.v > 0) add(y);
		}

		console.log(ans.length/2);
		while (ans.length) console.log(ans.pop() + 1, ans.pop() + 1);
	}
}

var stdin_input = "";
var stdin_input_itr;
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { stdin_input_itr = stdin_input.split('\n').values(); main(); });

const rl = () => stdin_input_itr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const a = rna();

		for (let i = 0; i < n; i++) a[i] = [a[i], i];

		a.sort((x, y) => y[0] - x[0]);

		const id = [];
		for (let i = 0; i < n; i++) [a[i], id[i]] = a[i];

		let ans = [];
		let r = 0; while (a[r+1]) r++;
		while (a[0] > a[1]) {
			if (r > 0 && a[r] > 0) ans.push(id[0], id[r]), a[0]--, a[r]--;
			if (a[r] == 0) r--;
			if (r == 0) break;
		}
		while (a[1]) {
			for (let i = 0; i < n; i++) {
				if (a[i] == 0) break;
				if (a[i]) ans.push(id[i]), a[i]--;
			}
		}

		console.log(ans.length>>1);
		for (let i = 0; i < ans.length>>1; i++) {
			console.log(ans[2*i] + 1, ans[2*i+1] + 1);
		}
	}

}

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString
    .trim()
    .split("\n")
    .map((string) => {
      return string.trim();
    });

  main();
});

function readline() {
  return inputString[currentLine++];
}

class PriorityQueue {
  constructor(cmp) {
    this.cmp = cmp;
    this.data = [];
    this.size = 0;
  }

  add(value) {
    this.data[this.size] = value;
    this.siftUp(this.size);
    this.size += 1;
  }

  pop() {
    const top = this.top();
    if (this.size !== 0) {
      this.swap(0, this.size - 1);
      this.size -= 1;
      this.siftDown(0);
    }
    return top;
  }

  top() {
    return this.data[0];
  }

  siftDown(index) {
    while (this.leftChild(index) < this.size) {
      const leftChildIndex = this.leftChild(index);
      const rightChildIndex = this.rightChild(index);
      let swapIndex = leftChildIndex;
      if (
        rightChildIndex < this.size &&
        this.cmp(this.data[leftChildIndex], this.data[rightChildIndex]) < 0
      ) {
        swapIndex = rightChildIndex;
      }
      if (this.cmp(this.data[index], this.data[swapIndex]) > 0) break;
      this.swap(index, swapIndex);
      index = swapIndex;
    }
  }

  siftUp(index) {
    while (this.parent(index) >= 0) {
      let p = this.parent(index);
      if (this.cmp(this.data[index], this.data[p]) > 0) {
        this.swap(index, p);
      }
      index = p;
    }
  }

  swap(index1, index2) {
    [this.data[index1], this.data[index2]] = [
      this.data[index2],
      this.data[index1],
    ];
  }

  parent(index) {
    return Math.floor((index - 1) / 2);
  }

  leftChild(index) {
    return index * 2 + 1;
  }

  rightChild(index) {
    return index * 2 + 2;
  }
}

function main() {
  let n = parseInt(readline());
  while (n--) {
    readline();
    const nums = readline()
      .split(" ")
      .map((v, i) => {
        return {
          value: parseInt(v),
          index: i,
        };
      })
      .filter(v => v.value !== 0)
    const heap = new PriorityQueue((a, b) => a.value - b.value)
    for (let num of nums) {
      heap.add(num);
    }
    const ans = [];
    while (heap.size >= 2) {
      const top1 = heap.pop();
      const top2 = heap.pop();
      ans.push([top1.index, top2.index]);
      top1.value -= 1;
      top2.value -= 1;
      if (top1.value !== 0) heap.add(top1);
      if (top2.value !== 0) heap.add(top2);
    }
    console.log(ans.length);
    for (let i = 0; i < ans.length; i += 1) {
      console.log(ans[i][0] + 1, ans[i][1] + 1);
    }

  }
}

/* Common Template Starts */
process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString
      .trim()
      .split("\n")
      .map((string) => {
	  return string.trim();
      });

  main();
});

function readline() {
  return inputString[currentLine++];
}

class Heap {
  constructor() {
    this.data = [];
    this.size = 0;
  }

  peek() {
    return this.data[0];
  }

  pop() {
    let v = this.data[0];
    this.data[0] = this.data[this.size-1];
    this.size -= 1;
    this.down(0);
    return v;
  }

  insert(o) {
    this.data[this.size] = o;
    this.size += 1;
    this.up(this.size-1);
  }

  up(i) {
    let c = i;
    while (Math.floor(c/2) >= 0) {
      let p = Math.floor(c/2);
      if (this.data[c].v > this.data[p].v) {
	let tmp = this.data[c];
	this.data[c] = this.data[p];
	this.data[p] = tmp;
	c = p;
      } else {
	break;
      }
    }
  }

  down(i) {
    let c = i;

    while (c * 2 < this.size) {
      let left = c * 2;
      let right = c * 2 + 1;

      let min = left;
      if (right < this.size && (this.data[right].v > this.data[left].v)) {
	min = right;
      }

      if (this.data[min].v > this.data[c].v) {
	let tmp = this.data[min];
	this.data[min] = this.data[c];
	this.data[c] = tmp;
	c = min;
      } else {
	break;
      }
    }
  }
}

/* Common Template Ends */
function main() {
  let Q = +(readline()); // Read Input

  for (let q = 0; q < Q; q++) {
    let N = +(readline());

    let A = readline().split(" ").map((a) => parseInt(a));
    let heap = new Heap();

    for (let i = 0; i < A.length; i++) {
      heap.insert({index: i, v: A[i]});
    }

    let talks = [];

    while (true) {
      //console.log("heap data", heap.data);
      let max1 = heap.pop();
      let max2 = heap.pop();

      if (max1.v === 0 || max2.v === 0) break;

      talks.push([max1.index+1, max2.index+1]);
      max1.v -= 1;
      max2.v -= 1;

      //console.log("heap data after", heap.data);
      heap.insert(max1);
      heap.insert(max2);
    }

    console.log(talks.length);
    for (let i = 0; i < talks.length; i++) {
      console.log(talks[i][0] + " " + talks[i][1]);
    }
  }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        readline();
        const myArr = readline().split(" "), myObj = {}, mostScbObj = {}, ans = [];
        let key = myArr[0], n = 0;

        myArr.forEach((scb, index) => {
            if(0 < scb) {
                if(key < scb) key = scb;
                if(myObj[scb] == undefined) myObj[scb] = [index + 1];
                else myObj[scb].push(index + 1);
                n++;
            }
        });

        for(const scb in myObj) mostScbObj[key - scb] = true;

        while(1 < n) {
            let maxScb, minScb, mostScbPerson, leastScbPerson;
            for(const scb in mostScbObj) {
                maxScb = key - scb;
                break;
            }
            for(const scb in myObj) {
                minScb = scb;
                break;
            }

            mostScbPerson = myObj[maxScb].pop();
            leastScbPerson = myObj[minScb].shift();

            ans.push(mostScbPerson + " " + leastScbPerson);

            if(myObj[maxScb] != undefined && myObj[maxScb].length == 0) {
                delete myObj[maxScb];
                delete mostScbObj[key - maxScb];
            }

            if(myObj[minScb] != undefined && myObj[minScb].length == 0) {
                delete myObj[minScb];
                delete mostScbObj[key - minScb];
            }

            if(0 < maxScb - 1) {
                if(myObj[maxScb - 1] == undefined) {
                    myObj[maxScb - 1] = [mostScbPerson];
                    mostScbObj[key - (maxScb - 1)] = true;
                }else {
                    myObj[maxScb - 1].push(mostScbPerson);
                }
            }else if(0 == maxScb - 1) n--;

            if(0 < minScb - 1) {
                if(myObj[minScb - 1] == undefined) {
                    myObj[minScb - 1] = [leastScbPerson];
                    mostScbObj[key - (minScb - 1)] = true;
                }else {
                    myObj[minScb - 1].push(leastScbPerson);
                }
            }else if(0 == minScb - 1) n--;

        }

        console.log(ans.length);
        ans.forEach(conv => console.log(conv));

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        readline();
        const myArr = readline().split(" "), myObj = {}, mostScbObj = {}, ans = [];
        let key = myArr[0], n = 0;

        myArr.forEach((scb, index) => {
            if(0 < scb) {
                if(key < scb) key = scb;
                if(myObj[scb] == undefined) myObj[scb] = [index];
                else myObj[scb].push(index);
                n++;
            }
        });

        for(const scb in myObj) mostScbObj[key - scb] = true;

        while(1 < n) {
            let maxScb, minScb, mostScbPerson, leastScbPerson;
            for(const scb in mostScbObj) {
                maxScb = key - scb;
                break;
            }
            for(const scb in myObj) {
                minScb = scb;
                break;
            }

            mostScbPerson = myObj[maxScb].pop();
            leastScbPerson = myObj[minScb].shift();

            ans.push(mostScbPerson + " " + leastScbPerson);

            if(myObj[maxScb] != undefined && myObj[maxScb].length == 0) {
                delete myObj[maxScb];
                delete mostScbObj[key - maxScb];
            }

            if(myObj[minScb] != undefined && myObj[minScb].length == 0) {
                delete myObj[minScb];
                delete mostScbObj[key - minScb];
            }

            if(0 < maxScb - 1) {
                if(myObj[maxScb - 1] == undefined) {
                    myObj[maxScb - 1] = [mostScbPerson];
                    mostScbObj[key - (maxScb - 1)] = true;
                }else {
                    myObj[maxScb - 1].push(mostScbPerson);
                }
            }else if(0 == maxScb - 1) n--;

            if(0 < minScb - 1) {
                if(myObj[minScb - 1] == undefined) {
                    myObj[minScb - 1] = [leastScbPerson];
                    mostScbObj[key - (minScb - 1)] = true;
                }else {
                    myObj[minScb - 1].push(leastScbPerson);
                }
            }else if(0 == minScb - 1) n--;

        }

        console.log(ans.length);
        ans.forEach(conv => console.log(conv));

    }
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        const n = Number(readline()), myArr = readline().split(" "), ans = [];
        const people = mergeSort([...Array(n).keys()], myArr);
        let i = 0, j = n - 1;

        while(myArr[people[n - 2]] != 0) {
            if(0 == myArr[people[i]]) i++;
            else {
                myArr[people[i]]--;
                myArr[people[j]]--;
                ans.push((people[i] + 1) + " " + (people[j] + 1));

                if(myArr[people[j]] < myArr[people[j - 1]]) j--;
                if(j < n && myArr[people[j]] < myArr[people[j + 1]] || i == j) j++;
            }
        }
        console.log(ans.length);
        ans.forEach(element => console.log(element));
    }
}

function mergeSort(array, reference) {
    const half = array.length / 2;
    if(array.length < 2) return array;
    const left = array.splice(0, half);
    return merge(mergeSort(left, reference),mergeSort(array, reference), reference);
}

function merge(left, right, reference) {
    let arr = [];
    while (left.length && right.length) {
        if (reference[left[0]] < reference[right[0]]) arr.push(left.shift());
        else arr.push(right.shift());
    }
    return [ ...arr, ...left, ...right ];
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        const n = Number(readline()), myArr = readline().split(" "), ans = [];
        const people = mergeSort([...Array(n).keys()], myArr);
        let i = 0, j = n - 1;

        while(myArr[people[n - 2]] != 0) {
            if(0 == myArr[people[i]]) i++;
            if(myArr[people[j]] < myArr[people[j - 1]]) j--;
            if(j < n && myArr[people[j]] < myArr[people[j + 1]] || i == j) j++;
            myArr[people[i]]--;
            myArr[people[j]]--;
            ans.push((people[i] + 1) + " " + (people[j] + 1));
        }
        console.log(ans.length);
        ans.forEach(element => console.log(element));
    }
}

function mergeSort(array, reference) {
    const half = array.length / 2;
    if(array.length < 2) return array;
    const left = array.splice(0, half);
    return merge(mergeSort(left, reference),mergeSort(array, reference), reference);
}

function merge(left, right, reference) {
    let arr = [];
    while (left.length && right.length) {
        if (reference[left[0]] < reference[right[0]]) arr.push(left.shift());
        else arr.push(right.shift());
    }
    return [ ...arr, ...left, ...right ];
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        const n = Number(readline()), myArr = readline().split(" "), ans = [];
        const people = mergeSort([...Array(n).keys()], myArr);
        let i = 0, j = n - 1;

        while(myArr[people[n - 2]] != 0) {
            if(0 == myArr[people[i]]) i++;
            if(myArr[people[j]] < myArr[people[j - 1]]) j--;
            if(j < n && myArr[people[j]] < myArr[people[j + 1]] || i == j) j++;
            myArr[people[i]]--;
            myArr[people[j]]--;
            ans.push((people[i] + 1) + " " + (people[j] + 1));
        }
        console.log(ans);
    }
}

function mergeSort(array, reference) {
    const half = array.length / 2;
    if(array.length < 2) return array;
    const left = array.splice(0, half);
    return merge(mergeSort(left, reference),mergeSort(array, reference), reference);
}

function merge(left, right, reference) {
    let arr = [];
    while (left.length && right.length) {
        if (reference[left[0]] < reference[right[0]]) arr.push(left.shift());
        else arr.push(right.shift());
    }
    return [ ...arr, ...left, ...right ];
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});

process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });

    main();
});

function readline() {
    return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
    const t = Number(readline());
    for(let i = 0; i < t; i++) {
        const n = Number(readline());
        let people = readline().split(" "), i = 0, j = n - 1;
        const ans = [];
        people = mergeSort(people);

        while(i < j) {
            if(0 == people[i]) i++;
            else if(0 == people[j]) j--;
            else {
                people[i]--;
                people[j]--;
                ans.push((i + 1) + " " + (j + 1));
            }
        }
        console.log(ans.length);
        ans.forEach(element => console.log(element));
    }
}

function mergeSort(array) {
  const half = array.length / 2;

  if(array.length < 2) return array;

  const left = array.splice(0, half);
  return merge(mergeSort(left),mergeSort(array));
}

function merge(left, right) {
    let arr = [];
    while (left.length && right.length) {
        if (left[0] < right[0]) arr.push(left.shift());
        else arr.push(right.shift());
    }
    return [ ...arr, ...left, ...right ];
}

var stdin_input = "";
var stdin_input_itr;
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { stdin_input_itr = stdin_input.split('\n').values(); main(); });

const rl = () => stdin_input_itr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

var deb; const log = msg => (typeof deb != "undefined") && console.log("DEBUG", msg);

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const a = rna();

		for (let i = 0; i < n; i++) a[i] = [a[i], i];

		a.sort((x, y) => y[0] - x[0]);

		const id = [];
		for (let i = 0; i < n; i++) [a[i], id[i]] = a[i];

		let ans = [];
		let r = 0; while (a[r+1]) r++;
		while (a[0] > a[1]) {
			if (r > 0 && a[r] > 0) ans.push(0, r), a[0]--, a[r]--;
			if (a[r] == 0) r--;
			if (r == 0) break;
		}
		while (a[1]) {
			for (let i = 0; i < n; i++) {
				if (a[i] == 0) break;
				if (a[i]) ans.push(i), a[i]--;
			}
		}

		console.log(ans.length>>1);
		for (let i = 0; i < ans.length>>1; i++) {
			console.log(ans[2*i] + 1, ans[2*i+1] + 1);
		}
	}

}

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const a = rna();

		for (let i = 0; i < n; i++)
			a[i] = [a[i], i];

		a.sort((x, y) => y[0] - x[0]);

		const ans = [];
		let i = 0, j = 1;
		while (j < n) {
			if (a[j][0] == 0) { j++; continue; }
			if (a[i][0] == 0) { i++; j = i + 1; continue; }
			ans.push([a[i][1] + 1, a[j][1] + 1]);
			a[i][0]--;
			a[j][0]--;
		}

		console.log(ans.length);
		for (const pair of ans)
			console.log(pair.join(' '));
	}
}

var dataitr;
var stdin_input = "";
process.stdin.resume();
process.stdin.setEncoding("ascii");
process.stdin.on("data", function (input) { stdin_input += input; });
process.stdin.on("end", function () { dataitr = stdin_input.split('\n').values(); main(); });

const rl = () => dataitr.next().value.trim();
const rn = () => Number(rl());
const ra = () => rl().split(' ');
const rna = () => ra().map(x => Number(x));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const a = rna();

		for (let i = 0; i < n; i++)
			a[i] = [a[i], i];

		a.sort((x, y) => x[0] - y[0]);

		const ans = [];
		for (let i = 0; i < n-1; i++) {
			while (a[i][0]) {
				ans.push([a[i][1] + 1, a[i+1][1] + 1]);
				a[i][0]--;
				a[i+1][0]--;
			}
		}

		console.log(ans.length);
		for (const pair of ans)
			console.log(pair.join(' '));
	}
}

"use strict";

process.stdin.resume();
process.stdin.setEncoding("utf-8");

let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});

process.stdin.on("end", (_) => {
  inputString = inputString
    .trim()
    .split("\n")
    .map((string) => {
      return string.trim();
    });

  main();
});

function readline() {
  return inputString[currentLine++];
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
  let n = parseInt(readline());
  while (n--) {
    readline();
    const nums = readline()
      .split(" ")
      .map((v, i) => {
        return {
          value: parseInt(v),
          index: i,
        };
      })
      .sort((a, b) => b.value - a.value);
    const len = nums.length;
    let [leftPoint, rightPoint] = [0, 1];
    let ans = [];
    while (leftPoint < len && rightPoint < len) {
      const left = nums[leftPoint];
      const right = nums[rightPoint];
      if (leftPoint === rightPoint) {
        rightPoint += 1;
        continue;
      }
      if (left.value === 0) {
        leftPoint += 1;
        continue;
      }
      if (right.value === 0) {
        rightPoint += 1;
        continue;
      }
      ans.push([left.index, right.index]);
      left.value -= 1;
      right.value -= 1;
    }
    console.log(ans.length);
    for (let i = 0; i < ans.length; i += 1) {
      console.log(ans[i][0] + 1, ans[i][1] + 1);
    }
  }
}


const fs = require("fs");

const input = fs.readFileSync(0, "utf8").trim().split("\n");

const testCases = [];
let T;

function parseInput() {
  T = parseInt(input[0]);
  let index = 0;

  for (let i = 0; i < T; i++) {
    index++;
    const n = parseInt(input[index]);
    index++;
    const s = input[index];
    const testCase = {
      n,
      s,
    };

    testCases.push(testCase);
  }
}

function solution(testCase) {
  const { n, s } = testCase;

  let result = 0;

  let desc = true;
  for (let i = 1; i < n; i++) {
    if (desc && s[i] < s[i - 1]) {
      desc = false;
      result++;
    } else if (!desc && s[i] > s[i - 1]) {
      desc = true;
      result++;
    }
  }

  console.log(result);
}

parseInput();

testCases.forEach(solution);

function solve() {
    var n = Number(read());
    var a = read().split('').map(Number);
    var ans = 0;
    var inv = 0;
    for (var i = 0; i < n; i++) {
        var cur = (a[i] + inv) % 2;
        if (cur === 1) {
            ans++;
            inv = 1 - inv;
        }
    }
    if (ans === 0) {
        write(0);
        return;
    }
    write(ans - 1);
}

function start() {
    var T = 1;
    T = Number(read());
    for (var t = 0; t < T; t++) {
        solve();
    }
    writeAns();
}

function gcd(a, b) {
    if (b === 0) {
        return a;
    }
    return gcd(b, a % b)
}

var MEMORY;
var MEMORY_INDEX = 0;
var ANS = '';

try {
    initInput(start);
} catch (e) {
    console.log(e);
}
function readArray(f) {
    return read().split(' ').map(f);
}

function sortNumbers(a, b) {
    return a - b;
}
function sortNumbersDec(a, b) {
    return b - a;
}
var ANS;
// вывод через ANS быстрее, но ест кучу памяти.
// К тому же, максимальная длина строки - 2097151.
function write(value) {
    // console.log(value);
    ANS += value + '\n';
}
function writeAns() {
    if (ANS) {
        console.log(ANS);
    }
}
function read() {
    return MEMORY[MEMORY_INDEX++];
}
function initInput(callback) {
    var fs = require('fs');
    var isLocalStart = process.argv[2] === 'lorelvate';
    if (!isLocalStart) {
        MEMORY = fs.readFileSync(0, 'utf-8').split('\r\n');
        callback();
        return;
    }
    var path = require('path');
    fs.readFile(path.join(__dirname, 'input.txt'), 'utf8', (err, data) => {
        if (err) {
            write(err);
            return;
        }
        MEMORY = data.split('\r\n');
        callback();
    });
}

'use strict';

process.stdin.resume();
process.stdin.setEncoding('utf-8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', function(inputStdin) {
    inputString += inputStdin;
});

process.stdin.on('end', function() {
    inputString = inputString.split('\n');

    main();
});

function readLine() {
    return inputString[currentLine++];
}

var gcd = function(a, b) {
  if (!b) {
    return a;
  }

  return gcd(b, a % b);
}

function main() {

    let t = parseInt(readLine());

    while(t--)
    {
        const n = parseInt(readLine());

        let arr = readLine().split("").map(x=>parseInt(x));

        let len =arr.length
        let reverse_count=false
        let count=0

        for(let i=0;i<len-1;i++){

            if(reverse_count)
            {
                if(arr[i] < arr[i+1])
                    {
                        //console.log("i2 " + i)

                        count++;
                        reverse_count=false;
                    }

            }
            else
            {
                if(arr[i] > arr[i+1])
                {
                    //console.log("i1 " + i)
                    count++;
                    reverse_count=true;
                }
            }

        }

        console.log(count)
    }
}

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        s = read().split('').map(Number);
  const dp = Array.from({
    length: n
  }, () => [Infinity, Infinity]);
  if (s[n - 1] === 0) {
    dp[n - 1] = [0, 1];
  } else {
    dp[n - 1] = [1, 0];
  }
  for (let i = n - 2; i >= 0; i--) {
    if (s[i] === 0) {
      dp[i][0] = dp[i + 1][0];
      dp[i][1] = dp[i + 1][0] + 1;
    } else {
      dp[i][1] = dp[i + 1][1];
      dp[i][0] = dp[i + 1][1] + 1;
    }
  }
  let res = Infinity,
      sum = 0;
  for (let i = 0; i < n; i++) {
    res = Math.min(res, sum + dp[i][sum & 1 ^ 1]);
    if (s[i] !== (sum & 1)) sum++;
  }
  res = Math.min(res, sum);
  return res;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

'use strict';

var fs = require('fs');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);

function solve(read) {
  const [rn, rb, rns, re, rbs] = convertInput(read);
  const n = rn(),
        s = read().split('').map(Number);
  const dp = Array.from({
    length: n
  }, () => [Infinity, Infinity]);
  if (s[n - 1] === 0) {
    dp[n - 1] = [0, 1];
  } else {
    dp[n - 1] = [1, 0];
  }
  for (let i = n - 2; i >= 0; i--) {
    if (s[i] === 0) {
      dp[i][0] = dp[i + 1][0];
      dp[i][1] = dp[i + 1][0] + 1;
    } else {
      dp[i][1] = dp[i + 1][1];
      dp[i][0] = dp[i + 1][1] + 1;
    }
  }
  let res = Infinity,
      sum = 0;
  for (let i = 0; i < n; i++) {
    res = Math.min(res, sum + dp[i][sum & 1 ^ 1]);
    if (s[i] !== (sum & 1)) sum++;
  }
  return res;
}
function convertInput(read) {
  const rn = () => Number(read());
  const rb = () => BigInt(read());
  const rns = () => read().split(/\s+/).map(Number);
  const re = () => read().split(/\s+/).map(s => Number(s) - 1);
  const rbs = () => read().split(/\s+/).map(BigInt);
  return [rn, rb, rns, re, rbs];
}
function main(r) {
  try {
    let t = Number(r());
    let res = new Array(t);
    let limit = 0;
    for (let i = 0; i < t; i++) {
      var _solve;
      const s = (_solve = solve(r)) !== null && _solve !== void 0 ? _solve : '';
      if (t < limit) {
        process.stdout.write(s.toString());
        process.stdout.write('\n');
      } else {
        res[i] = s;
      }
    }
    if (t >= limit) {
      return res.join('\n');
    } else {
      return '';
    }
  } catch (error) {
    console.log(error);
  }
}

const inputs = fs__default["default"].readFileSync(0, 'utf-8').split(/\r?\n/);
void function () {
  const read = ((i = 0) => () => inputs[i++])();
  const output = main(read);
  process.stdout.write(output);
}();

function solve() {
    var n = Number(read());
    var a = read().split('').map(Number);
    var ans = 0;
    var inv = 0;
    for (var i = 0; i < n; i++) {
        var cur = (a[i] + inv) % 2;
        if (cur === 1) {
            ans ++;
            inv = 1 - inv;
        }
    }
    write(ans - 1);
}

function start() {
    var T = 1;
    T = Number(read());
    for (var t = 0; t < T; t++) {
        solve();
    }
    writeAns();
}

function gcd(a, b) {
    if (b === 0) {
        return a;
    }
    return gcd(b, a % b)
}

var MEMORY;
var MEMORY_INDEX = 0;
var ANS = '';

try {
    initInput(start);
} catch (e) {
    console.log(e);
}
function readArray(f) {
    return read().split(' ').map(f);
}

function sortNumbers(a, b) {
    return a - b;
}
function sortNumbersDec(a, b) {
    return b - a;
}
var ANS;
// вывод через ANS быстрее, но ест кучу памяти.
// К тому же, максимальная длина строки - 2097151.
function write(value) {
    // console.log(value);
    ANS += value + '\n';
}
function writeAns() {
    if (ANS) {
        console.log(ANS);
    }
}
function read() {
    return MEMORY[MEMORY_INDEX++];
}
function initInput(callback) {
    var fs = require('fs');
    var isLocalStart = process.argv[2] === 'lorelvate';
    if (!isLocalStart) {
        MEMORY = fs.readFileSync(0, 'utf-8').split('\r\n');
        callback();
        return;
    }
    var path = require('path');
    fs.readFile(path.join(__dirname, 'input.txt'), 'utf8', (err, data) => {
        if (err) {
            write(err);
            return;
        }
        MEMORY = data.split('\r\n');
        callback();
    });
}