Similar Subways

Given two trees with up to 50 nodes each, find the largest k such that some connected k-node subtree of the first is isomorphic to some connected k-node subtree of the second.

Hard8TreeDynamic programmingDFSBrute forceNo attempts yetTime limit3sMemory limit512 MB

Problem

Basil often travels to international programming contests held in different cities. After arriving in Byteland he noticed that he had seen a very similar subway map somewhere before. After a moment of thought he understood why: the subway in Byteland looks a lot like the subway in Bytowia. In both cities the subway tunnels form a tree. Every tunnel can be traveled in both directions, and there is exactly one way to get from one station to another using tunnels.

To prove to his friend Peter that the two maps really are similar, Basil wants to find a connected set of kk stations a1,a2,,aka_1, a_2, \ldots, a_k in Byteland and a connected set of kk stations b1,b2,,bkb_1, b_2, \ldots, b_k in Bytowia such that for every ii and jj, Byteland has a tunnel between aia_i and aja_j if and only if Bytowia has a tunnel between bib_i and bjb_j. A set of stations is connected if you can travel from each station of the set to any other station of the set using only stations from that set as intermediate stops.

Help Basil find similar connected sets with the largest number of stations.

Input

The first line contains an integer nn, the number of stations in the Byteland subway (1n501 \le n \le 50).

Each of the next n1n - 1 lines describes one tunnel by the pair of stations it connects, uiu_i and viv_i (1ui,vin1 \le u_i, v_i \le n). It is guaranteed that there is exactly one way to get from any station to any other station.

The next line contains an integer mm, the number of stations in the Bytowia subway (1m501 \le m \le 50).

Each of the next m1m - 1 lines describes one tunnel in Bytowia in the same format.

Output

Print one integer kk, the largest possible size of similar connected sets of stations in Byteland and Bytowia.