This page is still under construction.

Parts of this page are still being built. What you see may change.

Ants

Time limit1sMemory limit512 MB

Summary
Each query moves every ant one step toward the current position of ant a_j, then reports how many pairs of ants share a vertex.
Level

Hard8 of 10

Topics
Tree, DFS, Implementation
Solved
No attempts yet

Problem

You are given a tree with vertices numbered from 11 to nn. There are ants in the vertices of the tree. Initially, at each vertex ii, there is one ant having index ii.

You will be given qq queries. Each query jj contains an index aja_j of an ant that needs help. During query jj, each ant goes to an adjacent vertex that is closest to ant aja_j, or does not move if it is located at the same vertex as ant aja_j. After each query, print the total number of pairs of ants that are located at the same vertex.

The changes persist between queries. For example, when the ants have to move to ant a2a_2, they are already in the positions reached after moving to ant a1a_1. Each query requires moving to ant aja_j, which was at vertex aja_j initially but can be in some other vertex at the time of the query.

Input

The first line of input contains an integer nn, the size of the tree (2≤n≤1052 \le n \le 10^5).

Each of the next n−1n - 1 lines contains two integers uu and vv describing an edge of the tree (1≤u,v≤n1 \le u, v \le n). It is guaranteed that the edges form a tree.

The next line contains an integer qq, the number of queries (1≤q≤1051 \le q \le 10^5).

The next qq lines contain integers a1,a2,…,aqa_1, a_2, \ldots, a_q, one per line: the numbers of ants in the queries (1≤aj≤n1 \le a_j \le n).

Output

For each query, print a single line with the answer to it: the number of pairs of ants that are located at the same vertex after this query.

Examples2

  1. Example 1

    Input
    5
    1 2
    1 3
    2 4
    2 5
    5
    1
    1
    3
    3
    5
    
    Expected output
    4
    10
    10
    10
    10
    
  2. Example 2

    Input
    8
    1 2
    1 3
    2 4
    2 5
    3 6
    3 7
    7 8
    6
    1
    3
    4
    2
    5
    6
    
    Expected output
    5
    21
    28
    28
    28
    28