Under construction

Still being built. Things may change

LIS On Tree

Time limit4sMemory limit1024 MB

Summary
Given a tree where node x_i receives value i in order, report after each update the longest increasing subsequence over any path's nonblank values.
Level

Hard9 of 10

Topics
Tree, Dynamic programming, Segment tree, Binary search
Solved
No attempts yet

Problem

There were too many constructive problems in this contest, so we decided to set a standard data structure problem.

You are given a tree with nn labeled nodes. Each node starts with a blank value.

The longest increasing tree subsequence between two nodes (u,v)(u, v) is computed as follows:

  • Write down all the nonblank values on the path from uu to vv, in order. Compute the longest increasing subsequence (LIS) of the resulting sequence.

†^\dagger A subsequence ai1,ai2,…,aika_{i_1}, a_{i_2}, \dots, a_{i_k} with 1≤i1<i2<⋯<ik≤m1 \le i_1 < i_2 < \cdots < i_k \le m is increasing if ai1<ai2<⋯<aika_{i_1} < a_{i_2} < \cdots < a_{i_k}. A longest increasing subsequence is one with the maximum length among all increasing subsequences.

You are given nn updates x1,x2,…,xnx_1, x_2, \dots, x_n. Update ii writes the value ii at node xix_i. After each update, compute the maximum longest increasing tree subsequence length over all pairs of nodes (u,v)(u, v).

All xix_i are distinct.

Input

The first line contains the number of test cases tt (1≤t≤1041 \le t \le 10^4).

Each test case starts with one integer nn (2≤n≤5⋅1052 \le n \le 5 \cdot 10^5). The next n−1n-1 lines each contain two integers uu and vv (1≤u,v≤n1 \le u, v \le n), an undirected edge between nodes uu and vv. The edges form a tree.

The last line of the test case contains the updates x1,…,xnx_1, \dots, x_n. The sum of nn over all test cases does not exceed 5⋅1055 \cdot 10^5.

Output

For each test case, print nn space-separated integers on a single line. The ii-th integer is the answer after the ii-th update.

Hint

An update tells you the value of node xix_i, not that the value of node ii is xix_i.

Examples1

  1. Example 1

    Input
    4
    5
    1 2
    2 3
    3 4
    4 5
    3 1 5 2 4
    10
    5 1
    3 4
    3 6
    3 7
    8 3
    5 8
    2 5
    9 2
    9 10
    3 6 9 4 5 2 1 8 10 7
    15
    10 1
    3 4
    13 5
    3 7
    8 3
    15 8
    12 13
    9 12
    2 9
    11 2
    11 14
    6 11
    10 6
    10 15
    9 2 10 5 13 14 7 15 11 12 1 6 8 3 4
    2
    1 2
    1 2
    
    Expected output
    1 2 2 2 3
    1 2 2 2 2 3 3 3 4 4
    1 2 3 3 3 3 4 4 4 4 4 4 5 6 7
    1 2