Under construction

Still being built. Things may change

Highways

Time limit3sMemory limit128 MB

Summary
Given a tree plus extra highway edges, count for each query (x,y) the main tree path plus alternative single-highway paths that touch the main path only at x and y.
Level

Hard9 of 10

Topics
Tree, DFS, Prefix sum, Dynamic programming
Solved
No attempts yet

Problem

Byteland is a small country with nn cities connected by n−1n-1 bidirectional roads. Using only roads, there is exactly one way to travel between any two cities, so the road network forms a tree. Because that single path causes heavy traffic, several highways were built, and each highway directly connects some pair of cities.

A route is a sequence of roads and highways along which every visited city is distinct (a simple path). For every pair of cities xx and yy there is exactly one route that uses no highway at all; we call it the main route between xx and yy (the unique path through the road network).

To travel from city xx to city yy, people may take the main route, or take an alternative route that uses exactly one highway. An alternative route is allowed only if it shares no city with the main route other than the endpoints xx and yy.

For each queried pair of cities, report the number of routes people can take between them (the main route together with every allowed alternative route).

Input

The first line contains a single integer nn (1≤n≤1051 \le n \le 10^5), the number of cities. Cities are numbered from 11 to nn.

Each of the next n−1n-1 lines contains two integers aia_i and bib_i (1≤ai,bi≤n1 \le a_i, b_i \le n), meaning cities aia_i and bib_i are connected by a road.

The next line contains an integer mm (1≤m≤1051 \le m \le 10^5), the number of highways. Each of the next mm lines gives the two cities that one highway connects.

The next line contains an integer qq (1≤q≤5⋅1051 \le q \le 5 \cdot 10^5), the number of queries. Each of the next qq lines contains two integers xx and yy, a query about travel between cities xx and yy. Highways and queries are given in the same two-integer format as the roads.

Output

Print exactly qq lines. The ii-th line must contain a single integer, the number of routes for the ii-th query.

Examples2

  1. Example 1

    Input
    9
    1 2
    2 3
    4 2
    1 5
    5 6
    7 5
    7 8
    9 7
    4
    2 5
    3 4
    6 4
    8 3
    4
    4 9
    2 5
    1 6
    1 7
    
    Expected output
    1
    4
    2
    2
    
  2. Example 2

    Input
    2
    1 2
    1
    1 2
    1
    1 2
    
    Expected output
    2