Nonsense Time

Time limit12sMemory limit512 MB

Summary
Elements of a random permutation are unfrozen one at a time; after each unfreeze report the longest increasing subsequence length among the currently available elements.
Level

Hard8 of 10

Topics
Dynamic programming, Binary search, Greedy, Probability
Solved
No attempts yet

Problem

You are given a permutation p1,p2,…,pnp_1, p_2, \ldots, p_n of size n. Initially, all elements of p are frozen. Over n stages, the elements become available one by one. On stage i, the element pkip_{k_i} becomes available.

For each i, find the longest increasing subsequence among the available elements after the first i stages.

Input

The first line of the input contains an integer T (1 ≤ T ≤ 3), the number of test cases.

In each test case, the first line contains one integer n (1 ≤ n ≤ 50 000), the size of the permutation.

In the second line of each test case, there are n distinct integers p1,p2,…,pnp_1, p_2, \ldots, p_n (1 ≤ pip_i ≤ n), the permutation.

In the third line of each test case, there are n distinct integers k1,k2,…,knk_1, k_2, \ldots, k_n (1 ≤ kik_i ≤ n), describing each stage.

It is guaranteed that p1,p2,…,pnp_1, p_2, \ldots, p_n and k1,k2,…,knk_1, k_2, \ldots, k_n are generated uniformly at random among all possible permutations of the given size.

Output

For each test case, print a single line containing n integers, where the i-th integer is the length of the longest increasing subsequence among the available elements after the first i stages.

Examples1

  1. Example 1

    Input
    1
    5
    2 5 3 1 4
    1 4 5 3 2
    
    Expected output
    1 1 2 3 3