	// this code is writen by ashraf
	var r = readline();

	for(var p=0;p<r;p++){
		var s = readline().split(" ").map(function(a){return (+a)});
        var x1 = s[0]+s[1];x1=Math.floor(x1/2);
        var x2 = s[2]+s[3];;x2=Math.floor(x2/2);
        if(x1===x2){x2+=1;}
       print(x1 + " " + x2)
	}

var n = parseInt(readline());

for (var t =0;t<n;t++) {
  var ar = readline().split(' ').map((v)=>{return parseInt(v);});

   var l1 = ar[0], r1 = ar[1], l2 = ar[2], r2 = ar[3];

   var a = ar.filter(x=>(l1<= x && x<=r1)).sort((z,h)=>(h-z))
   var b = ar.filter(x=>(l2<= x && x<=r2)).sort()
   var pass = true
   for (var i=0;i<a.length && pass;i++) {

     for (var j=0;j<b.length && pass; j++) {
       if(a[i] != b[j]) {
          print(a[i], b[j]);
          pass = false;
       }
     }
   }
 //  var a=Math.max(ar[0], ar[2]), b=Math.min(ar[1], ar[3]);

//   print(a.join(','), b.join(','))

}

//var input = readline()
var t = parseInt(readline())

for(var i=0; i<t; i++) {
    var ar = readline().split(' ')
    var arr = ar.map(Number)
    var a = arr[0]
    var b = arr[1]
    var c = arr[2]
    var d = arr[3]

    var n = a+1
    var m = c+1

    if(n == m) print(a +' '+ m)
    else print(n +' '+ m)

}

//

 var q=parseInt(readline());
for(var i=0;i<q;i++){
	var s=readline().split(" ");
	var l1=parseInt(s[0]),r1=parseInt(s[1]),l2=parseInt(s[2]),r2=parseInt(s[3]);
	if(l1===r2){r2--;print(l1+" "+r2);}
	else{print(l1+" "+r2);}
}

var t = readline();
for(var i=0;i<t;i++){
var s = readline().split(" ");var r = [... new Set(s)];

var x1 = s[0].slice(0,s[0].length) + s[1].slice(0,s[1].length);// 1 2
var x2 = s[2].slice(0,s[2].length) + s[3].slice(0,s[3].length);// 2 1

if(r.length == 3){
if(x1.indexOf(r[0])){print(r[0] + " " + r[r.length-1])}else{print(r[1] + " " + r[r.length-1])}

}else{print(r[0] + " " + r[r.length-1]);}
}

var t = +readline();

for (var i=0; i<t; i++){
	var tn = readline().split(' ').map(Number);
	if (tn[0] != tn[3])	print (tn[0], tn[3]);
	else print (tn[0], tn[2]);
}

var t = parseInt(readline())

while(t--) {
    var arr = readline().split(' ')
    var a = parseInt(arr[0])
    var b = parseInt(arr[1])
    var c = parseInt(arr[2])
    var d = parseInt(arr[3])

    var n = a+1
    var m = c+1
    //print('n = '+ n +' '+'m = '+ m)
    if(n == m) print(a +' '+ m)
    else print(n +' '+ m)

}

function query(l1, r1, l2, r2) {
    if (l1 === l2) {
        return { a: l1, b: r2 };
    } else {
        return { a: l1, b: l2 };
    }
}

function getAnswer() {
    const fs = require('fs');
    const stdinBuffer = fs.readFileSync(0).toString('utf8');
    let [n, ...lines] = stdinBuffer.split('\n');

    lines = lines.slice(0, n);

    const queries = lines.map((line) => {
        const tokens = line.split(' ').map((num) => parseInt(num));

        return {
            l1: tokens[0],
            r1: tokens[1],
            l2: tokens[2],
            r2: tokens[3],
        }
    });

    return queries.map((q) => query(q.l1, q.r1, q.l2, q.r2));
}

const ans = getAnswer();

console.log(ans.map((x) => x.a + ' ' + x.b).join('\n'));

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  console.error('\n');
  var start = Date.now();
  Main();
  var end = Date.now() - start;
  myerr('time : ' + end + 'ms');
  myerr('memory : ' + Math.round(process.memoryUsage().heapTotal / 1024) + 'KB');
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  return {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
  };
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
  var t = nextInt();
  var output = new Array(t);
  for(var i = 0; i < t; i++){
    var tmp = nextIntArray();
    var L1 = tmp[0];
    var R1 = tmp[1];
    var L2 = tmp[2];
    var R2 = tmp[3];
    var a = L1;
    var b = L2;
    if(a == b){
      b++;
    }
    output[i] = a + " " + b;
  }
  myout(myconv(output, 9));
}

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const [l1, r1, l2, r2] = d.split(' ').map(Number);

  if (l1 === r2) {
    console.log(l1, l2);
  }
  else {
    console.log(l1, r2);
  }
  c++;
});

var cnt = 0;
var ms = [];
var readliner = require('readline');
var rl = readliner.createInterface(process.stdin, process.stdout, null);
rl.on('line', function (cmd) {
                  //console.log('You just typed: '+cmd);
                  cnt++;
                  if (cnt > 1) ms.push(cmd);
              }).on('close', main);

function main() {
	for (var i=0; i<ms.length; i++) {
		var numbers = ms[i].split(' ');
		var l1 = parseInt(numbers[0]);
		var r1 = parseInt(numbers[1]);
		var l2 = parseInt(numbers[2]);
		var r2 = parseInt(numbers[3]);
		if (l1 !== l2)
			console.log(l1 + " " + l2);
		else if (r1 !== r2)
			console.log(r1 + " " + r2);
		else
			console.log(l1 + " " + r2);
	}
    //console.log(ans);
    //console.log(Q);

    //var x = 1.2345678;
    //console.log('x = ',Number(x).toFixed(5));

    process.exit(0);
};

//var lines = data.split(/\r?\n/);

function ansa(n,arr){

    for(var i = 0 ; i < n ; i++){

      var flag = 0;
      var as = [];
      var bs = [];
      for(var j = arr[i][0] ; j <= arr[i][1] ; j++ ){
        for(var k = arr[i][2] ; k <= arr[i][3] ; k++ ){
          if(j !== k){
            print(j + ' ' + k);
            flag = 1;
            break;
        }
        }
        if(flag === 1)break;
      }

    }
}

var n = parseInt(readline())
var arr = []
for(var i = 0 ; i < n ; i++){
  var a = readline().split(" ");
  for(var j = 0 ; j < a.length ; j++) a[j] = parseInt(a[j]);
  arr[i] = a;
}

ansa(n,arr);
//ansa(5,[[1,2,1,2],[2,6,3,4],[2,4,1,3],[1,2,1,3],[1,4,5,8]])

function query(l1, r1, l2, r2) {
    if (l1 === l2) {
        return { a: l1, b: r2 };
    } else {
        return { a: l1, b: l2 };
    }
}

function getAnswer() {
    const fs = require('fs');
    const stdinBuffer = fs.readFileSync(0).toString('utf8');
    const [__, ...lines] = stdinBuffer.split('\n');
    const queries = lines.map((line) => {
        const tokens = line.split(' ').map((num) => parseInt(num));

        return {
            l1: tokens[0],
            r1: tokens[1],
            l2: tokens[2],
            r2: tokens[3],
        }
    });

    return queries.map((q) => query(q.l1, q.r1, q.l2, q.r2));
}

const ans = getAnswer();

console.log(ans.map((x) => x.a + ' ' + x.b).join('\n'));

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  console.error('\n');
  var start = Date.now();
  Main();
  var end = Date.now() - start;
  myerr('time : ' + end + 'ms');
  myerr('memory : ' + Math.round(process.memoryUsage().heapTotal / 1024) + 'KB');
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  return {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
  };
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
  var t = nextInt();
  var output = new Array(t);
  for(var i = 0; i < t; i++){
    var tmp = nextIntArray();
    var L1 = tmp[0];
    var R1 = tmp[1];
    var L2 = tmp[2];
    var R2 = tmp[3];
    var a = Math.max(L1, L2);
    var b = Math.min(R1, R2);
    output[i] = a + " " + b;
  }
  myout(myconv(output, 9));
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
  input: process.stdin, output: process.stdout
});
var obj; var inLine = [];
read.on('line', function(input){inLine.push(input);});
read.on('close', function(){
  obj = init(inLine);
  console.error('\n');
  var start = Date.now();
  Main();
  var end = Date.now() - start;
  myerr('time : ' + end + 'ms');
  myerr('memory : ' + Math.round(process.memoryUsage().heapTotal / 1024) + 'KB');
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextInt(){return myconv(next(),1);} function nextStrArray(){return myconv(next(),2);}
function nextIntArray(){return myconv(next(),4);} function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();}
function init(input){
  return {
    list : input, index : 0, max : input.length,
    hasNext : function(){return (this.index < this.max);},
    next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}}
  };
}
function myout(s){console.log(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
  var t = nextInt();
  var output = new Array(t);
  for(var i = 0; i < t; i++){
    var tmp = nextIntArray();
    var L1 = tmp[0];
    var R1 = tmp[1];
    var L2 = tmp[2];
    var R2 = tmp[3];
    var a = Math.min(L1, L2);
    var b = Math.max(R1, R2);
    output[i] = a + " " + b;
  }
  myout(myconv(output, 9));
}

const readline = require('readline');
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false
});
let c = 0;

rl.on('line', (d) => {
  if (c === 0) {
    c++;
    return;
  }

  const [l1, r1, l2, r2] = d.split(' ').map(Number);

  console.log(l1, r2);
  c++;
});

var a = +readline();

for(var i = 0; i < a; i++) {
    var l = readline().split(' ');
    print(l[0] + ' ' + l[3]);
}

function ansa(n,arr){

    for(var i = 0 ; i < n ; i++){

      var q1 = Math.max(arr[i][0],arr[i][2]);
      var q2 = Math.min(arr[i][1],arr[i][3]);
      print(q1,q2)
    }
}

var n = parseInt(readline())
var arr = []
for(var i = 0 ; i < n ; i++){
  var a = readline().split(" ");
  for(var j = 0 ; j < a.length ; j++) a[j] = parseInt(a[j]);
  arr[i] = a;
}
ansa(n,arr);

// var b = readline().split(' ');
// for(var i = 0 ; i < b.length ; i++) b[i] = parseInt(b[i]);

function ansa(n,arr){

    for(var i = 0 ; i < n ; i++){

      var q1 = Math.min(arr[i][0],arr[i][2]);
      var q2 = Math.max(arr[i][1],arr[i][3]);
      print(q1,q2)
    }
}

var n = parseInt(readline())
var arr = []
for(var i = 0 ; i < n ; i++){
  var a = readline().split(" ");
  for(var j = 0 ; j < a.length ; j++) a[j] = parseInt(a[j]);
  arr[i] = a;
}
ansa(n,arr);

// var b = readline().split(' ');
// for(var i = 0 ; i < b.length ; i++) b[i] = parseInt(b[i]);

function ansa(n,arr){

    for(var i = 0 ; i < n ; i++){

      var q1 = Math.max(arr[i][0],arr[i][2]);
      var q2 = Math.min(arr[i][1],arr[i][3]);
      print(q2,q1)
    }
}

var n = parseInt(readline())
var arr = []
for(var i = 0 ; i < n ; i++){
  var a = readline().split(" ");
  for(var j = 0 ; j < a.length ; j++) a[j] = parseInt(a[j]);
  arr[i] = a;
}
ansa(n,arr);

var n = parseInt(readline());

for (var i =0;i<n;i++) {
  var ar = readline().split(' ').map((v)=>{return parseInt(v);});

   var l1 = ar[0], r1 = ar[1], l2 = ar[2], r2 = ar[3];

   var a=Math.max(ar[0], ar[2]), b=Math.min(ar[1], ar[3]);
  if (  l1 > a || a > r1) {
   a = ar[1]-1;
  }

  if (  l2 > b || b > r2) {
   b = ar[3]-1;
  }
   print(a, b)

}

//var input = readline()
var n = parseInt(readline())

for(var i=0; i<n; i++) {
    var arr = readline().split(' ')
    var a = parseInt(arr[0])
    var b = parseInt(arr[1])
    var c = parseInt(arr[2])
    var d = parseInt(arr[3])

    var n = a+1
    var m = c+1

    if(n == m) print(a +' '+ n)
    else print(n +' '+ m)
}

//var input = readline()
var t = parseInt(readline())

for(var i=0; i<t; i++) {
    var arr = readline().split(' ')
    var a = arr[0]
    var b = arr[1]
    var c = arr[2]
    var d = arr[3]

    var n = a+1
    var m = c+1

    if(n == m) print(a +' '+ m)
    else print(n +' '+ m)

}

var n = parseInt(readline())

for(var i=0; i<n; i++) {
    var arr = readline().split(' ')
    var a = parseInt(arr[0])
    var b = parseInt(arr[1])
    var c = parseInt(arr[2])
    var d = parseInt(arr[3])

    var n = a+1
    var m = c+1

    if(n == m) write(a +' '+ m)
    else write(n +' '+ m)
}

var r = readline();for(var p=0;p<r;p++){
var s = readline().split(" ");
var r = [... new Set(s[0].slice(0,s[0].length)+s[1].slice(0,s[1].length))];
var n = [... new Set(s[2].slice(0,s[2].length)+s[3].slice(0,s[3].length))];
print(r[r.length-1] + " " + n[n.length-1]);

		}


const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

let i = 0;
let t = NaN;
const N = 101;
const matr = new Array(N);
for (let i = 0; i < N; i++) {
    matr[i] = new Array(N).fill(0);
}

rl.question('', v => {
    t = +v;
    input();
});

function input() {
    rl.question('', v => {
        const n = +v;

        rl.question('', v => {
            const arr = v.trim().split(' ').map(el => +el);
            main(n, arr);

            i++;
            if (i === t) {
                rl.close();
                return;
            }
            input();
        });
    });
}

function createTree(l, r, arr, darr, d = 0) {
    if (r < l) {
        return;
    }

    if (l === r) {
        darr[l] = d;
        return;
    }

    let m = l;
    for (let i = l + 1; i <= r; i++) {
        if (arr[i] > arr[m]) {
            m = i;
        }
    }

    darr[m] = d;
    createTree(l, m - 1, arr, darr, d + 1);
    createTree(m + 1, r, arr, darr, d + 1);
}

function main(n, arr) {
    const darr = new Array(n);
    createTree(0, n - 1, arr, darr);
    console.log(darr.join(' '));
}

"use strict";
const fs = require("fs");
//let filename = "0.txt";
let filename = 0;
const input = fs.readFileSync(filename, "utf8").trim().split("\n");
let line = 0;
function readline() {
  return input[line++];
}
function iInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => parseInt(cur));
}
function BInpArr() {
  return readline()
    .trim()
    .split(" ")
    .map((cur) => BigInt(cur));
}
function string() {
  return readline().trim();
}
function stringArr() {
  return readline().trim().split("");
}
function getValue() {
  return parseInt(readline());
}
let mod = BigInt(1e9 + 7);
class node {
  constructor(val, ind) {
    this.ind = ind;
    this.val = val;
    this.left = null;
    this.right = null;
  }
}
class Tree {
  constructor() {
    this.root = null;
  }
  push(val, ind) {
    const helper = (head) => {
      if (head === null) return new node(val, ind);
      else if (ind < head.ind) head.left = helper(head.left);
      else head.right = helper(head.right);
      return head;
    };
    this.root = helper(this.root);
  }
  find(ind) {
    let d = 0;
    const helper = (head, d) => {
      if (head.ind === ind) return d;
      else if (head.ind > ind) return helper(head.left, d + 1);
      else return helper(head.right, d + 1);
    };
    return helper(this.root, d);
  }
}
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    for (let i = 0; i < n; i++) arr[i] = { val: arr[i], ind: i + 1 };
    arr.sort((a, b) => b.val - a.val);
    let tree = new Tree();
    for (let i = 0; i < n; i++) {
      tree.push(arr[i].val, arr[i].ind);
    }
    let res = "";
    for (let i = 1; i <= n; i++) {
      res += tree.find(i) + " ";
    }
    console.log(res);
  }
};
solve();

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));

const INF = Number.POSITIVE_INFINITY;

const top = s => s[s.length - 1];

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

		a[-1] = a[n] = INF;
		let lmx = [], rmx = [];

		let st = [-1];
		for (let i = 0; i < n; i++) {
			while (a[top(st)] < a[i]) st.pop();
			lmx[i] = a[top(st)];
			st.push(i);
		}

		st = [n];
		for (let i = n - 1; i >= 0; i--) {
			while (a[top(st)] < a[i]) st.pop();
			rmx[i] = a[top(st)];
			st.push(i);
		}

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

		const dp = [];
		dp[n] = 0;
		for (let i = n - 1; i > 0; i--) {
			const prnt = Math.min(lmx[inx[i]], rmx[inx[i]]);
			dp[i] = dp[prnt] + 1;
		}

		let ans = [];
		for (i in dp)
			ans[inx[i]] = dp[i];

		console.log(...ans);
	}
}

'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++];
}

// Actual code...

function depth(target, arr, left, right) {
    let maximum = 0;
    let indexOfMax = 0;
    for (let i = left; i < right; i++) {
        if (arr[i] > maximum) {
            maximum = arr[i];
            indexOfMax = i;
        }
    }
    const indexOfTarget = arr.indexOf(target);
    if (target === maximum) {
        return 0;
    } else if (indexOfTarget < indexOfMax) {
        return 1 + depth(target, arr, left, indexOfMax);
    } else {
        return 1 + depth(target, arr, indexOfMax + 1, right);
    }
}

function main() {
    const numCases = parseInt(readline());
    for (let i = 0; i < numCases; i++) {
        const numTerms = parseInt(readline());
        const arr = readline().split(" ").map(str => parseInt(str));
        let result = "";
        for (let v = 0; v < numTerms; v++) {
            const d = depth(arr[v], arr, 0, arr.length);
            if (v != 0) result += " ";
            result += d;
        }
        console.log(result);
    }
}

let lines = require('fs').readFileSync(global.FILE_DESCRIPTOR || 0).toString().split('\n');
let index = 0;
const readline = () => lines[index++] || '';

let n = +readline();
for (let i = 0; i < n; i++) {
  {
    readline();
    let arr = readline().split(' ').map(i => +i);
    let res = {};
    function check(arr, depth = 0) {
      if (arr.length === 0) return;
      let max = Math.max(...arr);
      let index = arr.indexOf(max);
      res[max] = depth;
      check(arr.slice(0, index), depth+1)
      check(arr.slice(index + 1), depth+1)
    }
    check(arr)
    console.log(arr.map(num => res[num]).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();

var height = []

function main() {
    var xx = Number(readline())
    Array(Number(xx)).fill(1).map((t, i) => {

        var x = parseInt(readline())
        height = new Array(x).fill(0)
        var a = readline().split(' ').map((x, i) => {
            return parseInt(x)
        })

        dfs(a, 0, a.length, 0)
        console.log(height.join(' '))
    })

}

// [l,r)
function dfs(array, l, r, currentHeight) {
    var res = []
    var index = max(array, l, r)
    height[index] = currentHeight

    if (l < index) {
        dfs(array, l, index, currentHeight + 1)

    }
    if (r > index + 1) dfs(array, index + 1, r, currentHeight + 1)
}

function max(array, l, r) {
    var m = array[l]
    var mI = l
    for (var i = l; i < r; i++) {
        if (array[i] > m) {
            m = array[i]
            mI = i
        }
    }
    return mI
}

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	for(var i = 0; i < t; i++){
		var N = nextInt();
		var list = nextIntArray(N);
		var output = new Array(N).fill(-1);
		var stack = [{list : list, count : 0}];
		while(stack.length > 0){
			var now = stack.pop();
			var max = -1;
			var lastIndex = -1;
			for(var j = 0; j < now.list.length; j++){
				if(max < now.list[j]){
					max = now.list[j];
					lastIndex = j;
				}
			}
			var maxIndex = list.indexOf(max);
			output[maxIndex] = now.count;
			if(now.list.length == 1){
				continue;
			}
			var left = {list : [], count : now.count + 1};
			for(var j = 0; j < lastIndex; j++){
				left.list.push(now.list[j]);
			}
			if(left.list.length > 0){
				stack.push(left);
			}
			var right = {list : [], count : now.count + 1};
			for(var j = lastIndex + 1; j < now.list.length; j++){
				right.list.push(now.list[j]);
			}
			if(right.list.length > 0){
				stack.push(right);
			}
		}
		myout(myconv(output, 8));
	}
}

var row = readline().split(" ").map(function(x) { return parseInt(x); });
while(true) {
    var read = readline();
    if(read == undefined) break;
    var n = read.split(" ").map(function(x) { return parseInt(x); })[0];
    var arr = readline().split(" ").map(function(x) { return parseInt(x); });

    var result = [];
    result = createTree(arr, 0, n - 1, 0, result);

    var resultArr = [];
    for(var i = 0; i < n; i++) {
        resultArr.push(result[arr[i]]);
    }

    write(resultArr.join(" ") + "\n");
}

function createTree(arr, arrStart, arrEnd, level, result) {
    var max = 0;
    var index = 0;
    for(var i = arrStart; i <= arrEnd; i++) {
        index = arr[i] > max ? i : index;
        max = arr[i] > max ? arr[i] : max;
    }
    result[max] = level;
    level++;
    if(arrStart != arrEnd) {
        if(index - 1 >= arrStart)
            result = createTree(arr, arrStart, index - 1, level, result);
        if(index + 1 <= arrEnd)
            result = createTree(arr, index + 1, arrEnd, level, result);
    }

    return result;
}


const n = Number(readline());
const s = Array.from(readline().substring(0, n), c => Number(c));

function stats(digits) {
  var sum = 0;
  var erased = 0;
  for (var digit of digits)
    if (Number.isNaN(digit)) erased++;
    else sum += digit;
  return [sum, erased];
}

const half = n * 0.5;
const l = stats(s.slice(0, half));
const r = stats(s.slice(half));
const diff = l[0] - r[0];
const le = l[1];
const re = r[1];
print((() => {
  var pe = (le + re) * 0.5;
  var cl = Math.min(le, pe);
  var cr = Math.min(re, pe);
  var nd = diff + cl * 9;
  if (nd < -(le - cl) * 9 || nd > cr * 9) return true;
  nd = diff - cr * 9;
  if (nd > (re - cr) * 9 || nd < -cl * 9) return true;
  return false;
})() ? "Monocarp" : "Bicarp");


print(function(n, q) {
	var a = readline().split(' ').map(Number);
	var b = [];
	for (var i = 0; i <= n; i++) {
		b[i] = 0;
	}

	for (var i = 0; i < q; i++) {
		readline().split(' ').reduce(function(l, r) {
			b[l - 1]++;

			b[r]--;
		});
	}

	var x = 0;
	for (var i = 0; i < n; i++) {
		b[i] = (x += b[i]);
	}
	b.pop();
	b.sort(function(a, b) {
		return a - b;
	});
	a.sort(function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 0; i < n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = readline().split(' ').map(Number);
	var b = [];
	for (var i = 0; i < n; i++) {
		b[i] = 0;
	}

	for (var i = 0; i < q; i++) {
		readline().split(' ').reduce(function(l, r) {
			b[l - 1]++;
			if (r < n)
				b[r]--;
		});
	}

	var x = 0;
	for (var i = 0; i < n; i++) {
		b[i] = (x += b[i]);
	}

	b.sort(function(a, b) {
		return a - b;
	});
	a.sort(function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 0; i < n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = ('0 ' + readline()).split(' ').map(Number).sort(function(a, b) {
		return a - b;
	});
	var b = new Int32Array(n + 1);

	for (var i = 0; i < q; i++) {
		var l = readline().split(' ');
		b[l[0]]++;
		b[+l[1] + 1]--;
	}

	var x = 0;
	for (var i = 1; i <= n; i++) {
		b[i] = (x += b[i]);
	}

	Array.prototype.sort.call(b, function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 1; i <= n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

var tree = new Uint32Array(2e5 + 1);

function update(i, v, n) {
	while (i <= n) {
		tree[i] += v;
		i += (i & -i);
	}
}

function read(i) {
	var sum = 0;
	while (i > 0) {
		sum += tree[i];
		i -= (i & -i);
	}
	return sum;
}

print(function(n, q) {
	var a = readline().split(' ').map(Number).sort(function(a, b) {
		return a - b;
	});

	while (q--) {
		readline().split(' ').map(Number).reduce(function(x, y) {
			update(x, 1, n);
			update(y + 1, -1, n);
		});
	}
	var c = new Uint32Array(n);
	for (var i = 1; i <= n; i++) {
		c[i-1] = read(i);
	}
	Array.prototype.sort.call(c, function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 0; i < n; i++) {
		ans += a[i] * c[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = new Int32Array(n + 1);
	var b = new Int32Array(n + 1);

	var i = 1;
	readline().replace(/\d+/g, function(x) {
		a[i++] = +x;
	});

	for (var i = 0; i < q; i++) {
		var l = readline().split(' ');
		b[l[0]]++;
		b[+l[1] + 1]--;
	}

	var x = 0;
	for (var i = 1; i <= n; i++) {
		b[i] = (x += b[i]);
	}

	Array.prototype.sort.call(b, function(a, b) {
		return a - b;
	});
	Array.prototype.sort.call(a, function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 1; i <= n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = readline().split(' ').map(Number);
	var b = [];
	for (var i = 0; i <= n; i++) {
		b[i] = 0;
	}

	var c=[]
	for (var i = 0; i < q; i++) {
		c.push(readline());
	}
	c.forEach(function(v){
		v.split(' ').reduce(function(l, r) {
			b[l - 1]++;
			b[r]--;
		});
	});

	var x = 0;
	for (var i = 0; i < n; i++) {
		b[i] = (x += b[i]);
	}
	b.pop();
	b.sort(function(a, b) {
		return a - b;
	});
	a.sort(function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 0; i < n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a= readline().split(' ').map(Number);
	var b = new Int32Array(n);

	for (var i = 0; i < q; i++) {
		readline().split(' ').reduce(function(l, r){
			b[l-1]++;
			if( r<n )
				b[r]--;
		});
	}

	var x = 0;
	for (var i = 0; i < n; i++) {
		b[i] = (x += b[i]);
	}

	Array.prototype.sort.call(b, function(a, b) {
		return a - b;
	});
	Array.prototype.sort.call(a, function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 0; i < n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = new Int32Array(n + 1);
	var b = new Int32Array(n + 1);

	var i = 1;
	readline().split(' ').forEach(function(x) {
		a[i++] = +x;
	});

	for (var i = 0; i < q; i++) {
		var l = readline().split(' ');
		b[l[0]]++;
		b[+l[1] + 1]--;
	}

	var x = 0;
	for (var i = 1; i <= n; i++) {
		b[i] = (x += b[i]);
	}

	Array.prototype.sort.call(b, function(a, b) {
		return a - b;
	});
	Array.prototype.sort.call(a, function(a, b) {
		return a - b;
	});

	var ans = 0;
	for (var i = 1; i <= n; i++) {
		ans += a[i] * b[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = ('0 '+readline()).split(' ').map(Number).sort(function(a, b) {
		return a-b;
	});

	// n=n+1;

	var b = new Int32Array(n+1);
	for (var i = 0; i < q; i++) {
		readline().split(' ').map(Number).reduce(function(l, r) {
			b[l]++;
			b[r+1]--;
		});
	}

	var x = 0;
	var t = new Uint32Array(n+1);
	for (var i = 1; i <= n; i++) {
		x += b[i];
		t[i] = x;
	}

	Array.prototype.sort.call(t, function(a, b) {
		return a-b;
	});

	var ans = 0;
	for (var i = 1; i <= n; i++) {
		ans += a[i] * t[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

print(function(n, q) {
	var a = readline().split(' ').map(Number).sort(function(a, b) {
		return b - a;
	});
	var b = new Uint32Array(q);
	var c = new Uint32Array(q);

	for (var i = 0; i < q; i++) {
		readline().split(' ').map(Number).reduce(function(l, r) {
			b[i] = l;
			c[i] = r + 1;
		});
	}
	Array.prototype.sort.call(b, function(a, b) {
		return a - b;
	});
	Array.prototype.sort.call(c, function(a, b) {
		return a - b;
	});

	var x = 0;
	var j = 0;
	var k = 0;
	var t = new Uint32Array(n);
	for (var i = 1; i <= n; i++) {
		while (i == b[j]) x++, j++;
		while (i == c[k]) x--, k++;
		t[i-1]=x;
	}
	Array.prototype.sort.call(t, function(a, b) {
		return b-a;
	});

	var ans = 0;
	for (var i = 0; i < n; i++) {
		ans += a[i] * t[i];
	}
	return ans;
}.apply(0, readline().split(' ').map(Number)));

var first = readline().split(' ').map(f);
var n = first[0];
var q = first[1];

var a = readline().split(' ').map(f);
a.sort((x, y) => x - y);
a.reverse();
var pref = [];

var i, li, ri;

for(i = 0; i <= n + 1; i++){
    pref.push(0);
}

for(i = 0; i < q; i++){
    var query = readline().split(' ').map(f);
    li = query[0];
    ri = query[1];
    pref[li]++;
    pref[ri + 1]--;
}

for(i = 1; i <= n; i++){
    pref[i] = pref[i] + pref[i - 1];
}

pref.sort((x, y) => x - y);
pref.reverse();

var ans = 0;
for(i = 0; i < n; i++){
    ans = ans + pref[i] * a[i];
}

print(ans);

function f(x){
    return parseInt(x);
}

var first = readline().split(' ').map(f);
var n = first[0];
var q = first[1];

var a = readline().split(' ').map(f);
a.sort();
var pref = [];

var i, li, ri;

for(i = 0; i <= n; i++){
    pref.push(0);
}

for(i = 0; i < q; i++){
    var query = readline().split(' ').map(f);
    li = query[0];
    ri = query[1];
    pref[li]++;
    pref[ri + 1]--;
}

for(i = 1; i <= n; i++){
    pref[i] = pref[i] + pref[i - 1];
}

pref.sort();

var ans = 0;
for(i = 1; i <= n; i++){
    ans = ans + pref[i] * a[i - 1];
}

print(ans);

function f(x){
    return parseInt(x);
}


function getLine(){
    return readline().split(' ').map(function(x){
       return parseInt(x);
    });
}

var L = getLine();
var n = L[0];
var k = L[1];
var p = getLine();
var r = 0;
while(true){
    if(p.every(function(x){
        return x == k;
    })){
        break;
    }
    p = p.map(function(x, ind, self){
        if(ind === self.indexOf(x) && x < k){
            x++;
        }
        return x;
    });
    r++;
}
print(r);

'use strict'

const {EOL} = require('os')

let ipt = ''
process.stdin.on('data', s => ipt += s)
process.stdin.on('end', () => {
  let [nk, zs] = ipt.split(EOL)
  const [n, k] = nk.split(' ')
  zs = zs.split(' ').map(v => +v)
  let g = 0
  while (zs.some(z => z < k)) {
    g++
    let cz = 0
    for (let i = 0; i < zs.length; i++) {
      const z = zs[i]
      if (z == k) break
      if (cz < z) {
        zs[i]++
        cz = z
      }
    }
    zs = zs.sort((a, b) => a - b)
  }

  console.log(g)
})


'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.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));
  main();
});

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

function solve(n, a) {
  let last = a[0];
  let sum = last;
  for (let j = 1; j < n; j++) {
    if (last * a[j] > 0) {
      if (a[j] > last) {
        sum = sum - last + a[j];
        last = a[j];
      }
    } else {
      last = a[j];
      sum += a[j];
    }
  }
  return sum;
}

function main() {
  const t = parseInt(readLine());
  for (let i = 0; i < t; i++) {
    let n = parseInt(readLine());
    let a = readLine().split(' ').map((val) => parseInt(val));
    let res = solve(n, a);
    console.log(res);
  }
  //process.stdout.write("hello: ");
}

'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.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));
  main();
});

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

function solve(n, a) {
  let countN = 0;
  let firstN = -1;
  for (let i = 0; i < n; i++) {
    if (a[i] < 0) {
      firstN = i;
      countN++;
      break;
    }
  }
  let sumN = Number.MIN_SAFE_INTEGER;
  if (countN > 0) {
    let last = a[firstN];
    sumN = last;
    for (let j = firstN + 1; j < n; j++) {
      if (last * a[j] > 0) {
        if (a[j] > last) {
          sumN = sumN - last + a[j];
          last = a[j];
        }
      } else {
        last = a[j];
        sumN += a[j];
        countN++;
      }
    }
  }
  let countP = 0;
  let firstP = -1;
  for (let i = 0; i < n; i++) {
    if (a[i] > 0) {
      firstP = i;
      countP++;
      break;
    }
  }
  let sumP = Number.MIN_SAFE_INTEGER;
  if (countP > 0) {
    let last = a[firstP];
    sumP = last;
    for (let j = firstP + 1; j < n; j++) {
      if (last * a[j] > 0) {
        if (a[j] > last) {
          sumP = sumP - last + a[j];
          last = a[j];
        }
      } else {
        last = a[j];
        sumP += a[j];
        countP++;
      }
    }
  }
  let sum = countP > countN ? sumP : countN > countP ? sumN : Math.max(sumN, sumP);
  return sum;
}

function main() {
  const t = parseInt(readLine());
  for (let i = 0; i < t; i++) {
    let n = parseInt(readLine());
    let a = readLine().split(' ').map((val) => parseInt(val));
    let res = solve(n, a);
    console.log(res);
  }
  //process.stdout.write("hello: ");
}

'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, industry] = readline().split(" ").map(e => parseInt(e));

    let cases = parseInt(readline(), 10);
    for (var i = 0; i < cases; i++) {
      readline();
      const numbers = readline().split(" ").map(e => parseInt(e));

      let sum = 0;
      while (numbers.length > 0) {
        let sub = [numbers.pop()];
        let sign = Math.sign(sub[0]);

        while (numbers.length > 0 && Math.sign(numbers[numbers.length-1]) === sign) {
          sub.push(numbers.pop());
        }

        sum += Math.max.apply(null, sub);
      }

      console.log(sum);

    }

}

Main();
function myout(t){print(t);}//standard output
function next(){return readline();}
function nextInt(){return myconv(next(),1);}
function nextStrArray(){return myconv(next(),2);}//半角スペース分割
function nextIntArray(){return myconv(next(),4);}//半角スペース + 数値化
function nextCharArray(){return myconv(next(),6);}//1文字分割
//[no]要素の扱い。数値型
//不明値、異常時:引数そのまま返す
//6:1文字に分割  7:1文字に分割し、数値配列へ
//8:半角SPで結合　9:改行で結合　0:文字なしで結合
//atcoderのサンプルは最後に改行入れること
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(" ");case 4:return i.split(" ").map((a)=>Number(a));case 6:return i.split("");case 7:return i.split("").map((a)=>Number(a));case 8:return i.join(" ");case 9:return i.join("\n");case 0:return i.join("");default:return i;}}catch(e){return i;}}
function Main() {
    var t = nextInt();
	var output = [];
	for(var i = 0; i < t; i++){
		var n = nextInt();
		var list = nextIntArray();
		var isPlus = true;
		var sum = 0;
		if(list[0] < 0){
			isPlus = false;
		}
		var plusList = [];
		var minusList = [];
		var tmpPlusList = [];
		var tmpMinusList = [];
		for(var j = 0; j < n; j++){
			if(isPlus){
				if(list[j] > 0){
					tmpPlusList.push(list[j]);
				}else{
					plusList.push(tmpPlusList);
					tmpPlusList = [];
					tmpMinusList.push(list[j]);
					isPlus = false;
				}
			}else{
				if(list[j] < 0){
					tmpMinusList.push(list[j]);
				}else{
					minusList.push(tmpMinusList);
					tmpMinusList = [];
					tmpPlusList.push(list[j]);
					isPlus = true;
				}
			}
			if(j == n - 1){
				if(tmpMinusList.length > 0){
					minusList.push(tmpMinusList);
				}
				if(tmpPlusList.length > 0){
					plusList.push(tmpPlusList);
				}
			}
		}
		//myerr("p:" + plusList);
		//myerr("m:" + minusList);
		for(var j = 0; j < plusList.length; j++){
			//myerr(plusList[j]);
			sum += Math.max.apply(null,plusList[j]);
		}
		for(var j = 0; j < minusList.length; j++){
			//myerr(minusList[j]);
			sum += Math.max.apply(null,minusList[j]);
		}
		output[i] = sum;
	}
	myout(myconv(output,9));
}

var t = parseInt(readline());

function mapper(s) { return +s; }

for(var qq = 0; qq < t; qq++) {
    var n = parseInt(readline());
    var a = readline().split(' ').map(mapper);
    var al = a.length;

    var sum = 0;

    var curMax = a[0];
    var onPos = a[0] > 0;

    for(var i = 1; i < al; i++) {
        if (onPos) {
            if (a[i] > 0) {
                if (a[i] > curMax) {
                    curMax = a[i];
                }
            } else {
                sum += curMax;
                onPos = false;
                curMax = a[i];
            }
        } else {
            if (a[i] < 0) {
                if (a[i] > curMax) {
                    curMax = a[i];
                }
            } else {
                sum += curMax;
                onPos = true;
                curMax = a[i];
            }
        }
    }
    sum += curMax;

    print(sum);
}

var range = readline();

for(var i=0;i<range;i++) {
    var input = parseInt(readline());
    var numbers = readline().split(" ").map((x) => parseInt(x));
    var sums = [];
    var max = [];
    var min = [];

    for(var j=0;j<numbers.length;j++) {
        if(numbers[j] < 0) {
            if(max.length) {
                var value = Math.max.apply(null, max);
                sums.push(value);

                max.length = 0;
            }
            min.push(numbers[j]);
        } else if(numbers[j] > 0) {
            if(min.length) {
                var value = Math.max.apply(null, min);
                sums.push(value);
                min.length = 0;
            }
            max.push(numbers[j]);
        }
    }

    if(max.length) {
        sums.push(Math.max.apply(null, max));
    }

    if(min.length) {
        sums.push(Math.max.apply(null, min));
    }

    if(sums.length === 0) {
        print(Math.max.apply(null, numbers));
    } else {
        var sum = sums.reduce((a, b) => a + b, 0);
        print(sum);
    }

}

//Alternating Subsequence

const {EOL} = require('os');

let inp = '';

process.stdin.on('data', c => inp += c);

process.stdin.on('end', () => {
    let lines = inp.split(EOL);

    let t = +lines[0];

    let sgn = (x) => (x > 0) ? 1 : -1;

    let f = (arr) => {
    	let sign = sgn(arr[0]);
    	let minmax = arr[0];
    	let sum = 0;
    	for(let i = 0; i < arr.length; i++)  {
    		if(sgn(arr[i]) == sign)  {
    			if(arr[i] > minmax)  {
    				minmax = arr[i];
    			}
    		}  else  {
    			sum += minmax;
    			sign = -sign;
    			minmax = arr[i];
    		}
    	}
		sum += minmax;
		return sum;
    }

    for(let i = 0; i < t; i++)  {
    	// let n = +lines[2 * i + 1];
    	let arr = lines[2 * i + 2].split(' ').map(c => +c);
    	process.stdout.write(f(arr) + EOL);
    }

    return;
});

'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.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));
  main();
});

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

function solve(n, a) {
  let sum = Number.MIN_SAFE_INTEGER;
  let countN = 0;
  let countP = 0;
  let firstN = -1;
  let firstP = -1;
  for (let i = 0; i < n; i++) {
    if (firstN !== -1 && firstP !== -1) break;
    if (firstN === -1 && a[i] < 0) {
      firstN = i;
      countN++;
    }
    if (firstP === -1 && a[i] > 0) {
      firstP = i;
      countP++;
    }
  }
  if (countN > 0) {
    for (let i = firstN; i < n; i++) {
      if (a[i] > 0) break;
      let last = a[i];
      let curSum = a[i];
      countN = 1;
      for (let j = i + 1; j < n; j++) {
        if (last * a[j] > 0) {
          if (curSum - last + a[j] > curSum) {
            last = a[j];
            curSum = curSum - last + a[j];
          }
        } else {
          last = a[j];
          curSum += a[j];
          countN++;
        }
      }
      sum = Math.max(sum, curSum);
    }
  }
  if (countP > 0) {
    for (let i = firstP; i < n; i++) {
      if (a[i] < 0) break;
      let last = a[i];
      let curSum = a[i];
      countP = 1;
      for (let j = i + 1; j < n; j++) {
        if (last * a[j] > 0) {
          if (curSum - last + a[j] > curSum) {
            last = a[j];
            curSum = curSum - last + a[j];
          }
        } else {
          last = a[j];
          curSum += a[j];
          countP++;
        }
      }
      if (countP >= countN) sum = Math.max(sum, curSum);
    }
  }
  return sum;
}

function main() {
  const t = parseInt(readLine());
  for (let i = 0; i < t; i++) {
    let n = parseInt(readLine());
    let a = readLine().split(' ').map((val) => parseInt(val));
    let res = solve(n, a);
    console.log(res);
  }
  //process.stdout.write("hello: ");
}

var range = readline();

for(var i=0;i<range;i++) {
    var input = parseInt(readline());
    var numbers = readline().split(" ").map((x) => parseInt(x));
    var sums = [];

    for(var j=0;j<numbers.length;j++) {
        if(numbers[j] < 0 && numbers[j+1] > 0) {
            sums.push(numbers[j]);
            sums.push(numbers[j+1]);
            j++;
        } else if(numbers[j] > 0 && numbers[j+1] <0) {
            sums.push(numbers[j]);
            sums.push(numbers[j+1]);
            j++;
        }
    }

    if(sums.length === 0) {
        print(Math.max.apply(null, numbers));
    } else {
        var sum = sums.reduce((a, b) => a + b, 0);
        print(sum);
    }

}


var nm = readline().trim().split(' ').map((x)=>parseInt(x));
var n= nm[0];
var m = nm[1];
var b = []
for(var i=0;i<n;i++){
    b.push(readline().trim().split(' ').map((x)=>parseInt(x)));
}

var ans = [];
for (var i = 0; i < n; i++) {
		ans.push([]);
	for (var j = 0; j < m; j++) {
		ans[i].push(-1);
	}
}
var wrong = false;
for (var i = 0; i < n; i++) {
	for (var j = 0; j < m; j++) {
		if(b[i][j]==0){
			for (var k = 0; k < m; k++) {
				if(ans[i][k]==1){
					wrong = true;
					break
				}
				ans[i][k]=0;
			}
			for (var k = 0; k < n; k++) {
				if(ans[k][j]==1){
					wrong = true;
					break
				}
				ans[k][j]=0;
			}
		}
		if(wrong)
			break;
	}

	if(wrong)
		break;
}

if(wrong)
	print('NO');
else{
	for (var i = 0; i < n; i++) {
		for (var j = 0; j < m; j++) {
			if(b[i][j]==1){
				var isOne = false
				for (var k = 0; k < m; k++) {
					if(ans[i][k]!=0){
						isOne = true;
						break
					}
				}
				if(!isOne){
					for (var k = 0; k < n; k++) {
						if(ans[k][j]!=0){
							isOne = true;
							break
						}
					}
				}
				if(!isOne){
					wrong = true;
					break;
				}
			}

		}

		if(wrong)
			break;
	}

	if(wrong)
		print('NO');
	else{
		for (var i = 0; i < n; i++) {
			for (var j = 0; j < m; j++) {
				if(ans[i][j]==-1){
					ans[i][j]=1;
				}
			}
		}
		print('YES');
		for (var i = 0; i < n; i++) {
		    print(ans[i].join(' '));
		}
	}
}

var size = readline().trim().split(' ');
var m = size[0], n = size[1];
var MatrixA = [], MatrixB = [];

for (var i = 0; i < m; i++) {
  MatrixB.push([]);
  MatrixB[i] = readline().trim().split(' ');
  for (var j = 0; j < n; j++) {
    MatrixB[i][j] = parseInt(MatrixB[i][j]);
  }
}
for (var i = 0; i < m; i++) {
  MatrixA.push([]);
  for (var j = 0; j < n; j++) {
    MatrixA[i][j] = 1;
  }
}

function setRowZero(row) {
  for (var i = 0; i < n; i++) {
    MatrixA[row][i] = 0;
  }
}
function setColumnZero(col) {
  for (var i = 0; i < m; i++) {
    MatrixA[i][col] = 0;
  }
}

for (var i = 0; i < m; i++) {
  for (var j = 0; j < n; j++) {
    if (MatrixB[i][j] === 0) {
      setRowZero(i);
      setColumnZero(j);
    }
  }
}

function check() {
  var isOk = true;
  for (var i = 0; i < m; i++) {
    for (var j = 0; j < n; j++) {
      var target = MatrixB[i][j];
      var source = 0;
      for (var k = 0; k < m; k++) {
        if (MatrixA[k][j] === 1) {
          source = 1;
        }
      }
      for (var k = 0; k < n; k++) {
        if (MatrixA[i][k] === 1) {
          source = 1;
        }
      }
      if (target !== source) {
        isOk = false;
      }
    }
  }
  if (isOk) {
    print('YES');
    for (var i = 0; i < m; i++) {
      print(MatrixA[i].join(' '));
    }
  } else {
    print('NO');
  }

}

check();


const {stdin, exit} = require('process');
let inp = "";
stdin.on('data', chunk => inp += chunk).on('end', () => {
    let [a, b] = inp.split('\n').map(s => s.trim());
    if (a === b) console.log(-1);
    else console.log(Math.max(a.length, b.length));
})

'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++];
}

function readint() {
    let line = readline().split(' ').map(function(num) {
        return parseInt(num);
    });
    return line;
}

function readfloat() {
    let line = readline().split(' ').map(function(num) {
        return parseFloat(num);
    });
    return line;
}

function print(x) {
    console.log(x);
}

function write(x) {
    process.stdout.write(x);
}

function array2d(r,c) {
    let ans =[];
    for(let i = 0; i < r; i++) {
        let rowElements = [];
        for(let j = 0; j < c; j++) {
            rowElements.push(0);
        }
        ans.push(rowElements);
    }
    return ans;
}
function max(a,b){
    if(a > b) return a;
    else return b;
}
function main() {
    let m = readline();
    let e = readline();
    if(m.length < e.length) {
        [e, m] = [m, e];
    }
    if(m == e) print(-1);
    else print(m.length);
}

let data = '';
process.stdin.on('data', c => data += c);

function* nextLine() {
	let lines = data.trim().split(/\n/g);
	for(let l of lines){
		yield l.trim();
	}
}

let nl = nextLine();

nl.elem = function(funParse) {
		return funParse(this.next().value);
	};

nl.array = function(funParse) {
		return this.next().value.split(/\s/g).map(funParse||(x => x));
	};

nl.num = function() {
		return nl.elem(Number);
	};

nl.nums = function() {
		return nl.array(Number);
	};

nl.line = function() {
		return this.next().value;
	};

const sol = () => {
	let a = nl.line();
	let b = nl.line();
    console.log((a !== b)?Math.max(a.length, b.length):-1);

};

process.stdin.on('end', sol);

function out(a, b){return (a===b)?-1:(a.length > b.length)?a.length:b.length}
print(out(readline(),readline()))

var out = function(a, b){return (a===b)?-1:Math.max(a.length,b.length)}
print(out(readline(),readline()))

var a = readline();
var b = readline();
if(a==b)
    print(-1);
else
    print(Math.max(a.length,b.length));

var x = readline();
var y = readline();

if (x == y) {
  print(-1);
} else {
  print(parseInt(Math.max(x.length,y.length)));
}

s1 = readline();
s2 = readline();
ans = -1;
if(s1 !== s2){
    ans = Math.max(s1.length, s2.length);
}
print(ans);

/* global readline, print */
const a = readline()
const b = readline()

if (a === b) print(-1)
else print(Math.max(a.length, b.length))

var a = readline(),
    b = readline(),
    arr = [],
    res = 0;

if (a.length > b.length) res = a.length;
else if (b.length > a.length) res = b.length;
else if (a === b) res = -1;
else {
    res = a.length;
}

print(res);

var s1 =readline();
var s2 = readline();
var p =function () {if (s1!==s2) {return Math.max(s1.length,s2.length)} else return -1};
print (p());

var a = readline();
var b = readline();
if (a == b) {
	print (-1);
} else {
	if (a.length > b.length) {
		print (a.length);
	} else if (a.length < b.length) {
		print (b.length);
	} else if (a.length = b.length) {
		print (a.length);
	}
}

function main() {
    var line1 = readline();
    var line2 = readline();
    if(line1 == line2){
        return print(-1);
    } else {
        print(Math.max(line1.length, line2.length))
    }
}

main()

;(function () {
	var a = readline(),
		b = readline()
	if (a === b) print(-1)
	else print(Math.max(a.length, b.length))
})();

var a = readline();
var b = readline();

if (a === b) {
    print(-1);
} else {
    print(Math.max(a.length , b.length));
}

'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++];
}

function print(s) {
  process.stdout.write(`${s}`);
}
// Make a Snippet for the code above this and then write your logic in main();

function main() {
  // input
  const a = readline();
  const b = readline();
  let l;
  if (a.length !== b.length) {
    l = Math.max(a.length, b.length);
  } else {
    l = a === b ? -1 : a.length;
  }
  print(l);
}

'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++];
}

function readint() {
    let line = readline().split(' ').map(function(num) {
        return parseInt(num);
    });
    return line;
}

function readfloat() {
    let line = readline().split(' ').map(function(num) {
        return parseFloat(num);
    });
    return line;
}

function print(x) {
    console.log(x);
}

function write(x) {
    process.stdout.write(x);
}

function array2d(r,c) {
    let ans =[];
    for(let i = 0; i < r; i++) {
        let rowElements = [];
        for(let j = 0; j < c; j++) {
            rowElements.push(0);
        }
        ans.push(rowElements);
    }
    return ans;
}
function max(a,b){
    if(a > b) return a;
    else return b;
}
function main() {
    let m = readline();
    let e = readline();
    if(m.length < e.length) {
        [e, m] = [m, e];
    }
    let dp = array2d(m.length, e.length);
    let i = 0;
    for(const element of e) {
        if(m[0] != element)  dp[0][i] = 1;
        i++;
    }
    i = 0;
    for(const element of m) {
        if(e[0] != element) dp[i][0] = 1;
        i++;
    }
    for(let i = 1; i < m.length; i++) {
        for(let j = 1; j < e.length; j++) {
            if(m[i] != e[j]) {
                dp[i][j] = dp[i-1][j - 1] + 1;
            }
        }
    }
    let ans = dp[m.length - 1][e.length - 1];
    if(ans == e.length) print(m.length);
    else if(ans == 0) print(-1);
    else print(ans);
}

let data = '';
process.stdin.on('data', c => data += c);

function* nextLine() {
	let lines = data.trim().split(/\n/g);
	for(let l of lines){
		yield l.trim();
	}
}

let nl = nextLine();

nl.elem = function(funParse) {
		return funParse(this.next().value);
	};

nl.array = function(funParse) {
		return this.next().value.split(/\s/g).map(funParse||(x => x));
	};

nl.num = function() {
		return nl.elem(Number);
	};

nl.nums = function() {
		return nl.array(Number);
	};

nl.line = function() {
		return this.next().value;
	};

const sol = () => {
	let a = nl.line().split('');
	let b = nl.line().split('');
	let t = '';
	let aa = [];
	for(let c of a){
		if (t.length > 0) {
			if (c >= t.charAt(t.length-1)) {
				t += c;
			} else {
				aa.push(t);
				t = c;
			}

		} else {
			t += c;
		}
	}
	aa.push(t);
	t = '';
	let bb = [];
	for(let c of b){
		if (t.length > 0) {
			if (c >= t.charAt(t.length-1)) {
				t += c;
			} else {
				bb.push(t);
				t = c;
			}

		} else {
			t += c;
		}
	}
	bb.push(t);
	let cc = aa.filter(x => !bb.includes(x)).concat(bb.filter(x => !aa.includes(x)));
	if (cc.length === 0){
		console.log(-1);
	} else {
		console.log(Math.max(...(cc.map(x => x.length))));
	}

};

process.stdin.on('end', sol);

let data = '';
process.stdin.on('data', c => data += c);

function* nextLine() {
	let lines = data.trim().split(/\n/g);
	for(let l of lines){
		yield l.trim();
	}
}

let nl = nextLine();

nl.elem = function(funParse) {
		return funParse(this.next().value);
	};

nl.array = function(funParse) {
		return this.next().value.split(/\s/g).map(funParse||(x => x));
	};

nl.num = function() {
		return nl.elem(Number);
	};

nl.nums = function() {
		return nl.array(Number);
	};

nl.line = function() {
		return this.next().value;
	};

const sol = () => {
	let a = nl.line().split('');
	let b = nl.line().split('');
	let t = '';
	let aa = [];
	for(let c of a){
		if (t.length > 0) {
			if (c > t.charAt(t.length-1)) {
				t += c;
			} else {
				aa.push(t);
				t = c;
			}

		} else {
			t += c;
		}
	}
	aa.push(t);
	t = '';
	let bb = [];
	for(let c of b){
		if (t.length > 0) {
			if (c > t.charAt(t.length-1)) {
				t += c;
			} else {
				bb.push(t);
				t = c;
			}

		} else {
			t += c;
		}
	}
	bb.push(t);
	let cc = aa.filter(x => !bb.includes(x)).concat(bb.filter(x => !aa.includes(x)));
	if (cc.length === 0){
		console.log(-1);
	} else {
		console.log(Math.max(...(cc.map(x => x.length))));
	}

};

process.stdin.on('end', sol);

var a = readline();
var b = readline();
if (a == b) {
	print (-1);
} else {
	if (a.length > b.length) {
		print (a.length);
	} else if (a.length < b.length) {
		print (b.length);
	} else if (a.length = b.length) {
		print (-1);
	}
}

var a = readline();
var b = readline();
if (a == b) {
	print (-1);
} else {
	if (a.length > b.length) {
		print (a.length);
	} else if (a.length < b.length) {
		print (b.length);
	}
}


'use strict';

var readline = require('readline');

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

var readline__default = /*#__PURE__*/_interopDefaultLegacy(readline);

function solve([a, b, c, x, y]) {
  x -= a;
  y -= b;
  if (x <= 0 && y <= 0) {
    console.log('yes');
    return;
  }
  if (x > 0) {
    c -= x;
  }
  if (c < 0) {
    console.log('no');
    return;
  }
  if (y > 0) {
    c -= y;
  }
  if (c < 0) {
    console.log('no');
    return;
  }
  console.log('yes');
}

async function main(read) {
  let t = Number(await read());
  while (t--) {
    const a = (await read()).split(' ').map(Number);
    solve(a);
  }
}

const rl = readline__default["default"].createInterface({
  input: process.stdin,
  output: process.stdout
});
void async function () {
  const inputs = [];
  let i = 0;
  for await (const line of rl) {
    inputs.push(line.trim());
  }
  function read() {
    return inputs[i++];
  }
  await main(read);
}();

var t = +readline()

for(var i = 0; i< t; i ++) {
    var a = readline().split(' ').map((t) => +t)
    print(Task1(a[0],a[1],a[2],a[3],a[4]));
}

function Task1 (a,b,c,x,y){

  var test1 = a-x;
  var test2 = b-y;

  if (test1 < 0) {
    var just = test1;
    test1 += c;
    c += just;
  }
  if (test2 < 0){
    test2 += c;
  }

  var res = test1 >= 0 && test2 >= 0;
  return res ? 'YES': 'NO'

}

    var t = readline();
    for(var i = 0 ; i < t ; i++){
        var inputs = readline().split(' '),
            a = inputs[0], b = inputs[1], c = inputs[2];
            x = inputs[3], y = inputs[4] , counter = 0;
             if(a - x < 0){
                counter+= Math.abs(a - x);
            }
             if(b - y < 0){
                counter+= Math.abs(b - y);
            }
            if(counter === 0){
                print("YES");
            }
            else if(c - counter >= 0){
                print("YES");
            }
            else{
                print("NO");
            }
    }

//import { readline, print } from '@ip-algorithmics/codeforces-io';
var t = +readline();
for (var _i = 0, _a = Array.from(Array(t).keys()); _i < _a.length; _i++) {
    var i = _a[_i];
    var _b = readline().split(' ').map(function (v) { return +v; }), a = _b[0], b = _b[1], c = _b[2], x = _b[3], y = _b[4];
    print(Math.max(0, x - a) + Math.max(0, y - b) > c ? 'NO' : 'YES');
}

T = readline()
while(T--){
   x = readline().split(' ').map(x => +x)
   forDogs = x[0]
   forCats = x[1]
   forAll = x[2]
   dogs = x[3]
   cats = x[4]

   res = 'NO'

   if (forDogs >= dogs &&forCats  >= cats)
      res = 'YES'
   else if(forDogs < dogs && forCats  >= cats && forAll >= dogs - forDogs)
      res = 'YES'
   else if (forDogs >= dogs && forCats  < cats && forAll >= cats - forCats)
      res = 'YES'
   else if(forDogs < dogs && forCats  < cats && forAll >= (cats - forCats)+ (dogs - forDogs))
      res = 'YES'

   print(res)
}

const readline = require("readline");

var input = [];

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

rl.on("line", function (val) {
  input.push(val);
});

rl.on("close", function () {
  for (let i = 1; i <= input[0]; i++) {
    var entities = input[i].split(" ").map(x => parseInt(x))
    var dogFoodEater = (entities[3] - entities[0]) < 0 ? 0 : (entities[3] - entities[0]);
    var catFoodEater = (entities[4] - entities[1]) < 0 ? 0 : (entities[4] - entities[1]);
    if (dogFoodEater + catFoodEater > entities[2]) {
        console.log("NO");
    } else {
        console.log("YES");
    }
  }

  process.exit(0);
});

'use-strict'

process.stdin.resume();
process.stdin.setEncoding('utf8');

let data = '';
let currentLine = 0;

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

process.stdin.on('end', () =>{
    data = data.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));

    let testCases = parseInt(data.shift());

    while(testCases--) {

        const [a,b,c,x,y] = get_ints();

        const res = MY_FUNC(a,b,c,x,y);

        console.log(res)
    }
});

function get_ints() {
    return data.shift().split(' ').map(Number);
}

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

function MY_FUNC(a,b,c,x,y){
    let ans = 'NO';
    Math.max(0,x - a) + Math.max(0,(y - b)) <= c ? ans = 'YES': ans;
    return ans;
}

'use-strict'

process.stdin.resume();
process.stdin.setEncoding('utf8');

let data = '';
let currentLine = 0;

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

process.stdin.on('end', () =>{
    data = data.replace(/\s*$/, '')
    .split('\n')
    .map(str => str.replace(/\s*$/, ''));

    let testCases = parseInt(data.shift());

    while(testCases--) {

        const [a,b,c,x,y] = get_ints();

        const res = MY_FUNC(a,b,c,x,y);

        console.log(res)
    }
});

function get_ints() {
    return data.shift().split(' ').map(Number);
}

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

function MY_FUNC(a,b,c,x,y){
    if(x > a){
        if(a + c < x) return 'NO';
        c-= x - a;
    }
    if(y > b){
        if(b + c < y) return 'NO';
    }
    return 'YES';
}

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
function alpha() {
  var g = []
  var f = 'abcdefghijklmnopqrstuvwxyz';
  for(var x=0;x<f.length;x++) g[f[x]] = x+1;
  return g;
}
function Alpha() {
  var g = []
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  for(var x=0;x<f.length;x++) g[f[x]] = x+1;
  return g;
}
var T = readline();
for(var q=1;q<=T;q++) {
  var [a,b,c,x,y] = readline().split(' ').map((x)=> parseInt(x))
  a -= x;
  b -= y;
  a = Math.min(a, 0);
  b = Math.min(b, 0)
  var ans = 0;
  if(c+(a+b) >= 0) ans = 1;
  else ans = 0;
  console.log(ans? 'YES': 'NO');
  //fs.appendFileSync('output.txt', (ans?'YES':'NO')+'\r\n');
  //os.EOL
}
//fs.writeFileSync('output.txt', f.join(' '));

'use strict';

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

let inputString = '';
let currentLine = 0;

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

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

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

function output(x) {
    if (typeof x !== 'string') {
        x = x.toString();
    }
    //process.stdout.write(x);   // without auto '\n' (newline)
    console.log(x);  // with auto '\n' (newline)
}

function main() {
    let N = readline();

    for (let test = 0; test < N; test++) {
        let [a,b,c,x,y] = readline().split(' ').map(Number);

        x = Math.max(0, x - a);
        y = Math.max(0, y - b);

        output((x + y) <= c ? 'YES' : 'NO');
    }
}

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

let lines = [];
rl.on('line', (line) => {
    lines.push(line);
}).on('close', () => {
    // input in lines
    solve();
});

let line = 0;
const readline = () => lines[line++];

solve = () => {
    const t = parseInt(readline());
    for (let i = 0; i < t; i++) {
        var food = readline().split(" ").map(Number);
        var a = food[0];
        var b = food[1];
        var c = food[2];
        var x = food[3];
        var y = food[4];
        if (c >= Math.max(0, x - a) + Math.max(0, y - b)) {
            console.log("YES")
        } else {
            console.log("NO")
        }

    }
}

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

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {
    var t = parseInt(readline());

    for (let i = 0; i < t; i++) {
        var line = readline().split(' ');
        var a = parseInt(line[0]);
        var b = parseInt(line[1]);
        var c = parseInt(line[2]);
        var x = parseInt(line[3]);
        var y = parseInt(line[4]);

        a -= x;
        b -= y;
        if (Math.max(0,-a)+Math.max(0,-b) > c) {
            console.log('NO');
        } else {
            console.log('YES');
        }

    }
}

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    for(i = 1; i <= n; i++){
        var k = lines[i].split(' ').map(Number);
        var a = k[0];
        var b = k[1];
        var c = k[2];
        var x = k[3];
        var y = k[4];
        var s = 0
        x -= a
        y -= b
        if(x > 0){
            c -= x;
        }if(y > 0){
            c -= y;
        }
        if(c >= 0){
            console.log('YES');
        }else{
            console.log('NO');
        }
    }
});

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

function print(x) {
  console.log(x);
}
let inputString = "";
let currentLine = 0;

process.stdin.on("data", (inputStdin) => {
  inputString += inputStdin;
});
process.stdin.on("end", () => {
  inputString = inputString.split("\n");
  main();
});
function readline() {
  return inputString[currentLine++];
}

// ********** Code Start **********

function main() {

    let num = Number.parseInt(readline());

    while(num-- > 0){
        let input = readline();
        let inputArr = input.split(' ')
        let dogF = Number.parseInt(inputArr[0])
        let catF = Number.parseInt(inputArr[1])
        let UniversalF = Number.parseInt(inputArr[2])
        let dog = Number.parseInt(inputArr[3])
        let cat = Number.parseInt(inputArr[4])
        let res1 = (dog <= dogF) ? 0 : dog - dogF;
        let res2 = (cat <= catF) ? 0 : cat - catF;
        let res = ((res1 + res2) <= UniversalF) ? 'YES' : 'NO';
        console.log(res)
    }
}

let q = '';
process.stdin.on('data', c => q += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = q.split(EOL);
    var n = lines[0];
    for(i = 1; i <= n; i++){
        var k = lines[i].split(' ').map(Number);
        var a = k[0]; var b = k[1]; var c = k[2]; var x = k[3]; var y = k[4];
        var temp = 0;
        x -= a;
        y -= b;
        if(x > 0){
            c -= x;
        }if(y > 0){
            c -= y;
        }
        if(c >= 0){
            console.log('Yes');
        }else{
            console.log('No');
        }
    }
});

var readline = require("readline");
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
  terminal: false,
});

rl.on("line", function (line) {
  var input = line.split(" ");
  if (input.length === 5) {
    var [dogsFood, catsFood, univeralFood, xDogs, yCats] = input.map(Number);
    var hungryDogs = xDogs - dogsFood < 0 ? 0 : xDogs - dogsFood;
    var hungryCats = yCats - catsFood < 0 ? 0 : yCats - catsFood;
    if (hungryDogs + hungryCats <= univeralFood) {
      console.log("YES");
    } else {
      console.log("NO");
    }
  }
});

const lines = [];
const rl = require('readline').createInterface({ input: process.stdin, output: process.stdout });
rl.on('line', line => lines.push(line));
rl.on('close', main);
let rli = 0;
function readline() {
    return lines[rli++];
}

const ra = ()=>readline().split(' ').map(a=>+a);

function div(val, by){
    return (val - val % by) / by;
}

const cnk = (n, k) => {
    let res = 1;
    for (let i = 1; i <= k; i++)
        res = res * (n - k + i) / i;
    return Math.round(res + 0.01);
}

const sumab = (a, b) => {
    let x = (b - a + 1);
    let y = a + b;
    if (x % 2 === 0) {
        x = Math.round(x / 2);
    } else {
        y = Math.round(y / 2);
    }
    return BigInt(x) * BigInt(y);
}

const calc = (a, b, c, x, y)=>{
    let dog = Math.max(x - a, 0);
    let cat = Math.max(y - b, 0);
    let all = dog + cat - c;
    if (all > 0) return 'NO';
    return 'YES';
};

function main() {
    let T = +readline();
    for (let t = 1; t <= T; t++){
        // let n = +readline();
        let [a, b, c, x, y] = ra();
        console.log(`${calc(a, b, c, x, y)}`);
    }
}

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const arr = lines[l++].trim().split(' ').map(Number)
        output[i] = solve(arr) ? 'YES' : 'NO'
    }
    console.log(output.join('\n'))
// })()
})

function solve(arr) {
    const [a, b, c, d, e] = arr
    const ca = Math.max(0, d - a)
    const cb = Math.max(0, e - b)
    return ca + cb <= c
}

'use strict';
// HEADER:
const print = console.log;
let IS_MULTITEST, testN;
const pcalc = testN=>{
  let res = calc(testN);
  if (Array.isArray(res)){
    pa(res);
  } else if (Number.isFinite(res) || typeof res=='string'){
    print(res);
  }
};

const main = ()=>{
  if (IS_MULTITEST){
    let T = +readline()
    for (testN=1; testN<=T; testN++)
      pcalc(T);
  } else
   pcalc();
};

const lines = [];
const rl = require('readline').createInterface({
   input: process.stdin, output: process.stdout});
rl.on('line', line=>{
  lines.push(line);
});
rl.on('close', main);
let rli = 0;
const readline = ()=>lines[rli++];
const L = process.env.DEBUG ? console.log : ()=>{};
const LT = obj=>L(`Test #${testN}`, obj);

const ra = ()=>readline().split(' ').map(a=>+a)
const pa = arr=>print(arr.join(' '));

// SOLUTION:
IS_MULTITEST = 1;

const calc = ()=>{
  // READING:
  let [a, b, c, x, y] = ra();
  LT({a, b, c, x, y});

  // PROCESSING:
  x -= a;
  y -= b;
  if (Math.max(x, 0)+Math.max(y, 0)<=c)
    return 'YES';
  return 'NO'
};

var t = Number(readline());

for (var i = 0; i < t; i++) {
    var arr = readline().split(" ");
    var a = Number(arr[0]);
    var b = Number(arr[1]);
    var c = Number(arr[2]);
    var x = Number(arr[3]);
    var y = Number(arr[4]);
    if (x <= a + c) {
        c -= Math.max(x - a, 0);
        if (y <= b + c) {
            print("YES");
            continue;
        }
    }
    print("NO");
}

var t = Number(readline());

for(var i = 0; i < t; i++){
    var arr = readline().split(' ');

    print('NO');
}

var t = Number(readline());
var str = readline().split('');
var arr = [];
str.forEach((cur, i) => {
    arr.push(str.slice(i,i+2).join(''));
});
var obj= {}, maxEl, maxCount = 0;
arr.forEach((cur, i) => {
    if(!obj[cur]){
        obj[cur] = 1;
    }else{
      obj[cur]++;
    }

    if(obj[cur] > maxCount){
        maxCount = obj[cur];
        maxEl = cur;
    }
});

print(maxEl);

var t = +readline()

for(var i = 0; i< t; i ++) {
    var a = readline().split(' ').map((t) => +t)
    print(Task1(a[0],a[1],a[2],a[3],a[4]));
}

function Task1 (a,b,c,x,y){

  var test1 = a-x;
  var test2 = b-y;

  if (test1 < 0) {
    var just = test1;
    test1 += c;
    c += just;
  }
  if (test2 < 0){
    test2 += c;
  }

  return test1 >= 0 && test2 >= 0;

}

const readline = require("readline");

var input = [];

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

rl.on("line", function (val) {
  input.push(val);
});

rl.on("close", function () {
  for (let i = 1; i <= input[0]; i++) {
    var entities = input[i].split(" ").map(x => parseInt(x))
    var dogFoodEater = entities[0] - entities[3];
    var catFoodEater = entities[1] - entities[4];
    if (dogFoodEater + catFoodEater > entities[2]) {
        console.log("NO");
    } else {
        console.log("YES");
    }
  }

  process.exit(0);
});

const fs = require('fs');
const input = fs.readFileSync(0, 'utf8').trim().split('\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}
function alpha() {
  var g = []
  var f = 'abcdefghijklmnopqrstuvwxyz';
  for(var x=0;x<f.length;x++) g[f[x]] = x+1;
  return g;
}
function Alpha() {
  var g = []
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  for(var x=0;x<f.length;x++) g[f[x]] = x+1;
  return g;
}
var T = readline();
for(var q=1;q<=T;q++) {
  var [a,b,c,x,y] = readline().split(' ').map((x)=> parseInt(x))
  a -= x;
  b -= y;
  a = Math.min(a, 0);
  b = Math.min(b, 0)
  var ans = 0;
  if(c+(a+b) >= 0) ans = 1;
  else ans = 0;
  console.log(ans);
  //fs.appendFileSync('output.txt', (ans?'YES':'NO')+'\r\n');
  //os.EOL
}
//fs.writeFileSync('output.txt', f.join(' '));


var t = parseInt(readline());

  function solve() {
    var n = parseInt(readline());
    var s = readline();
    var m = 0;
    var v = []
    var w = []
    var ans = [];
    var a = 0;
    var b = 0;
    for (var i = 0; i < n; i++) {
      if (s[i] == '1') {
        b++;
        v.push(i);
      }
    }
    for (var i = n - 1; i >= 0; i--) {
      if (s[i] == '0') {
        a++;
        w.push(i);
      }
    }
    for (var i = 0; i < v.length; i++) {
      if (v[i] < n - b) {
        m++;
        ans.push(v[i]);
      }
    }
    for (var i = 0; i < w.length; i++) {
      if (w[i] > a - 1) {
        m++;
        ans.push(w[i]);
      }
    }

    if (ans.length == 0)
      return "0";
    else {
      ans.sort((a, b) => {
        if (a > b) {
          return 1;
        } else if (a < b) {
          return -1;
        } else {
          return 0;
        }
      });
      var val1 = "1\n";
      var val2 = ans.length + " ";
      for (var i = 0; i < ans.length; i++) {
        val2 += ans[i] + 1 + " "
      }
      return val1 + val2
    }
  }

  while (t--) {
    print(solve())
  }

const solve = (n,arr)=>{
     let res = [];
     for(let i=n-1;i>=0;i--){
          if(arr[i]===0){
               let j = 0,k=0;
               while((!arr[i])&& (i>j)){
                    if(arr[j]){
                         [arr[i],arr[j]] = [arr[j],arr[i]];
                         res.push(i+1);
                         res.push(j+1);
                         i--;
                    }
                    j++;
               }
          }
     }
     if(res.length===0) console.log(0);
     else{
     console.log(1);
     res.sort((a,b)=>a-b);
     console.log(`${res.length} ${res.join(" ")}`);
     }
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       let arr =  readline().trim().split("").map(cur=>parseInt(cur));
       solve(n,arr);
  }
}
main();

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = BigInt(next());
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var N = nextInt();
		var s = nextCharArray();
		var output = [];
		for(var i = 0; i < N; i++){
			if(s[i] == "1"){
				for(var j = N - 1; j >= i; j--){
					if(s[j] == "0"){
						output.push(i + 1);
						output.push(j + 1);
						s[i] = "-1";
						s[j] = "-1";
						break;
					}
				}
			}
		}
		if(output.length > 0){
			myout(1);
			output.sort(function(a,b){
				return a - b;
			});
			output.unshift(output.length);
			myout(myconv(output, 8));
		}else{
			myout(0);
		}
	}
}

let stop = '';
process.stdin.on('data', c => stop += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = stop.split(EOL);
    var t = lines[0];
    var n = 0;
    for(i = 1; i <= t * 2; i++){
        if(i % 2 == 1){
            n = lines[i];
        }else{
            var a = lines[i].split('').map(Number);
            var b = lines[i].split('').map(Number).sort(function(a, b){return a - b});
            var c = '', d = '';
            for(j = 0; j < n; j++){
                c += a[j];
                d += b[j];
            }
            if(c == d){
                console.log(0);
            }else{
                var ans = '';
                console.log(1);
                var v = [];
                for(j = 0; j < n; j++){
                    if(a[j] != b[j]){
                        v.push(j + 1);
                    }
                }
                ans += v.length + ' ';
                for(j = 0; j < v.length; j++){
                    ans += v[j] + ' ';
                }
                console.log(ans);
            }
        }
    }
});

const readline = require('readline')

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
})
const lines = []
rl.on('line', (input) => {
    lines.push(input);
})
rl.on('close', () => {
// (function() {
    // const lines = require('fs').readFileSync('test.in', 'utf8').split('\n')
    let l = 0;
    let t = +lines[l++]
    const output = []
    for (let i = 0; i < t; i++) {
        const n = +lines[l++]
        const str = lines[l++]
        output[i] = solve(n, str)
    }
    console.log(output.join('\n'))
// })()
})

function solve(n, str) {
    let c = 0
    for (let i = 0; i < str.length; i++) {
        if (str[i] === '0') c++
    }

    const ans = []
    for (let i = 0; i < str.length; i++) {
        if (i < c) {
            if (str[i] === '1') ans.push(i + 1)
        } else {
            if (str[i] === '0') ans.push(i + 1)
        }
    }
    if (!ans.length) return 0
    return `1\n${ans.length} ${ans.join(' ')}`
}

var t = parseInt(readline());

  function solve() {
    var n = parseInt(readline());
    var s = readline();
    var v = []
    var w = []
    var ans = [];
    var a = 0;
    var b = 0;
    for (var i = 0; i < n; i++) {
      if (s[i] == '1') {
        b++;
        v.push(i);
      } else {
        a++;
        w.push(i);
      }
    }

    for (var i = 0; i < v.length; i++) {
      if (v[i] < n - b) {
        ans.push(v[i]);
      }
    }
    for (var i = 0; i < w.length; i++) {
      if (w[i] > a - 1) {
        ans.push(w[i]);
      }
    }

    if (ans.length == 0)
      return "0";
    else {
      ans.sort((a, b) => {
        if (a > b) {
          return 1;
        } else if (a < b) {
          return -1;
        } else {
          return 0;
        }
      });
      var val1 = "1\n";
      var val2 = ans.length + " ";
      for (var i = 0; i < ans.length; i++) {
        val2 += ans[i] + 1 + " "
      }
      return val1 + val2
    }
  }

  while (t--) {
    print(solve())
  }

const solve = (n,arr)=>{
     let r = [];
     for(let i=n-1;i>=0;i--){
          if(arr[i]===0){
               let j = 0,res=[],k=0;
               while((!arr[i])&& (i>j)){
                    if(arr[j]){
                         [arr[i],arr[j]] = [arr[j],arr[i]];
                         res.push(i+1);
                         res.push(j+1);
                         i--;
                    }
                    j++;
               }
               if(res.length) {
                    res.sort((a,b)=>a-b);
                    r.push(`${res.length} ${res.join(" ")}`);
               }
          }
     }
     if(r.length===0) console.log(0);
     else{
          console.log(r.length);
          for(let i=0;i<r.length;i++) console.log(r[i]);
     }
}
function main() {
  const fs = require("fs");
  const input = fs.readFileSync(0, "utf8").trim().split("\n");
  let line = 0;
  function readline() {
    return input[line++];
  }
  let t = parseInt(readline());
  for(let i=0;i<t;i++){
       let n = parseInt(readline());
       let arr =  readline().trim().split("").map(cur=>parseInt(cur));
       solve(n,arr);
  }
}
main();

//Don't have to see. start------------------------------------------
var read = require('readline').createInterface({
	input: process.stdin, output: process.stdout
});
var obj; var inLine = []; var outputList = [];var retcode = new Set();
read.on('line', function(input){
	var tmp = input.split(' ');
	for(var i = 0; i < tmp.length; i++){
		inLine.push(tmp[i]);
		if(i == tmp.length - 1){
			retcode.add(inLine.length);
		}
	}
});
read.on('close', function(){
	obj = init(inLine);
	console.error('\n↑入力 ↓出力');
	Main();
	console.log(myconv(outputList, 9));
});
function makeClone(obj){return (obj instanceof Set) ? new Set(Array.from(obj)) : JSON.parse(JSON.stringify(obj));}
function nextArray(size, code){
	var ret = new Array(size);
	for(var i = 0; i < size; i++){
		if(code == 'int'){
			ret[i] = nextInt();
		}else if(code == 'long'){
			ret[i] = BigInt(next());
		}else{
			ret[i] = next();
		}
	}
	return ret;
}
function nextIntArray(size){return nextArray(size, 'int');} function nextStrArray(size){return nextArray(size, 'str');} function nextLongArray(size){return nextArray(size, 'long');}
function nextCharArray(){return myconv(next(),6);}
function next(){return obj.next();} function hasNext(){return obj.hasNext();} function nextInt(){return myconv(next(),1);} function nextLong(){return BigInt(next());}
function getCountMap(list){
	var map = {};
	for(var i = 0; i < list.length; i++){
		if(map[list[i]] == null){
			map[list[i]] = 0;
		}
		map[list[i]]++;
	}
	return map;
}
function init(input){
	return {
		list : input, index : 0, max : input.length,
		hasNext : function(){return (this.index < this.max);},
		next : function(){if(this.hasNext()){return this.list[this.index++];}else{throw 'ArrayIndexOutOfBoundsException ‚There is no more input';}},
		isReturn : function(){return retcode.has(this.index);}
	};
}
function myout(s){outputList.push(s);}
function myerr(s){console.error('debug:' + require('util').inspect(s,false,null));}
function isReturn(){return obj.isReturn();}
//param "no" is
//unknown or outlier : return i. 1: parseInt.
//2: split space. 4: split space and parseInt.
//6: split 1 character. 7: split 1 character and parseInt.
//8: join space. 9: join nextline. 0: join no character.
function myconv(i,no){try{switch(no){case 1:return parseInt(i);case 2:return i.split(' ');case 4:return i.split(' ').map(Number);case 6:return i.split('');case 7:return i.split('').map(Number);case 8:return i.join(' ');case 9:return i.join('\n');case 0:return i.join('');default:return i;}}catch(e){return i;}}

//Don't have to see. end------------------------------------------
function Main(){
	var t = nextInt();
	while(hasNext()){
		var N = nextInt();
		var s = nextCharArray();
		var output = [];
		while(true){
			var zero = [];
			var one = [];
			var change = [];
			for(var i = N - 1; i >= 0; i--){
				if(s[i] == "0" && one.length == 0){
					zero.unshift(i);
					change.unshift(i);
				}else if(s[i] == "1" && zero.length > 0){
					one.unshift(i);
					change.unshift(i);
				}
			}
			if(zero.length > 0 && one.length > 0){
				var size = change.length;
				for(var i = 0; i < Math.floor(change.length / 2); i++){
					var tmp = s[change[i]];
					s[change[i]] = s[change[change.length - 1 - i]];
					s[change[change.length - 1 - i]] = tmp;
				}
				for(var i = 0; i < change.length; i++){
					change[i]++;
				}
				output.push(size + " " + myconv(change, 8));
			}else{
				break;
			}
		}
		myout(output.length);
		if(output.length > 0){
			myout(myconv(output, 9));
		}

	}
}


const readNumber = () => +readline();
const readStr = () => readline();

const n = readNumber();

for(var i = 0; i < n; i++) {
  var m = readNumber();
  var str = readStr();
  var str2 = str.split('').sort().join('');
  var count = 0;

  for(var j = 0; j < m; j++) {
    if(str[j] !== str2[j]) {
      count++;
    }
  }

  print(count);
}

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e =0
	for(j = 1; j <= n * 2; j++){
	    if(j % 2 == 1){
	        e = lines[j]
	    }else{
            var k = lines[j].split(''), a = lines[j].split('').sort(), p = 0
            for(l = 0; l < e; l++){
                if(k[l] != a[l]){
                    p++
                }
            }
            console.log(p)
	    }
    }
});

let y = '';
process.stdin.on('data', c => y += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = y.split(EOL);
	var t = lines[0];
	var e = 0;
	for(i = 1; i <= t * 2; i++){
        if(i % 2 == 1){
            e = lines[i];
        }else{
            var k = lines[i].split('').sort();
            var l = lines[i].split('');
            var ans = 0;
            for(j = 0; j < e; j++){
               if(k[j] != l[j]){
                   ans++;
               }
            }
            console.log(ans);
        }
	}
});

// Input handling (Ctrl-D for EOF)

const readlineModule = require("readline");
const rl = readlineModule.createInterface({
    input: process.stdin,
    output: process.stdout
});

const input = [];
let currLine = 0;

rl.on("line", line => {
    input.push(line);
});

rl.on("close", () => {
    main();
})

function readline() {
    return input[currLine++];
}

// Main function

function main() {
    const T = parseInt(readline());
    for (let i = 0; i < T; i++) {
        const N = parseInt(readline());
        const str = readline().split('');
        const sortedStr = str.slice();
        sortedStr.sort();
        let ans = 0;
        for (let j = 0; j < N; j++) {
            if (str[j] !== sortedStr[j]) ans++;
        }
        console.log(ans);
    }
}

const fs = require('fs');

const input = String(fs.readFileSync(0))
  .split('\n')
  .filter(Boolean)
  .map((str) => str.trim());

for (let i = 2; i < input.length; i += 2) {
  const n = input[i];
  console.log(solve(n));
}

function solve(s) {
  const sor = s.split('').sort().join('');

  let k = 0;
  for (let i = 0; i < s.length; i++) {
    if (sor[i] !== s[i]) {
      k += 1;
    }
  }

  return k;
}

let i = "";
process.stdin.on("data", c => (i += c));
process.stdin.on("end", () => {
	const { EOL } = require("os");
	const lines = i.split(EOL);
	solve(lines);
});

function solve(lines) {
    t =  +lines[0];
    for(let i = 0; i < t; i++) {
        let l = +lines[i * 2 + 1];
        let s = lines[i * 2 + 2];
        let sorted = s.split("");
        sorted.sort();
        let k = 0;
        for(let j=0; j < l ;j++) {
            if(sorted[j] !==  s.charAt(j)){
                k++;
            }
        }
        console.log(k);

    }

}

let i = '';//.map(Number).sort(function(a, b){return a - b})
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e = 0
	for(j = 1; j <= n * 2; j++){
	    if(j % 2 == 1){
	        e = lines[j], ans = 0
	    }else{
	        var k = lines[j].split('')
	        var a = lines[j].split('').sort()
	        for(l = 0; l < e; l++){
	            if(k[l] != a[l]){
	                ans++
	            }
	        }
	        console.log(ans)
	    }
	}

});//k[k.length - 1] == '' ||

let i = '';//.map(Number).sort(function(a, b){return a - b})
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e = 0
	for(j = 1; j <= n * 2; j++){
	    if(j % 2 == 1){
	        e = lines[j], ans = 0
	    }else{
	        var k = lines[j].split('')
	        var a = lines[j].split('').sort()
	        for(l = 0; l < e; l++){
	            if(k[l] == a[l]){
	                ans++
	            }
	        }
	        console.log(ans)
	    }
	}

});//k[k.length - 1] == '' ||

let i = '';//.map(Number).sort(function(a, b){return a - b})
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e = 0
	for(j = 1; j <= n * 2; j++){
	    if(j % 2 == 1){
	        e = lines[j], ans = 0
	    }else{
	        var k = lines[j].split('')
	        var a = lines[j].split('').sort(function(a, b){
	           return a - b
	        })
	        for(l = 0; l < e; l++){
	            if(k[l] == a[l]){
	                ans++
	            }
	        }
	        console.log(ans)
	    }
	}

});//k[k.length - 1] == '' ||

let i = '';//.map(Number).sort(function(a, b){return a - b})
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e = 0
	for(j = 1; j <= n; j++){
	    if(j % 2 == 1){
	        e = lines[j], ans = 0
	    }else{
	        var k = lines[j].split('')
	        var a = lines[j].split('').sort(function(a, b){
	           return a - b
	        })
	        for(l = 0; l < e; l++){
	            if(k[l] == a[l]){
	                ans++
	            }
	        }
	        console.log(ans)
	    }
	}

});//k[k.length - 1] == '' ||

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e =0
	for(j = 1; j <= n * 2; j++){
	    if(j % 2 == 1){
	        e = lines[j]
	    }else{
            var k = lines[j].split(''), a = lines[j].split('').sort(), p = 0
            for(l = 0; l < e; l++){
                if(k[l] == a[l]){
                    p++
                }
            }
            console.log(p)
	    }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0], e =0
	for(j = 1; j <= n; j++){
	    if(j % 2 == 1){
	        e = lines[j]
	    }else{
            var k = lines[j].split(''), a = lines[j].split('').sort(), p = 0
            for(l = 0; l < e; l++){
                if(k[l] == a[l]){
                    p++
                }
            }
            console.log(p)
	    }
    }
});

let i = '';
process.stdin.on('data', c => i += c);
process.stdin.on('end', () => {
    const {EOL} = require('os');
    const lines = i.split(EOL);
    var n = lines[0];
    console.log(2 + "/n" + 6 + "/n" + 0 + "/n" + 4 + "/n")
});