Under construction

Still being built. Things may change

Hierarchy

Interview

Time limit1sMemory limit1024 MB

Summary
After each edge reversal, report whether the directed tree of superior-subordinate edges is a valid hierarchy with one root.
Level

Easy3 of 10

Topics
Graph, Array, Implementation
Solved
No attempts yet

Problem

Krešimir is studying corporate structures, including hierarchies. He observed the employees of a company and their relationships. This problem only considers superior-subordinate relationships, meaning cases where one employee is directly superior to another employee in the company.

A hierarchy is a structure with NN employees and N−1N - 1 superior-subordinate relationships, where one person is directly or indirectly superior to all employees. The observed company also has NN employees and N−1N - 1 such relationships, but it is not certain whether it is a valid hierarchy.

Krešimir asked you to help answer this question. He recorded all the data in his notebook. He will also make QQ permanent changes by reversing one superior-subordinate relationship, so that the subordinate becomes superior to their former superior. After each change, you must answer the same question: is the current state a valid hierarchy?

Input

The first line contains a positive integer NN (2≤N≤3⋅1052 \le N \le 3 \cdot 10^5).

Each of the next N−1N - 1 lines contains a pair of integers pip_i and eie_i (1≤pi,ei≤N1 \le p_i, e_i \le N, pi≠eip_i \ne e_i), meaning that pip_i is directly superior to eie_i, for i=1,2,…,N−1i = 1, 2, \dots, N - 1.

The next line contains a non-negative integer QQ (0≤Q≤1060 \le Q \le 10^6).

Each of the following QQ lines contains a pair aia_i, bib_i (1≤ai,bi≤N1 \le a_i, b_i \le N, ai≠bia_i \ne b_i). At that moment, aia_i is directly superior to bib_i or bib_i is directly superior to aia_i.

It is guaranteed that at least one hierarchy can be obtained by some sequence of reversals.

Output

Print Q+1Q + 1 lines, one for each state (the initial state and the state after each change). Print DA if the current structure is a hierarchy, and NE otherwise.

Examples2

  1. Example 1

    Input
    3
    1 2
    1 3
    3
    1 2
    1 2
    1 3
    
    Expected output
    DA
    DA
    DA
    DA
    
  2. Example 2

    Input
    4
    2 1
    2 3
    1 4
    4
    4 1
    4 1
    3 2
    1 4
    
    Expected output
    DA
    NE
    DA
    DA
    NE