Under construction

Still being built. Things may change

Catching the Joker

Interview

Time limit2sMemory limit1024 MB

Summary
Given a tree and m paths, find the vertex from which the most paths can be walked as one simple route through every vertex on the path.
Level

Medium6 of 10

Topics
Tree, DFS, Prefix sum
Solved
No attempts yet

Problem

The Joker is plotting something again. Batman plans to find him and stop him.

Gotham consists of nn intersections connected by n−1n-1 two-way roads, so that there is exactly one path along the roads between any two intersections.

Batman knows that the Joker will soon travel from his hideout to a secret base. He does not know exactly where these two places are. He has mm guesses, and the ii-th guess is that the Joker travels from intersection aia_i to intersection bib_i.

If Batman stands at intersection xx, he can catch the Joker traveling between aia_i and bib_i if, starting from xx, he can fly over the roads so that he passes through every intersection on the path from aia_i to bib_i without flying over any road twice.

Batman wants to occupy an intersection so that he can catch the Joker on as many of the guessed routes as possible. Count how many routes he can catch the Joker on if he picks the best intersection.

Input

The first line contains the number nn of intersections in Gotham (2≤n≤2⋅1052\le n\le 2\cdot 10^5).

The next n−1n-1 lines describe the roads. Each road is given by two numbers xix_i and yiy_i, the intersections it connects (1≤xi,yi≤n1\le x_i,y_i \le n, xi≠yix_i \neq y_i). It is guaranteed that there is exactly one path between any two intersections.

The next line contains the number mm of guessed routes (1≤m≤2⋅1051\le m\le 2\cdot 10^5).

The next mm lines describe the routes. The ii-th line gives the start aia_i and the end bib_i of the ii-th route (1≤ai,bi≤n1\le a_i,b_i \le n, ai≠bia_i \neq b_i). Routes may overlap or coincide.

Intersections are numbered starting from 1.

Output

Print one integer: the maximum number of guessed routes on which Batman can catch the Joker if he occupies the best intersection.

Examples1

  1. Example 1

    Input
    7
    1 2
    2 3
    3 4
    3 5
    5 6
    5 7
    3
    1 5
    2 4
    6 7
    
    Expected output
    2