scmax

Time limit: 0.2s Memory limit: 256MB Input: Output:

You are given an array of integers. Find the longest increasing subsequence. Formally, remove as few values as possible such that the remaining values form a strictly increasing subsequence, and print the remaining numbers.

Just kidding, who needs this in real life? We are interested in something deeper than that.

Each number aa has now a favorite value TaT_a, which even though it can be (according to the normal rules) TaaT_a \leq a, aa is ready to consider that a<Taa < T_a, if this would bring TaT_a immediately to the right of aa in the increasing subsequence we consider. As a consequence, alongside the normal comparison rules, we also introduce the relation a<Taa < T_a, without deleting Ta<aT_a < a.

Task

Find the longest increasing subsequence, given the new conditions.

Formally, an array is now strictly increasing if for each pair of consecutive values (L,R)(L, R), either L<RL < R or TL=RT_L = R.

Input data

The first line contains the number QQ of tests in the file. For each test, the description is similar: On the first line, the number NN of numbers in the array. On the next line there are NN numbers with values between 11 and NN, representing the array. On the next line there are NN numbers with values between 11 and NN, the ithi^{th} of which is TiT_i.

Output data

The first line of each test case will contain the size of the longest maximally increasing substring of the string, considering new rules.

Constraints and clarifications

  • 1Q51 \leq Q \leq 5
  • 1N50 0001 \leq N \leq 50 \ 000
  • For 1010 points, N14N \leq 14
  • For 1010 other points, N100N \leq 100
  • For 1010 other points, Ti=iT_i = i, i\forall i
  • For 1010 other points, all values from the array are 11, 22 or 33.
  • For 1010 other points, for every integer aa there are at most 33 values of bb such that Tb=aT_b = a.

Example

stdin

2
10
4 1 6 1 8 10 10 1 9 1
3 6 3 2 1 9 8 1 5 10
8
8 1 8 6 4 8 5 7
7 3 2 8 8 4 5 6

stdout

5
6

Explanation

In the first test, the maximum sequence is 4 6 8 10 104 \ 6 \ 8 \ 10 \ 10
In the second test, the maximum sequence is 1 8 6 4 5 71 \ 8 \ 6 \ 4 \ 5 \ 7

Log in or sign up to be able to send submissions!