logo AlgoBeat OnlineJudge
登录 注册

#209293. [ICPC 2022 Jinan R] Stack Sort

内存限制:512 MiB 时间限制:1000 ms 标准输入输出
题目类型:VJudge(洛谷) 评测方式:VJudge
上传者: 匿名

题目描述

You are given a permutation with numbers, .

You want to sort these numbers using stacks. Specifically, you should complete the following task:

Initially, all stacks are empty. You need to push each number to the top of one of the stacks one by one, in the order of . , you pop all the elements from the stacks in a clever order so that the first number you pop is , the second number you pop is , and so on. If you pop an element from a stack , you cannot pop any element from the other stacks until becomes empty.

What is the minimum possible to complete the task?

输入格式

The first line contains one integer , the number of test cases.

For each test case, the first line contains one positive integer . The next line contains integers denoting the permutation. It is guaranteed that form a permutation, i.e. for .

It is guaranteed that the sum of over all test cases is no more than .

输出格式

For each test case, output the minimum possible in one line.

样例

样例输入 1

3
3
1 2 3
3
3 2 1
5
1 4 2 5 3

样例输出 1

3
1
4