Under construction

Still being built. Things may change

Anti-Plagiarism

Time limit5sMemory limit2048 MB

Summary
For each pair of trees, decide whether the second tree is a subgraph of the first, meaning the first can be obtained by extending the second.
Level

Medium7 of 10

Topics
Tree, Dynamic programming, DFS
Solved
No attempts yet

Problem

As homework, a teacher asked every student in an art class to draw a beautiful and, above all, original tree. After the submissions, she began to suspect some students of cheating. The teacher considers a tree T1T_1 to be copied from a tree T2T_2 if it is possible to add some (possibly zero) vertices and edges to T2T_2 and relabel its vertices so that it becomes the same as T1T_1. She suspects tt pairs of students. For each pair of trees, check whether the first tree could be copied from the second tree.

Input

The first line contains an integer tt (1≤t≤1041 \le t \le 10^4), the number of suspicious pairs. Each description of a pair begins with an integer nn (2≤n≤1052 \le n \le 10^5). The next n−1n-1 lines each contain two integers uu and vv (1≤u,v≤n1 \le u, v \le n), the edges of the first student's tree. The next line contains an integer mm (2≤m≤n2 \le m \le n). The next m−1m-1 lines each contain two integers uu and vv (1≤u,v≤m1 \le u, v \le m), the edges of the second student's tree. The sum of nn over all pairs does not exceed 5⋅1055 \cdot 10^5, and the sum of n⋅mn \cdot m does not exceed 10710^7.

Output

For each pair, print a line containing a single word, "Yes" if the first tree could be copied from the second tree, or "No" otherwise. The word is case-insensitive.

Examples1

  1. Example 1

    Input
    2
    5
    1 2
    1 5
    2 3
    2 4
    4
    1 2
    1 3
    1 4
    6
    1 2
    1 3
    1 4
    5 1
    6 1
    4
    1 2
    2 3
    3 4
    
    Expected output
    Yes
    No