let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=parseInt(line0[0]);
    let s=parseInt(line0[1]);

    if (line1[0] === '0') {console.log('NO'); return ;}
    if (line1[s-1] === '1') {console.log('YES');return;}
    if (line1[s-1] === '0' && line2[s-1]=== '0') {console.log('NO'); return ;}
    for (let i=s-1; i<n ; i++){

        if (line1[i] === '1' && line2[i] === '1')  {console.log('YES'); return ;}
    }

    console.log('NO');

}

var ns = readline().split(' ');
var a = readline().split(' ');
var b = readline().split(' ');

var n = parseInt(ns[0]);
var s = parseInt(ns[1]);
var bobStation = 1;

var checkMetro = function (stations, aliceStation, goStationsStatus, returnStationsStatus) {
    if (goStationsStatus[bobStation - 1] == 0) {
        print('NO');
        return;
    }

    if (goStationsStatus[aliceStation - 1] == 0
        && returnStationsStatus[aliceStation - 1] == 0) {
        print('NO');
        return;
    }

    if (goStationsStatus[aliceStation - 1] == 1) {
        print('YES');
        return;
    }

    for (var i = aliceStation + 1; i <= stations; i++) {
        if (goStationsStatus[i - 1] == 1 && returnStationsStatus[i - 1] == 1) {
            print('YES');
            return;
        }
    }
    print('NO');
}

checkMetro(n, s, a, b);

var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
var n=numbers[0]
var goal_node=numbers[1]
var a_line=readline().split(" ").map(function(x) { return parseInt(x); });
var b_line=readline().split(" ").map(function(x) { return parseInt(x); });
var yes=false

if(a_line[0] == 0 || (a_line[goal_node-1] == 0 && b_line[goal_node-1] == 0)) {
  yes=false
}
else if(a_line[goal_node-1] == 1) {
  yes=true
} else {
  for(var i=goal_node-1;i<n;i++) {
    if(a_line[i] == 1 && b_line[i] == 1) {
      yes=true;
      break;
    }
  }
}

// if(a_line[0] == 0 || (a_line[goal_node-1] == 0 && b_line[goal_node-1] == 0)) {
//   yes=false
// } else {
//   var visited = [];
//   var a = []
//   for(var i=0; i<n*2; i++) { visited[i] = new Array(n*2); a[i] = new Array(n*2); }

//   for(var i=0; i<n*2; i++) {
//     for(var j=0;j<n;j++) {
//       if (i < n) {
//         if (j > i && a_line[i] != 0 && a_line[j] != 0) { a[i][j] = 1 }
//         if (i == j && a_line[j] != 0 && b_line[j] != 0) { a[i][n+j] = 1; a[n+j][i] = 1; }
//       } else {
//         if (n+j < i && b_line[j] != 0) { a[i][n+j] = 1 }
//       }
//     }
//   }

//   var queue=[]
//   queue.push([0,0]);
//   visited[0][0] = true;

//   while(queue.length != 0) {
//     var node = queue.shift();
//     if(node[0] == goal_node-1 || node[1] == goal_node-1 || node[0] == n+goal_node-1 || node[1] == n+goal_node-1) {
//       yes=true;
//       break;
//     }
//     for(var i=0;i<n*2;i++) {
//       if (a[node[1]][i] == 1) {
//         child = a[node[1]][i]
//         if(visited[node[1]][i] != true) {
//           queue.push([node[1],i]);
//           visited[node[1]][i] = true;
//         }
//       }
//     }
//   }
// }

if (yes) {
  print("YES")
} else {
  print("NO")
}

var st = readline().split(" ");
var al = +st[1];
way1 = readline().split(" ");
way2 = readline().split(" ");

function haveStation(pos){
    for(var i = pos; i < way1.length; i++){
        if(way1[i] === '1' && way2[i] === '1') return true;
    }
    return false;
}
if(way1[0] === '0' ||
    (way1[al - 1] === '0' && (way2[al - 1] === '0')) ||
    (!haveStation(al) && way1[al - 1] === '0')
)
    print("NO");
else
    print("YES");

var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
var n=numbers[0]
var goal_node=numbers[1]
var a_line=readline().split(" ").map(function(x) { return parseInt(x); });
var b_line=readline().split(" ").map(function(x) { return parseInt(x); });
var yes=false

if(a_line[0] == 0 || (a_line[goal_node-1] == 0 && b_line[goal_node-1] == 0) || b_line[goal_node-1] == 0) {
  yes=false
} else {
  for(var i=goal_node-1;i<n;i++) {
    if(a_line[i] == 1 && b_line[i] == 1) {
      yes=true;
      break;
    }
  }
}

// if(a_line[0] == 0 || (a_line[goal_node-1] == 0 && b_line[goal_node-1] == 0)) {
//   yes=false
// } else {
//   var visited = [];
//   var a = []
//   for(var i=0; i<n*2; i++) { visited[i] = new Array(n*2); a[i] = new Array(n*2); }

//   for(var i=0; i<n*2; i++) {
//     for(var j=0;j<n;j++) {
//       if (i < n) {
//         if (j > i && a_line[i] != 0 && a_line[j] != 0) { a[i][j] = 1 }
//         if (i == j && a_line[j] != 0 && b_line[j] != 0) { a[i][n+j] = 1; a[n+j][i] = 1; }
//       } else {
//         if (n+j < i && b_line[j] != 0) { a[i][n+j] = 1 }
//       }
//     }
//   }

//   var queue=[]
//   queue.push([0,0]);
//   visited[0][0] = true;

//   while(queue.length != 0) {
//     var node = queue.shift();
//     if(node[0] == goal_node-1 || node[1] == goal_node-1 || node[0] == n+goal_node-1 || node[1] == n+goal_node-1) {
//       yes=true;
//       break;
//     }
//     for(var i=0;i<n*2;i++) {
//       if (a[node[1]][i] == 1) {
//         child = a[node[1]][i]
//         if(visited[node[1]][i] != true) {
//           queue.push([node[1],i]);
//           visited[node[1]][i] = true;
//         }
//       }
//     }
//   }
// }

if (yes) {
  print("YES")
} else {
  print("NO")
}

var numbers = readline().split(" ").map(function(x) { return parseInt(x); });
var n=numbers[0]
var goal_node=numbers[1]
var a_line=readline().split(" ").map(function(x) { return parseInt(x); });
var b_line=readline().split(" ").map(function(x) { return parseInt(x); });
var yes=false

if(a_line[0] == 0 || (a_line[goal_node-1] == 0 && b_line[goal_node-1] == 0)) {
  yes=false
} else {
  for(var i=goal_node-1;i<n;i++) {
    if(a_line[i] == 1 && b_line[i] == 1) {
      yes=true;
      break;
    }
  }
}

// if(a_line[0] == 0 || (a_line[goal_node-1] == 0 && b_line[goal_node-1] == 0)) {
//   yes=false
// } else {
//   var visited = [];
//   var a = []
//   for(var i=0; i<n*2; i++) { visited[i] = new Array(n*2); a[i] = new Array(n*2); }

//   for(var i=0; i<n*2; i++) {
//     for(var j=0;j<n;j++) {
//       if (i < n) {
//         if (j > i && a_line[i] != 0 && a_line[j] != 0) { a[i][j] = 1 }
//         if (i == j && a_line[j] != 0 && b_line[j] != 0) { a[i][n+j] = 1; a[n+j][i] = 1; }
//       } else {
//         if (n+j < i && b_line[j] != 0) { a[i][n+j] = 1 }
//       }
//     }
//   }

//   var queue=[]
//   queue.push([0,0]);
//   visited[0][0] = true;

//   while(queue.length != 0) {
//     var node = queue.shift();
//     if(node[0] == goal_node-1 || node[1] == goal_node-1 || node[0] == n+goal_node-1 || node[1] == n+goal_node-1) {
//       yes=true;
//       break;
//     }
//     for(var i=0;i<n*2;i++) {
//       if (a[node[1]][i] == 1) {
//         child = a[node[1]][i]
//         if(visited[node[1]][i] != true) {
//           queue.push([node[1],i]);
//           visited[node[1]][i] = true;
//         }
//       }
//     }
//   }
// }

if (yes) {
  print("YES")
} else {
  print("NO")
}

var st = readline().split(" ");
var bob = +st[0];
var al = +st[1];
way1 = readline().split(" ");
way2 = readline().split(" ");

function haveStation(pos){
    for(var i = way1[pos]; i < way1.length; i++){
        if(way1[i] && way2[i]) return true;
    }
    return false;
}
if(way1[0] === '0' ||
    (way1[al - 1] === '0' && (way2[al - 1] === '0')) ||
    (!haveStation(al) && way1[al - 1] === '0')
)
    print("NO");
else
    print("YES");

var st = readline().split(" ");
var bob = +st[0];
var al = +st[1];
way1 = readline().split(" ");
way2 = readline().split(" ");

function haveStation(arr, pos){
    for(var i = arr[pos]; i < arr.length; i++){
        if(arr) return true;
    }
    return false;
}
if(way1[0] === '0' || (way1[al - 1] === '0' && (way2[al - 1] === '0')) || (!haveStation(way1, al) && way1[al - 1] === '0'))
    print("NO");
else
    print("YES");

var st = readline().split(" ");
var bob = +st[0];
var al = +st[1];
way1 = readline().split(" ");
way2 = readline().split(" ");

function haveStation(pos){
    for(var i = pos; i < way1.length; i++){
        if(way1[i] && way2[i]) return true;
    }
    return false;
}
if(way1[0] === '0' ||
    (way1[al - 1] === '0' && (way2[al - 1] === '0')) ||
    (!haveStation(al) && way1[al - 1] === '0')
)
    print("NO");
else
    print("YES");

var st = readline().split(" ");
var bob = +st[0];
var al = +st[1];
way1 = readline().split(" ");
way2 = readline().split(" ");

function haveStation(arr, pos){
    for(var i = arr[pos]; i < arr.length; i++){
        if(arr) return true;
    }
    return false;
}
if(way1[0] === '0' || (way1[al - 1] === '0' && (way2[al - 1] === '0')) || !haveStation(way1, al))
    print("NO");
else
    print("YES");

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=line0[0];
    let s=line0[1];
    console.log(n,s);
    if ((line1[0] === 0) || (line2[s-1] === 0)) {console.log('NO'); return}

    for (let i=s-1; i<n ; i++){
        if ((line1[i] === 1) && (line2[i] === 1))  {console.log('YES'); return}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=parseInt(line0[0]);
    let s=parseInt(line0[1]);

    if (line1[0] === '0' || line2[s-1] === '0') {console.log('NO'); return ;}

    for (let i=s-1; i<n ; i++){

        if (line1[i] === '1' && line2[i] === '1')  {console.log('YES'); return ;}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=line0[0];
    let s=line0[1];
    //console.log(n,s);
    if ((line1[0] === 0) || (line2[s-1] === 0)) {console.log('NO'); return}

    for (let i=s-1; i<n ; i++){
        console.log(line1[i]);
        if ((line1[i] === 1) && (line2[i] === 1))  {console.log('YES'); return}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=line0[0];
    let s=line0[1];

    if (line1[0] === 0 || line2[s-1] === 0) {console.log('NO'); return}

    for (let i=s-1; i<n ; i++){
        if (line1[i] === 1 && line2[i] === 1)  {console.log('YES'); return}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=parseInt(line0[0]);
    let s=parseInt(line0[1]);

    if (line1[0] === '0') {console.log('NO'); return ;}

    for (let i=s-1; i<n ; i++){

        if (line1[i] === '1' && line2[i] === '1')  {console.log('YES'); return ;}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=parseInt(line0[0]);
    let s=parseInt(line0[1]);

    if (line1[0] === '0') {console.log('NO'); return ;}
    if (line1[s-1] === '1') {console.log('YES');return;}
    for (let i=s-1; i<n ; i++){

        if (line1[i] === '1' && line2[i] === '1')  {console.log('YES'); return ;}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=line0[0];
    let s=line0[1];

    if ((line1[0] === 0) || (line2[s-1] === 0)) {console.log('NO'); return}

    for (let i=s-1; i<n ; i++){
        if ((line1[i] === 1) && (line2[i] === 1))  {console.log('YES'); return}
    }

    console.log('NO');

}

let i = ''
process.stdin.on('data', c => i += c)
process.stdin.on('end', () => {
    const {EOL} = require('os')
    const lines = i.split(EOL) /*your input text, split by lines*/
    main(lines);
});

function main (lines) {
    let line0= lines[0].split(' ');
    let line1= lines[1].split(' ');
    let line2= lines[2].split(' ');
    let n=line0[0];
    let s=line0[1];
    //console.log(n,s);
    if ((line1[0] === 0) || (line2[s-1] === 0)) {console.log('NO'); return}

    for (let i=s-1; i<n ; i++){
        console.log(line1[i],line2[i]);
        if ((line1[i] === 1) && (line2[i] === 1))  {console.log('YES'); return}
    }

    console.log('NO');

}


var g = Number(readline());

var k = "";
var obj = {};
var obj2 = {};

var words = {};

for(i=0;i<g;i++){

	k = readline();
	obj = {};
	obj.str = k;
	obj.count = 0;

	k = k.split("");

	for(l=0;l<k.length;l++){
		if(k[l] == "a" || k[l] == "e" || k[l] == "o" || k[l] == "i" || k[l] == "u"){
			obj.last = k[l];
			obj.count++;
		}
	}

	if(!words.hasOwnProperty(obj.count))
	    words[obj.count] = {};

    if(!Array.isArray(words[obj.count][obj.last]))
        words[obj.count][obj.last] = [];

    words[obj.count][obj.last].push(obj.str);

}

var comp_duos = [];
var semi_comp_duos = [];

for(key in words){

    var cont = words[key];

    for(syl in cont){

        while(cont[syl].length >= 2){
            var first = cont[syl].pop();
            var second = cont[syl].pop();

            comp_duos.push([first, second]);
        }
    }

    var semi_comp = [];

    for(syl in cont){
        for(i=0;i<cont[syl].length;i++){
            semi_comp.push(cont[syl][i]);
        }

        cont[syl] = [];
    }

    while(semi_comp.length >= 2){
        var first = semi_comp.pop();
        var second = semi_comp.pop();

        semi_comp_duos.push([first, second]);
    }

}

var lyrics = [];

while(comp_duos.length != 0 && semi_comp_duos.length != 0){

    var semi_comp_duo = semi_comp_duos.pop();
    var comp_duo = comp_duos.pop();

    lyrics.push([semi_comp_duo[0], comp_duo[0]]);
    lyrics.push([semi_comp_duo[1], comp_duo[1]]);
}

while(comp_duos.length >= 2){
    var comp_duo1 = comp_duos.pop();
    var comp_duo2 = comp_duos.pop();

    lyrics.push([comp_duo1[0], comp_duo2[0]]);
    lyrics.push([comp_duo1[1], comp_duo2[1]]);
}

print(lyrics.length/2);
for(i= 0;i<lyrics.length;i++){
    var lyr = lyrics[i];

    print(lyr[0] + " " + lyr[1]);
}

var n = +readline();

function calc() {
    if (n < 4) {
        return [];
    }

    var result = [];
    var second = [];
    var first = [];
    var words = {};

    var vowels = /[aoiue]/g;
    function count(str) {
        var r = str.match(vowels);

        return r;
    }

    for (var i = 0; i < n; i++) {
        var input = readline();
        var r = input.match(vowels);
        var count = r.length;
        var last = r[count - 1];

        if (!words[count]) {
            words[count] = {
                array: []
            };
        }

        var hm = words[count];

        if (!hm[last]) {
            hm[last] = [input];
            hm.array.push(input);
        } else {
            var uniq = true;
            for (var j = 0; j < hm[last].length; j++) {
                var v = hm[last][j];
                second.push([input, v]);
                hm[last].splice(j, 1);
                hm.array.splice(hm.array.indexOf(v), 1);
                uniq = false;
                break;
            }

            if (uniq) {
                hm[last].push(input);
                hm.array.push(input);
            }
        }
    }

    for (var vow in words) {
        var arr = words[vow].array;

        for (var j = 0; j < arr.length - 1; j = j + 2) {
            first.push([arr[j], arr[j + 1]]);
        }
    }

    while (second.length >= 1 && (second.length + first.length) > 1) {
        var fp = null;
        if (first.length) {
            fp = first.pop();
        } else {
            fp = second.pop();
        }

        var sp = second.pop();

        result.push(fp[0] + ' ' + sp[0] + '\n' + fp[1] + ' ' + sp[1]);
    }

    return result;
}

var r = calc();
write(r.length);
if (r.length) {
    write('\n');
    write(r.join('\n'));
}

var n = +readline();

function calc() {
    if (n < 4) {
        return [];
    }

    var result = [];
    var second = [];
    var first = [];
    var words = {};

    var vowels = /[aoiue]/g;
    function count(str) {
        var r = str.match(vowels);

        return r;
    }

    for (var i = 0; i < n; i++) {
        var input = readline();
        var r = input.match(vowels);
        var count = r.length;
        var last = r[count - 1];

        if (!words[count]) {
            words[count] = {
                array: []
            };
        }

        var hm = words[count];

        if (!hm[last]) {
            hm[last] = [input];
            hm.array.push(input);
        } else {
            var uniq = true;
            for (var j = 0; j < hm[last].length; j++) {
                var v = hm[last][j];
                if (input !== v) {
                    second.push([input, v]);
                    hm[last].splice(j, 1);
                    hm.array.splice(hm.array.indexOf(v), 1);
                    uniq = false;
                    break;
                }
            }

            if (uniq) {
                hm[last].push(input);
                hm.array.push(input);
            }
        }
    }

    for (var vow in words) {
        var arr = words[vow].array;

        for (var j = 0; j < arr.length - 1; j = j + 2) {
            first.push([arr[j], arr[j + 1]]);
        }
    }

    while (second.length >= 1 && (second.length + first.length) > 1) {
        var fp = null;
        if (first.length) {
            fp = first.pop();
        } else {
            fp = second.pop();
        }

        var sp = second.pop();

        result.push(fp[0] + ' ' + sp[0] + '\n' + fp[1] + ' ' + sp[1]);
    }

    return result;
}

var r = calc();
write(r.length);
if (r.length) {
    write('\n');
    write(r.join('\n'));
}

var n = +readline();

function calc() {
    if (n < 4) {
        return [];
    }

    var result = [];
    var second = [];
    var first = [];
    var words = {};

    var vowels = /[aoiue]/g;
    function count(str) {
        var r = str.match(vowels);

        return r;
    }

    for (var i = 0; i < n; i++) {
        var input = readline();
        var r = input.match(vowels);
        var count = r.length;
        var last = r[count - 1];

        if (!words[count]) {
            words[count] = {
                array: []
            };
        }

        var hm = words[count];

        if (!hm[last]) {
            hm[last] = [input];
            hm.array.push(input);
        } else {
            var uniq = true;
            for (var j = 0; j < hm[last].length; j++) {
                var v = hm[last][j];
                if (input !== v) {
                    second.push([input, v]);
                    hm[last].splice(j, 1);
                    hm.array.splice(hm.array.indexOf(v), 1);
                    uniq = false;
                    break;
                }
            }

            if (uniq) {
                hm[last].push(input);
                hm.array.push(input);
            }
        }
    }

    for (var vow in words) {
        var arr = words[vow].array;

        for (var j = 0; j < arr.length - 1; j = j + 2) {
            first.push([arr[j], arr[j + 1]]);
        }
    }

    while (second.length >= 1 && (second.length + first.length) > 1) {
        var fp = null;
        if (first.length) {
            fp = first.pop();
        } else {
            fp = second.pop();
        }

        var sp = second.pop();

        result.push(fp[0] + ' ' + sp[0] + '\n' + fp[1] + ' ' + sp[1]);
    }

    return result;
}

var r = calc();
write(r.length);
write(r.join('\n'));


function D1437(array) {
  var partition = [];
  var current = [];
  var n = array.length;
  current.push(array[0]);

  for (var i = 1; i < n; i++) {
    if (array[i] < array[i - 1] || i === 1) {
      partition.push(current);
      current = [];
    }

    current.push(array[i]);
  }

  partition.push(current);
  var k = partition.length;
  var depth = Array(k).fill(null);
  depth[0] = 0;
  var cp = 0,
      cov = 1;

  while (cov < k) {
    var cl = partition[cp].length;

    for (var _i = cov; _i < Math.min(cov + cl, k); _i++) {
      depth[_i] = depth[cp] + 1;
    }

    cp++;
    cov += cl;
  }

  return depth[k - 1];
}

function execute(readline, print) {
  var iter = {
    next: function next() {
      return readline().trim();
    }
  };

  var split = function split(string) {
    return string.split(' ').map(Number);
  };

  var t = Number(iter.next());

  while (t--) {
    iter.next();
    var arr = split(iter.next());
    print(D1437(arr));
  }
}

execute(readline, print)

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

    D();
});

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

function pi(a) {
    return parseInt(a);
}

function ti(a){
    for(let i = 0; i < a.length; i++)
        a[i] = pi(a[i]);

    return a;
}

function D(){
    let t = pi(readline());
    while(t){
        t--;
        let n = pi(readline());
        let a = ti(readline().split(' '));
        let q = [a[0], null];
        let p = 1;
        let h = 0;
        while(q.length > 0 && p < n){
            let node = q.shift();
            if(node !== null){
                let x = p+1;
                while(x < n && a[x] >= a[x-1]){
                    q.push(x-1);
                    x++;
                }
                q.push(p);
                p = x;
            }else{
                h++;
                if(q.length > 0){
                    q.push(null);
                }
            }
        }

        if(q.length > 0)
            h++;

        console.log(h);
    }
}

var tc = parseInt(readline());
for (; tc--;){
    var n = parseInt(readline());
    var ar = readline().split(' ').map(x => parseInt(x));

    var level = 1;
    var ans = 0;
    var current = 0;

    for (var i = 1; i < n; i++){
        if (ar[i] < ar[i - 1]) {
            level--;
            if (level == 0) {
                ans++;
                level = current;
                current = 0;
            }
        }
        current++;
    }
    ans++; // root node
    print(ans);
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    let [node, map, result] = [1, new Map([[1, 0]]), 0];
    const queue = [];

    for (let i = 1; i < arr.length; i++) {
      const [prev, curr] = [arr[i - 1], arr[i]];
      if (curr < prev) {
        node = queue.shift();
      }
      queue.push(curr);
      map.set(curr, map.get(node) + 1);
      result = Math.max(result, map.get(curr));
    }

    console.log(result);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    let count = 0;

    for (let i = 0, j = 0; j < arr.length - 1; i++) {
      j++;
      while (arr[j + 1] - arr[j] >= 1) j++;
      count++;
    }

    console.log(count);
  }
}

let inputString = "";
let currentLine = 0;
process.stdin.on("data", (data) => {
  inputString += data;
});
process.stdin.on("end", function () {
  inputString = inputString
    .trim()
    .split("\n")
    .map((str) => str.trim());
  main();
});
function readLine() {
  return inputString[currentLine++];
}

// utils
const MOD = 1e9 + 7;
const mod = (n) => ((n % MOD) + MOD) % MOD;
const add = (a, b) => mod(mod(a) + mod(b));
const sub = (a, b) => mod(mod(a) - mod(b));
function mul() {
  for (var a = arguments, r = a[0], i = a.length; --i; )
    r = ((((r >> 16) * a[i]) % MOD) * 65536 + (r & 65535) * a[i]) % MOD;
  return r;
}
function inv(b) {
  for (var a = MOD, u = 0, v = 1, t; b; v = t)
    (t = (a / b) | 0), (a -= t * b), (u -= t * v), (t = a), (a = b), (b = t), (t = u), (u = v);
  u %= MOD;
  return u < 0 ? u + MOD : u;
}
function pow(base, n) {
  if (n === 0) return 1;
  let p = pow(base, Math.floor(n / 2));
  let res = mul(mod(p), mod(p));
  if (n % 2 === 1) res = mul(mod(res), base);
  return res;
}
function highestOneBit(i) {
  i |= i >> 1;
  i |= i >> 2;
  i |= i >> 4;
  i |= i >> 8;
  i |= i >> 16;
  return i - (i >>> 1);
}
function permutation(arr, len, store = "", permutations = []) {
  if (store.length === len) {
    permutations.push(store);
    return;
  }
  for (let i = 0; i < arr.length; i++) {
    const newArr = [...arr];
    const newChar = newArr.splice(i, 1);
    permutation(newArr, len, store.concat(newChar), permutations);
  }
  return permutations;
}

function main() {
  let t = +readLine();
  while (t--) {
    const len = +readLine();
    const arr = readLine().split(" ").map(Number);
    let count = 0;

    for (let i = 0, j = 0; j < arr.length - 1; i++) {
      j++;
      while (arr[j + 1] - arr[j] === 1) j++;
      count++;
    }

    console.log(count);
  }
}


function main()
{
    var a = readline(),
        b = readline();
    if ( a.length != b.length )
    {
        print("NO");
        return;
    }
    if ( a == b )
    {
        print("YES");
        return;
    }
    if ( a.match(/1/) && b.match(/1/) )
    {
        print("YES");
    }
    else
    {
        print("NO");
    }
}

main();

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

if ((x.length != y.length || x.search("1") == "-1" || y.search("1") == "-1") && x != y) {
	print("NO");
} else {
	print("YES");
}

(function main() {
    var s1 = readline();
    var s2 = readline();
    if (s1.length != s2.length) {
        print('NO');
        return;
    }
    if (s1 == s2 || s1.match(/1/) && s2.match(/1/))
        print('YES');
    else
        print('NO');
})();

print(function(a, b) {
  if (a.length != b.length) return 'NO';
  if (a.length === 1 && a !== b) return 'NO'
  if (a.indexOf(1) > -1 !== b.indexOf(1) > -1) return 'NO';
  return 'YES';
}(readline(), readline()));

function main(){
    var a = readline(),
        b = readline();
    if ( a.length != b.length ){
        print("NO");
        return;
    }
    if ( a == b ){
        print("YES");
        return;
    }
    if ( a.match(/1/) && b.match(/1/) ){
        print("YES");
    }
    else{
        print("NO");
    }
}

main();

function main()
{
    var a = readline(),
        b = readline();
    if ( a.length != b.length )
    {
        print("NO");
        return;
    }
    if ( a == b )
    {
        print("YES");
        return;
    }
    var cnt1 = 0,
        cnt2 = 0;
    for ( var i = 0; i < a.length; i++ )
    {
        if ( a[i] == "1" )
        {
            cnt1++;
        }
        if ( b[i] == "1" )
        {
            cnt2++;
        }
    }
    if ( cnt1 <= 0 || cnt2 <= 0 )
    {
        print("NO");
    }
    else
    {
        print("YES");
    }
}

main();

print(function(a, b) {
  if (a.length != b.length) return 'NO';
  if (a.length === 1 && a !== b) return 'NO'
  if (a.indexOf(1) === -1 && b.indexOf(1) > -1) return 'NO';
  return 'YES';
}(readline(), readline()));

print(function(a, b) {
  if (a.length != b.length) return 'NO';
  if (a.indexOf(1) === -1 && b.indexOf(1) > -1) return 'NO';
  return 'YES';
}(readline(), readline()));

function main()
{
    var a = readline(),
        b = readline();
    if ( a.length != b.length )
    {
        print("NO");
        return;
    }
    var cnt1 = 0,
        cnt2 = 0;
    for ( var i = 0; i < a.length; i++ )
    {
        if ( a[i] == "1" )
        {
            cnt1++;
        }
        if ( b[i] == "1" )
        {
            cnt2++;
        }
    }
    if ( cnt1 >= cnt2 )
    {
        print("YES");
    }
    else
    {
        print("NO");
    }
}

main();

function main()
{
    var a = readline(),
        b = readline();
    if ( a.length != b.length )
    {
        print("NO");
        return;
    }
    if ( a == b )
    {
        print("YES");
        return;
    }
    var cnt1 = 0,
        cnt2 = 0;
    for ( var i = 0; i < a.length; i++ )
    {
        if ( a[i] == "1" )
        {
            cnt1++;
        }
        if ( b[i] == "1" )
        {
            cnt2++;
        }
    }
    if ( cnt1 <= 0 && cnt2 <= 0 )
    {
        print("NO");
    }
    else
    {
        print("YES");
    }
}

main();

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

if (x.length != y.length || x.search("1") == "-1" || y.search("1") == "-1") {
	print("NO");
} else {
	print("YES");
}

function count(l) {
    if (l)
        return l.length;
    return 0;
}

(function main() {
    var s1 = readline();
    var s2 = readline();
    if (s1.length != s2.length) {
        print('NO');
        return;
    }
    var n1 = count(s1.match(/1/g));
    var n2 = count(s2.match(/1/g));
    if (n1 >= n2)
        print('YES');
    else
        print('NO');
})();


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

let currentline = 0;
function readline(){
    return input[currentline++];
}
let t = +readline();

while(t--){
    let [n,m,k] = readline().split(' ').map(x => +x);
    let a = readline().trim().split('').sort();
    let b = readline().trim().split('').sort();
    console.log(`${solve(a, b, n, m, k)}`)
}

function solve(str1, str2, n, m, k){
    let result = '';
    let idx1 = 0;
    let idx2 = 0;
    let count = k;
    let next;

    if(str1[idx1] < str2[idx2]){
        result += str1[idx1]
        idx1++;
        next = 'b';
    }else{
        result += str2[idx2]
        idx2++;
        next = 'a';
    }

    while(idx1 < n && idx2 < m){
        if(str1[idx1] <= str2[idx2] && next === 'a'){
            result += str1[idx1];
            idx1++;
            next = 'b';
            count = k;
        }else if(str2[idx2] <= str1[idx1] && next === 'b'){
            result += str2[idx2]
            idx2++;
            next = 'a';
            count = k;
        }else{
            if(str1[idx1] <= str2[idx2] && count > 1){
                result += str1[idx1];
                idx1++;
                count--;
                next = 'b';
            }else if(str2[idx2] <= str1[idx1] && count >1){
                result += str2[idx2]
                idx2++;
                next = 'a';
                count--;
            }else{
                if(next === 'a'){
                result += str1[idx1];
                idx1++;
                next = 'b';
                count = k;
             }else{
                result += str2[idx2]
                idx2++;
                next = 'a';
                count = k;
                }
            }
        }
    }
    return result;
}

process.stdin.resume();
process.stdin.setEncoding("utf-8");
//
let standardInputString = "";
let currentLine = 0;
//
process.stdin.on("data", (rawData) => {
  standardInputString += rawData;
});
process.stdin.on("end", (_) => {
  standardInputString = standardInputString
    .trim()
    .split("\n")
    .map((line) => {
      return line.trim();
    });
  main();
});

function readline() {
  return standardInputString[currentLine++];
}
function print(data) {
  console.log(data);
}

//line to array of integers
//readline().split(' ').map(Number)
//  let iter=parseInt(readline())
//multiple integer construction->const [a,b,c]=integer array

function main() {
  let iter = parseInt(readline());
  for (let i = 0; i < iter; i++) {
    let nmk = readline().split(" ").map(Number);
    let a = readline().split("").sort();
    let b = readline().split("").sort();
    print(DistinctC(a, b, nmk[2]));
  }
}

function DistinctC(a, b, k) {
  let taken_a = 0;
  let taken_b = 0;
  let i = 0;
  let j = 0;
  let c = "";
  while (i < a.length && j < b.length) {
    if (taken_a < k && taken_b < k) {
      if (a[i] < b[j]) {
        c += a[i];
        i++;
        taken_a++;
        taken_b = 0;
      } else {
        c += b[j];
        j++;
        taken_b++;
        taken_a = 0;
      }
    } else if (taken_a == k) {
      c += b[j];
      j++;
      taken_b++;
      taken_a = 0;
    } else {
      c += a[i];
      i++;
      taken_a++;
      taken_b = 0;
    }
  }
  return c;
}

'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 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 [n, m, k] = readline().split(' ').map(Number);
        let a = readline().split('').sort().join('');;
        let b = readline().split('').sort().join('');;
        let c = '';
        let pA = 0;
        let pB = 0;
        let opA = 0;
        let opB = 0;

        while (pA < a.length && pB < b.length) {
            if (a[pA] < b[pB] && opA < k) {
                c += a[pA];
                opB = 0;
                pA++;
                opA++;
            } else if (opB < k) {
                opA = 0;
                c += b[pB];
                pB++;
                opB++
            } else {
                c += a[pA];
                opB = 0;
                pA++;
                opA++;
            }
        }

        output(c);
    }
}

'use strict';

function solve(n, m, k, a, b) {
  a.sort((a, b) => b - a);
  b.sort((a, b) => b - a);
  const res = [];
  let pre = 0,
      count = 0;
  while (a.length && b.length) {
    if (a[a.length - 1] < b[b.length - 1]) {
      if (pre === 0 && count === k) {
        res.push(b.pop());
        pre = 1;
        count = 1;
      } else {
        if (pre === 1) {
          pre = 0;
          count = 0;
        }
        count++;
        res.push(a.pop());
      }
    } else {
      if (pre === 1 && count === k) {
        res.push(a.pop());
        pre = 0;
        count = 1;
      } else {
        if (pre === 0) {
          pre = 1;
          count = 0;
        }
        count++;
        res.push(b.pop());
      }
    }
  }
  return res.map(num => String.fromCharCode(num + 97)).join('');
}

async function main(read) {
  try {
    let res = [];
    let t = Number(await read());
    while (t--) {
      const [n, m, k] = (await read()).split(' ').map(Number);
      const a = (await read()).split('').map(ch => ch.charCodeAt(0) - 97);
      const b = (await read()).split('').map(ch => ch.charCodeAt(0) - 97);
      res.push(solve(n, m, k, a, b));
    }
    return res.join('\n');
  } catch (error) {
    console.log(error);
  }
}

let inputs,
    str = '';
function read() {
  return inputs.next().value.trim();
}
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', input => str += input);
process.stdin.on('end', async () => {
  inputs = str.split('\n').values();
  const output = await main(read);
  console.log(output);
});

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

let lines = -1,
  n,
  m,
  k,
  a,
  b;

const solve = (a, b, n, m, k) => {
  let temp_a = k,
    temp_b = k,
    sort_a = a.sort(),
    sort_b = b.sort(),
    c = [],
    i = 0;

  while (n > 0 && m > 0) {
    if ((sort_a[i] < sort_b[i] && temp_a !== 0) || temp_b === 0) {
      c.push(sort_a.shift());
      n--;
      temp_a--;
      temp_b = k;
    } else {
      c.push(sort_b.shift());
      m--;
      temp_b--;
      temp_a = k;
    }
  }
  console.log(c.join(""));
};

rl.on("line", function (line) {
  if (lines !== -1) {
    if (lines % 3 === 0) {
      [n, m, k] = line.split(" ").map(Number);
    }
    if (lines % 3 === 1) {
      a = line.split("");
    }
    if (lines % 3 === 2) {
      b = line.split("");
      // console.log(a, b, n, m, k);
      solve(a, b, n, m, k);
    }
  }
  lines++;
});

//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] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}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 nextDoubleArray(size){return nextArray(size, 'double');}
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 nextDouble(){return parseFloat(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;}}

function Main(){
    // test case in nodeJs
	var t = nextInt();
	while(hasNext()){
		solve();
	}
}

function solve() {
    let n = nextInt();
    let m = nextInt();
    let k = nextInt();
    let stringOne = nextCharArray().sort();
    let stringTwo = nextCharArray().sort();
    let c = [];
    let i = 0, j = 0, counter =[0 ,0];
    while(i < n && j < m) {
        if (stringOne[i].localeCompare(stringTwo[j]) < 0) {
            if (counter[0] < k) {
                c.push(stringOne[i]);
                i++;
                counter[0]++;
                counter[1]=0;
            } else {
                c.push(stringTwo[j]);
                j++;
                counter[0] = 0;
                counter[1]++;
            }
        } else {
            if (counter[1] < k) {
                c.push(stringTwo[j]);
                j++;
                counter[1]++;
                counter[0]=0;
            } else {
                c.push(stringOne[i]);
                i++;
                counter[1] = 0;
                counter[0]++;
            }
        }
    }

    myout(c.join(""));
}

process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', inputStdin => {
    inputString += inputStdin;
});
function readLine() {
    return inputString[currentLine++];
};
process.stdin.on('end', _ => {
    inputString = inputString.trim().split('\n').map(string => {
        return string.trim();
    });
    main();
    // cmd(not powershell): node cp.js < input.txt
});

function main() {
    let t = parseInt(readLine());
    while (t-- > 0) {
        console.log(solve());
    }
}
function solve() {
    let res = "";
    const [m, n, k] = readLine().split(" ").map(i => parseInt(i));
    let A = readLine().split("").sort((a, b) => a.localeCompare(b));
    let B = readLine().split("").sort((a, b) => a.localeCompare(b));
    let x = 0, y = 0, xb = 0, yb = 0;
    while (x < A.length && y < B.length) {
        if (((xb < k) && (A[x] < B[y]) || yb >= k)) {
            res += A[x++];
            xb++;
            yb = 0;
        } else {
            res += B[y++];
            yb++;
            xb = 0
        }
    }
    return res;
}

const fs = require('fs');
//const { addLeadingSlash } = require('history/PathUtils');
const input = fs.readFileSync(0, 'utf8').trim().split('\r\n');
let currentline = 0;
function readline(){
      return input[currentline++];
}

function alph() {
  var f = 'abcdefghijklmnopqrstuvwxyz';
  return f;
}
function Alph() {
  var f = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  return f;
}
function max(x,y) {
  return x >=y ? x : y;
}
function min(x,y) {
  return x <=y ? x : y;
}
function abs(x) {
  return x < 0 ? x*(-1) : x;
}
var T = readline();
for(var qe=1;qe<=T;qe++) {
  var [n, m, k] = readline().split(' ').map((x) => parseInt(x));
  var a = readline().split('');
  var b = readline().split('');
  a.sort();
  b.sort();

  var c = []
  var i = 0, j=0, cntA = 0, cntB = 0;
  while(i<a.length && j<b.length) {

    if(a[i]<=b[j] && cntA < k) {

      while(a[i]<=b[j] && cntA < k) {
        c.push(a[i]);
        i++;
        cntA++;
      }
      cntB = 0;
      continue;
    }
    if(a[i] > b[j] && cntB < k) {

      while(a[i] > b[j] && cntB < k) {
        c.push(b[j]);
        j++;
        cntB++;
      }
      cntA = 0;
      continue;
    }
    if(cntA == k) {
      c.push(b[j]);
      cntA = 0;
      j++
    }
    if(cntB == k) {
      c.push(a[i])
      cntB = 0;
      i++
    }

  }
  var ans = c.join('')

  console.log(ans);
  //fs.appendFileSync('output.txt', ans+'\r\n');

}
//

process.stdin.resume();
process.stdin.setEncoding("utf-8");
//
let standardInputString = "";
let currentLine = 0;
//
process.stdin.on("data", (rawData) => {
  standardInputString += rawData;
});
process.stdin.on("end", (_) => {
  standardInputString = standardInputString
    .trim()
    .split("\n")
    .map((line) => {
      return line.trim();
    });
  main();
});

function readline() {
  return standardInputString[currentLine++];
}
function print(data) {
  console.log(data);
}

//line to array of integers
//readline().split(' ').map(Number)
//  let iter=parseInt(readline())
//multiple integer construction->const [a,b,c]=integer array

function main() {
  let iter = parseInt(readline());
  for (let i = 0; i < iter; i++) {
    let nmk = readline().split(" ").map(Number);
    let a = readline().split("").sort();
    let b = readline().split("").sort();
    print(DistinctC(a, b, nmk[2]));
  }
}

function DistinctC(a, b, k) {
  let taken_a = 0;
  let taken_b = 0;
  let i = 0;
  let j = 0;
  let c = "";
  while (i < a.length && j < b.length) {
    if (taken_a < k && taken_b < k) {
      if (a[i] < b[j]) {
        c += a[i];
        i++;
        taken_a++;
      } else {
        c += b[j];
        j++;
        taken_b++;
      }
    } else if (taken_a == k) {
      c += b[j];
      j++;
      taken_b=1;
      taken_a = 0;
    } else {
      c += a[i];
      i++;
      taken_a=1;
      taken_b = 0;
    }
  }
  return c;
}

process.stdin.resume();
process.stdin.setEncoding("utf-8");
//
let standardInputString = "";
let currentLine = 0;
//
process.stdin.on("data", (rawData) => {
  standardInputString += rawData;
});
process.stdin.on("end", (_) => {
  standardInputString = standardInputString
    .trim()
    .split("\n")
    .map((line) => {
      return line.trim();
    });
  main();
});

function readline() {
  return standardInputString[currentLine++];
}
function print(data) {
  console.log(data);
}

//line to array of integers
//readline().split(' ').map(Number)
//  let iter=parseInt(readline())
//multiple integer construction->const [a,b,c]=integer array

function main() {
  let iter = parseInt(readline());
  for (let i = 0; i < iter; i++) {
    let nmk = readline().split(" ").map(Number);
    let a = readline().split("").sort();
    let b = readline().split("").sort();
    print(DistinctC(a.join(""), b.join(""), nmk[2]));
  }
}

function DistinctC(a, b, k) {
  let taken_a = 0;
  let taken_b = 0;
  let i = 0;
  let j = 0;
  let c = "";
  while (i < a.length && j < b.length) {
    if (taken_a < k && taken_b < k) {
      if (a[i] < b[j]) {
        c += a[i];
        i++;
        taken_a++;
      } else {
        c += b[j];
        j++;
        taken_b++;
      }
    } else if (taken_a == k) {
      c += b[j];
      j++;
      taken_b++;
      taken_a = 0;
    } else {
      c += a[i];
      i++;
      taken_a++;
      taken_b = 0;
    }
  }
  return c;
}

process.stdin.resume();
process.stdin.setEncoding("utf-8");
//
let standardInputString = "";
let currentLine = 0;
//
process.stdin.on("data", (rawData) => {
  standardInputString += rawData;
});
process.stdin.on("end", (_) => {
  standardInputString = standardInputString
    .trim()
    .split("\n")
    .map((line) => {
      return line.trim();
    });
  main();
});

function readline() {
  return standardInputString[currentLine++];
}
function print(data) {
  console.log(data);
}

//line to array of integers
//readline().split(' ').map(Number)
//  let iter=parseInt(readline())
//multiple integer construction->const [a,b,c]=integer array

function main() {
  let iter = parseInt(readline());
  for (let i = 0; i < iter; i++) {
    let nmk = readline().split(" ").map(Number);
    let a = readline().split("").sort();
    let b = readline().split("").sort();
    DistinctC(a, b, nmk[2]);
  }
}

function DistinctC(a, b, k) {
  let taken_a = 0;
  let taken_b = 0;
  let i = 0;
  let j = 0;
  let c = "";
  while (i < a.length && j < b.length) {
    if (taken_a < k && taken_b < k) {
      if (a[i] < b[j]) {
        c += a[i];
        i++;
        taken_a++;
      } else {
        c += b[j];
        j++;
        taken_b++;
      }
    } else if (taken_a == k) {
      c += b[j];
      j++;
      taken_b++;
      taken_a = 0;
    } else {
      c += a[i];
      i++;
      taken_a++;
      taken_b = 0;
    }
  }
  print(c);
}

process.stdin.resume();
process.stdin.setEncoding("utf-8");
//
let standardInputString = "";
let currentLine = 0;
//
process.stdin.on("data", (rawData) => {
  standardInputString += rawData;
});
process.stdin.on("end", (_) => {
  standardInputString = standardInputString
    .trim()
    .split("\n")
    .map((line) => {
      return line.trim();
    });
  main();
});

function readline() {
  return standardInputString[currentLine++];
}
function print(data) {
  console.log(data);
}

//line to array of integers
//readline().split(' ').map(Number)
//  let iter=parseInt(readline())
//multiple integer construction->const [a,b,c]=integer array

function main() {
  let iter = parseInt(readline());
  for (let i = 0; i < iter; i++) {
    let nmk = readline().split(" ").map(Number);
    let a = readline().split("").sort();
    let b = readline().split("").sort();
    print(DistinctC(a, b, nmk[2]));
  }
}

function DistinctC(a, b, k) {
  let taken_a = 0;
  let taken_b = 0;
  let i = 0;
  let j = 0;
  let c = "";
  while (i < a.length && j < b.length) {
    if (taken_a < k && taken_b < k) {
      if (a[i] < b[j]) {
        c += a[i];
        i++;
        taken_a++;
      } else {
        c += b[j];
        j++;
        taken_b++;
      }
    } else if (taken_a == k) {
      c += b[j];
      j++;
      taken_b++;
      taken_a = 0;
    } else {
      c += a[i];
      i++;
      taken_a++;
      taken_b = 0;
    }
  }
  return c;
}


"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 stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};

const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = [];
    for (let i = 0; i < n; i++) {
      let [j, ...k] = iInpArr();
      arr.push(k);
    }
    let narr = [];
    for (let i = 0; i < n; i++) {
      let j = arr[i];
      let en = arr[i][0] + 1;
      for (let j = 1; j < arr[i].length; j++) {
        en = Math.max(en, arr[i][j] - j + 1);
      }
      narr.push([en, arr[i].length]);
    }
    narr.sort((a, b) => a[0] - b[0]);
    let ans = narr[0][0],
      to = narr[0][1];
    for (let i = 1; i < narr.length; i++) {
      let j = ans + to;
      if (j < narr[i][0]) ans = narr[i][0] - to;
      to += narr[i][1];
    }
    console.log(ans);
  }
};
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));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const k = [];
		for (let i = 0; i < n; i++) {
			k[i] = rna().slice(1);
		}

		const v = [];
		for (const cave of k) {
			let mxpw = 0;
			for (let i = 0; i < cave.length; i++) {
				mxpw = Math.max(mxpw, cave[i] - i);
			}
			v.push([mxpw + 1, mxpw + 1 + cave.length]);
		}
		v.sort((x, y) => x[0] - y[0]);

		let mxd = 0;
		for (let i = 0, curd = 0; i < n - 1; i++) {
			curd += v[i+1][0] - v[i][1];
			mxd = Math.max(mxd, curd);
		}

		const ans = v[0][0] + mxd;
		console.log(ans);
	}
}

"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 stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
function iInpArr1() {
  return readline()
    .trim()
    .split("")
    .map((cur) => parseInt(cur));
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
const sqrt = (n) => {
  let low = 1n,
    high = n,
    ans;
  while (low <= high) {
    let mid = (low + high) / 2n;
    if (mid * mid <= n) {
      ans = mid;
      low = mid + 1n;
    } else high = mid - 1n;
  }
  return ans;
};

const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = [];
    for (let i = 0; i < n; i++) {
      let [j, ...k] = iInpArr();
      arr.push(k);
    }
    let narr = [];
    for (let i = 0; i < n; i++) {
      let j = arr[i];
      let en = arr[i][0] + 1;
      for (let j = 1; j < arr[i].length; j++) {
        en = Math.max(en, arr[i][j] - j);
      }
      narr.push([en, arr[i].length]);
    }
    narr.sort((a, b) => a[0] - b[0]);
    let ans = narr[0][0],
      to = narr[0][1];
    for (let i = 1; i < narr.length; i++) {
      let j = ans + to;
      if (j < narr[i][0]) ans = narr[i][0];
      to += narr[i][1];
    }
    console.log(ans);
  }
};
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));

function main () {
	let T = rn();
	while (T--) {
		const n = rn();
		const k = [];
		for (let i = 0; i < n; i++) {
			k[i] = rna().slice(1);
		}

		const v = [];
		for (const cave of k) {
			let mxpw = 0;
			for (let i = 0; i < cave.length; i++) {
				mxpw = Math.max(mxpw, cave[i] - i);
			}
			v.push([mxpw + 1, mxpw + 1 + cave.length]);
		}
		v.sort((x, y) => x[0] - y[0]);

		let mxd = 0;
		for (let i = 0; i < n - 1; i++) {
			mxd = Math.max(mxd, Math.max(0, v[i+1][0] - v[i][1]));
		}

		const ans = v[0][0] + mxd;
		console.log(ans);
	}
}


"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 stringArr1() {
  return readline().trim().split(" ");
}
function getValue() {
  return parseInt(readline());
}
let mod = 1e9 + 7;
mod = BigInt(mod);
const pow = (num, exp, mod) => {
  if (exp === BigInt(0)) return BigInt(1);
  let x = pow(num, exp / BigInt(2), mod);
  if (exp % BigInt(2)) return (((x * x) % mod) * num) % mod;
  else return (x * x) % mod;
};
const inverse = (num, exp) => {
  return pow(BigInt(num), BigInt(exp), BigInt(mod));
};
class graph {
  constructor() {
    this.list = {};
  }
  add(v) {
    if (this.list[v] !== undefined) return false;
    this.list[v] = [];
    return true;
  }
  addE(v, e) {
    this.list[v].push(e);
  }
}
let global = 0;
const dfs = (g, c) => {
  const helper = (v) => {
    if (g.list[v] === undefined) {
      if (c[v - 1] === "W") return [0, 1];
      else return [1, 0];
    }
    let b = 0,
      w = 0;
    let arr = g.list[v];
    for (let i = 0; i < arr.length; i++) {
      let k = helper(arr[i]);
      b += k[0];
      w += k[1];
    }
    if (c[v - 1] === "W") w++;
    else b++;
    if (b === w) global++;
    return [b, w];
  };
  helper(1);
};
const solve = () => {
  let t = getValue();
  while (t--) {
    let n = getValue();
    let arr = iInpArr();
    let colour = stringArr();
    let g = new graph();
    g.add(1);
    for (let i = 0, j = 2; i < n - 1; i++, j++) {
      g.add(arr[i]);
      g.addE(arr[i], j);
    }
    dfs(g, colour);
    console.log(global);
    global = 0;
  }
};
solve();

'use strict';
const readline = require('readline');
const util = require('util');
const lineReader = readline.createInterface({
    input: process.stdin,
});
let inputPos = 0;
const inputWords = [];
lineReader.on('line', (line) => {
    inputWords.push.apply(inputWords, line.split(' ').filter(s => s != ''));
});
lineReader.on('close', main);
function check(b) {
    if (!b) {
        process.stderr.write(`${new Error().stack}\n`);
        process.exit(-1);
    }
}
function printf(...args) {
    process.stdout.write(util.format(...args));
}
function nextInt() {
    return Math.trunc(Number(inputWords[inputPos++]));
}
function nextStr() {
    return inputWords[inputPos++];
}
function af(l) {
    return Array.from({ length: l });
}
const INIT = -1;
const INF = Infinity;
let n;
let p;
let col;
let adjList;
let bc;
let cnt;
function f(u, par) {
    cnt[u] = 1;
    if (col[u - 1] == 'B') {
        bc[u] = 1;
    }
    for (let v of adjList[u]) {
        if (v == par) {
            continue;
        }
        f(v, u);
        bc[u] += bc[v];
        cnt[u] += cnt[v];
    }
}
function solve() {
    bc = af(n + 1).fill(0);
    cnt = af(n + 1).fill(0);
    f(1, INIT);
    let ans = 0;
    for (let u = 1; u <= n; u++) {
        if (bc[u] + bc[u] == cnt[u]) {
            ans++;
        }
    }
    // printf("%j\n%j\n", bc, cnt);
    return ans;
}
function main() {
    let tc = nextInt();
    while (tc-- > 0) {
        n = nextInt();
        p = af(n + 1);
        adjList = Array.from({ length: n + 1 });
        for (let i = 1; i <= n; i++) {
            adjList[i] = [];
        }
        for (let i = 2; i <= n; i++) {
            p[i] = nextInt();
            adjList[p[i]].push(i);
        }
        col = nextStr();
        printf("%d\n", solve());
    }
}

function solve(inp) {
  var t = inp.nextNum();

  while(t--) {
    const n = inp.nextNum();
    const tree = new Array(n + 1);
    for (let i = 1; i <= n; i++) tree[i] = [];
    const par = inp.nextNumArr();
    for(let i = 0; i < n - 1; i++) {
      const p = par[i];
      tree[p].push(i + 2);
    }
    const col = inp.nextLine();

    const dfs = (i) => {
      var w = col.charAt(i - 1) === 'B' ? 1 : -1;
      var c = 0;
      tree[i].forEach((j) => {
        const [cw, cc] = dfs(j);
        w += cw;
        c += cc;
      });
      if (w === 0) c++;
      return [w, c];
    }

    const [w, c] = dfs(1);
    console.log(c);
  }
}

class Input {
    constructor(lines) {
        this.lines = lines;
        this.next = 0;
    }

    addLine(line) {
        this.lines.push(line)
    }

    hasLine() {
       return this.lines.size() > 0;
    }

    nextLine() {
        const line = this.lines[this.next];
        if (line) {
            this.next++;
        }
        return line;
    }

    nextNum() {
      return Number(this.nextLine());
    }

    nextNumArr() {
      return inp.nextLine().split(" ").map(x => Number(x));
    }
}

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

const inp = new Input([]);

readline.on('line', line => {
    inp.addLine(line);
});

readline.on('close', () => {
    solve(inp);
});

'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(n, a, s) {
  const g = Array.from({
    length: n + 1
  }, () => []);
  for (let i = 0; i < n - 1; i++) {
    const u = a[i],
          v = i + 2;
    g[u].push(v);
  }
  let res = 0;
  const dfs = i => {
    let b = 0,
        w = 0;
    if (s[i - 1] === 'B') b++;else w++;
    for (let child of g[i]) {
      const [rb, rw] = dfs(child);
      b += rb;
      w += rw;
    }
    if (b === w) res++;
    return [b, w];
  };
  dfs(1);
  return res.toString();
}

async function main(read) {
  let t = Number(await read());
  let res = [];
  while (t--) {
    const n = Number(await read());
    const a = (await read()).split(' ').map(Number);
    const s = await read();
    res.push(solve(n, a, s));
  }
  console.log(res.join('\n'));
}

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

//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] = nextLong();
		}else if(code == 'double'){
			ret[i] = nextDouble();
		}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 nextDoubleArray(size){return nextArray(size, 'double');}
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 nextDouble(){return parseFloat(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 list = new Array(N);
		for(var i = 0; i < N; i++){
			list[i] = {
				child : [],
				s : "",
				b : 0,
				w : 0
			};
		}
		for(var i = 1; i < N; i++){
			var p = nextInt() - 1;
			list[i].child.push(p);
			list[p].child.push(i);
		}
		var S = next();
		for(var i = 0; i < N; i++){
			list[i].s = S[i];
		}
		var output = 0;
		dfs(0, -1);
		myout(output);

		function dfs(now, parent){
			var b = 0;
			var w = 0;
			var child = list[now].child;
			for(var j = 0; j < child.length; j++){
				var nx = child[j];
				if(nx != parent){
					var ret = dfs(nx, now);
					b += ret.b;
					w += ret.w;
				}
			}
			if(list[now].s == "W"){
				w++;
			}else{
				b++;
			}
			if(b == w){
				output++;
			}
			return {
				b : b,
				w : w
			};
		}
	}
}

'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 [n] = ra();
  let P = ra();
  P = P.map(a=>a-1);
  let S = readline()
  // PROCESSING:
  let ans = 0;
  let G = {};
  for (let i=1; i<n; i++){
    if (!G[P[i-1]]) G[P[i-1]] = [];
    G[P[i-1]].push(i);
  }
  //LT({n, P, S, G});
  let dfs = (v)=>{
    let white = 0, black = 0;
    if (S[v]=='B') black++; else white++;
    let children = G[v]||[];
    //L({v, children})
    for (let i=0; i<children.length; i++){
      let res = dfs(children[i]);
      white += res.white
      black += res.black
    }
    if (white==black) ans++;
    return {white, black};
  };
  dfs(0);
  return 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 ps = lines[l++].trim().split(' ').map(Number)
        const arr = lines[l++]
        output[i] = solve(n, ps, arr)
    }
    console.log(output.join('\n'))
// })()
})

class Queue {
    constructor() {
        this.map = {}
        this.first = 0
        this.last = -1
    }
    push(...args) {
        let i = 0
        if (!this.length) {
            this.first = this.last = 0
            this.map[this.first] = args[i++]
        }
        for (; i < args.length; i++) {
            this.map[++this.last] = args[i]
        }
    }
    unshift(...args) {
        let i = 0
        if (!this.length) {
            this.first = this.last = 0
            this.map[this.first] = args[i++]
        }
        for (; i < args.length; i++) {
            this.map[--this.first] = args[i]
        }
    }
    pop() {
        const r = this.map[this.last]
        delete this.map[this.last]
        this.last--
        return r
    }
    shift() {
        const r = this.map[this.first]
        delete this.map[this.first]
        this.first++
        return r
    }
    get length() {
        if (this.first > this.last) return 0
        return this.last - this.first + 1
    }
    getLast() {
        return this.map[this.last]
    }
}

function solve(n, ps, arr) {
    const adj = {}
    ps.forEach((p, i) => {
        const a = i + 2
        const b = p
        adj[a] = adj[a] || []
        adj[b] = adj[b] || []
        adj[a].push(b)
        adj[b].push(a)
    })
    return dfs(adj, 1, arr)
}
function dfs(adj, r, arr) {
    // const stack = [[r, 0, -1]]
    const stack = new Queue()
    stack.push([r, 0, -1])
    const bs = []
    const ws = []
    let ans = 0
    while (stack.length) {
        const [u, i, p] = stack.getLast()// stack[stack.length - 1]
        // visited[u] = 1

        const nb = adj[u] || []
        if (!i) {
            // first visited
        }
        if (i < nb.length) {
            // stack[stack.length - 1][1]++
            stack.getLast()[1]++
            const v = nb[i]
            // if (!visited[v]) { // has circle
            if (v !== p) {
                stack.push([v, 0, u])
            }
        } else {
            // last visited
            stack.pop()
            bs[u] = arr[u - 1] === 'B' ? 1 : 0
            ws[u] = arr[u - 1] === 'W' ? 1 : 0
            ;(adj[u] || []).forEach(v => {
                if (v === p) return
                bs[u] += bs[v]
                ws[u] += ws[v]
            })
            if (bs[u] === ws[u]) ans++
        }
    }
    // console.log(bs, ws)
    return ans
}

//let { readline, print } = require('@ip-algorithmics/codeforces-io');
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var t = +readline();
while (t--) {
    var n = readline().split(' ').map(function (v) { return +v; })[0];
    var a = __spreadArray([0, 0], readline().split(' ').map(function (v) { return +v; }), true);
    var s = readline().split('');
    var c = Array(n + 1);
    for (var i = 0; i <= n; i++) {
        c[i] = [];
    }
    for (var i = 1; i <= n; i++) {
        c[a[i]].push(i);
    }
    /*
    function f (v) {
        let balance = 0, count = 0;
        for (let i=0; i<c[v].length; i++) {
            let res = f(c[v][i]);
            balance += res[0];
            count += res[1];
        }
        balance += s[v-1] === 'B' ? 1 : -1;
        count += +!balance;

        return [balance, count];
    }
    */
    var ba = Array(n + 1).fill(0);
    var co = Array(n + 1).fill(0);
    var st = Array(n + 1).fill(0);
    var sti = Array(n + 1).fill(0);
    var d = 0;
    st[d] = 1;
    root: while (true) {
        ba[d] = co[d] = sti[d] = 0;
        loop: while (true) {
            if (sti[d] < c[st[d]].length) {
                st[d + 1] = c[st[d]][sti[d]];
                d++;
                continue root;
            }
            ba[d] += s[st[d] - 1] === 'B' ? 1 : -1;
            co[d] += +!ba[d];
            if (!d)
                break root;
            d--;
            ba[d] += ba[d + 1];
            co[d] += co[d + 1];
            sti[d]++;
        }
    }
    var ans = co[0];
    //let ans = f(1)[1];
    print(ans);
}

//import { readline, print } from '@ip-algorithmics/codeforces-io';
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
    if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
        if (ar || !(i in from)) {
            if (!ar) ar = Array.prototype.slice.call(from, 0, i);
            ar[i] = from[i];
        }
    }
    return to.concat(ar || Array.prototype.slice.call(from));
};
var t = +readline();
var _loop_1 = function () {
    var n = readline().split(' ').map(function (v) { return +v; })[0];
    var a = __spreadArray([0, 0], readline().split(' ').map(function (v) { return +v; }), true);
    var s = readline().split('');
    var c = Array(n + 1);
    for (var i = 0; i <= n; i++) {
        c[i] = [];
    }
    for (var i = 1; i <= n; i++) {
        c[a[i]].push(i);
    }
    function f(v) {
        var balance = 0, count = 0;
        for (var i = 0; i < c[v].length; i++) {
            var res = f(c[v][i]);
            balance += res[0];
            count += res[1];
        }
        balance += s[v - 1] === 'B' ? 1 : -1;
        count += +!balance;
        return [balance, count];
    }
    var ans = f(1)[1];
    print(ans);
};
while (t--) {
    _loop_1();
}


function swap(items, firstIndex, secondIndex){
    const temp = items[firstIndex];
    items[firstIndex] = items[secondIndex];
    items[secondIndex] = temp;
}
function partition(items, left, right) {
    var pivot   = items[Math.floor((right + left) / 2)],
        i       = left,
        j       = right;

    while (i <= j) {
        while (items[i] < pivot) {
            i++;
        }
        while (items[j] > pivot) {
            j--;
        }
        if (i <= j) {
            swap(items, i, j);
            i++;
            j--;
        }
    }
    return i;
}
function quickSort(items, left, right) {
    var index;
    if (items.length > 1) {
        left = typeof left != "number" ? 0 : left;
        right = typeof right != "number" ? items.length - 1 : right;
        index = partition(items, left, right);
        if (left < index - 1) {
            quickSort(items, left, index - 1);
        }
        if (index < right) {
            quickSort(items, index, right);
        }
    }
    return items;
}

function unique(arr) {
  var obj = {};

  for (var i = 0; i < arr.length; i++) {
    var str = arr[i];
    obj[str] = true;
  }

  return Object.keys(obj); // или собрать ключи перебором для IE8-
}

function main(r) {
    var k = r().split(' ');
    var n = +k[0];
    k = +k[1];
    var a = r().split(' ').map(v => +v)
    a = quickSort(a);
    a = unique(a)
    var t = 0;
    for(var i = 0 ; i < k; i++) {
        if(a[i]){
            print(a[i] - t);
            t += a[i] - t;
        }
        else{
            print(0);
        }
    }

}
main(readline);

function doIt() {
    var repeats = readline().split(' ').map(v=>+v)[1];
    var arr = readline().split(' ').sort((a, b) => a - b);

    var prev = 0;

    for(var i = 0; i < arr.length; i++) {
        var v = arr[i];
        if(v === prev) continue;

        print(v - prev);
        if(!(--repeats)) break;

        prev = v;
    }

    while(repeats--) print(0);
}

doIt();

ip = readline().split(' ');
n = +ip[0], k = +ip[1];
sub = 0;
count = 0;
res = '';
ar = readline().split(' ').map(Number).sort((a,b)=>a-b);

for (i = 0; i < n; i++){
    if (ar[i] - sub > 0){
        res += (ar[i] - sub)+'\n';
        sub = ar[i];
        count++;
    }
    if (count === k) break;
}

for (;count < k;count++){
    res += '0\n';
}
print(res);

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

let l = 0;
let op = []
let array = []

rl.on('line', (input) => {
    if (l === 0) {
        op = input.split(' ').map(function (item) {
            return parseInt(item, 10);
        });
    } else if (l === 1) {
        rl.close();

        array = input.split(' ').map(function (item) {
            return parseInt(item, 10);
        });

        array.sort(function sortNumber(a, b) {
            return a - b;
        })
        let mn = array[0]
        console.log(mn)
        let amm=mn;
        let st=0
        let res=''
        for (let i = 0; i < op[1] - 1; i++) {
            let min = mn
            if (mn > 0) {
                mn = -1
                for (let j = st; j < array.length&&mn==-1; j++) {
                    array[j] -= amm;
                    if (array[j] > 0 && mn === -1) {
                        mn = array[j]
                        amm+=mn
                        res+=mn+'\n'
                    } else if(mn==-1){
                        st++
                    }
                }
            }
            if (mn === -1) {
                res+='0'+'\n'
            }
        }
        console.log(res)
    }
    l++
});

function swap(items, firstIndex, secondIndex){
    const temp = items[firstIndex];
    items[firstIndex] = items[secondIndex];
    items[secondIndex] = temp;
}
function partition(items, left, right) {
    var pivot   = items[Math.floor((right + left) / 2)],
        i       = left,
        j       = right;

    while (i <= j) {
        while (items[i] < pivot) {
            i++;
        }
        while (items[j] > pivot) {
            j--;
        }
        if (i <= j) {
            swap(items, i, j);
            i++;
            j--;
        }
    }
    return i;
}
function quickSort(items, left, right) {
    var index;
    if (items.length > 1) {
        left = typeof left != "number" ? 0 : left;
        right = typeof right != "number" ? items.length - 1 : right;
        index = partition(items, left, right);
        if (left < index - 1) {
            quickSort(items, left, index - 1);
        }
        if (index < right) {
            quickSort(items, index, right);
        }
    }
    return items;
}

function unique(arr) {
  var obj = {};

  for (var i = 0; i < arr.length; i++) {
    var str = arr[i];
    obj[str] = true;
  }

  return Object.keys(obj); // или собрать ключи перебором для IE8-
}

function main(r) {
    var k = r().split(' ');
    var n = +k[0];
    k = +k[1];
    var a = r().split(' ').map(v => +v)
    a = quickSort(a);
    a = unique(a)
    print(a)
    var t = 0;
    for(var i = 0 ; i < k; i++) {
        if(a[i]){
            print(a[i] - t);
            t += a[i] - t;
        }
        else{
            print(0);
        }
    }

}
main(readline);