Long ago, Vadim came up with a problem, but could not solve one detail of it. He approached an experienced competitive programmer and learned that this problem already existed. After that, Vadim abandoned his idea and forgot about it... Several years passed. Vadim submitted a solution to this problem... And a few days later he came up with a more difficult version... So, what was I talking about?..
LLC “Logistics Routes” and LLC “Route Logistics” — are two competing companies. They operate in a country with N cities (all cities are numbered from 1 to N), and some pairs of cities are connected by railway tracks; moreover, any city can be reached from any other city by a unique sequence of these tracks.
Each city can enter into and terminate contracts with the companies. If one of the companies serves some set of cities, then its trains travel only between these cities and nowhere else. Let us call all cities through which the trains of a company pass its logistics network. For example, if a company serves two cities, then all cities on the path between them are included in the logistics network. It is quite obvious that if some city enters into or terminates a contract, then the logistics network changes immediately.
Over the course of Q years, many changes occurred, because that was exactly when LLC “Logistics Routes” and LLC “Route Logistics” were founded, and exactly when they began attracting clients. In one year, one of five important events for these companies could occur:
-
A city enters into a contract with LLC “Logistics Routes”. This could happen only if there was no contract between them yet.
-
A city terminates a contract with LLC “Logistics Routes”. This could happen only if there was a contract between them.
-
A city enters into a contract with LLC “Route Logistics”. This could happen only if there was no contract between them yet.
-
A city terminates a contract with LLC “Route Logistics”. This could happen only if there was a contract between them.
-
The contracts remained unchanged. This could happen only if both companies had a contract with at least one city. In this year, the companies assess their competitiveness and calculate the distance between their logistics network and the competitor’s logistics network. This distance is defined as follows: consider all pairs where the first city is in the logistics network of LLC “Logistics Routes” and the second city is in the logistics network of LLC “Route Logistics”; find the number of railway tracks needed to travel from the first city to the second city (this number may be zero); choose the minimum of these numbers.
Next year, a new company, LLC “Logistics Routes and Route Logistics”, wants to enter the market. They hired you as an analyst and want to reconstruct the history of the two competitors. You easily found all information about the contracts, but you will have to calculate the distances yourself.
Input
The first line contains an integer N — the number of cities in the country (2 ≤ N ≤ 105).
The next N−1 lines contain two integers ui and vi — the numbers of the cities connected by the i-th railway track (1 ≤ ui, vi ≤ N, ui ≠ vi). It is guaranteed that any pair of cities is connected by at most one railway track.
The next line contains an integer Q — the number of years (3 ≤ Q ≤ 105). In year zero, the companies had no contracts.
The next Q lines describe events in one of 5 formats:
-
+1 v — city number v entered into a contract with LLC “Logistics Routes”. It is guaranteed that there was no contract between them yet.
-
-1 v — city number v terminated a contract with LLC “Logistics Routes”. It is guaranteed that there was a contract between them.
-
+2 v — city number v entered into a contract with LLC “Route Logistics”. It is guaranteed that there was no contract between them yet.
-
-2 v — city number v terminated a contract with LLC “Route Logistics”. It is guaranteed that there was a contract between them.
-
? — the contracts did not change and the distance between the logistics networks was calculated. It is guaranteed that each company has at least one contract.
For each event, the inequality 1 ≤ v ≤ N holds.
Output
For each year when the contracts did not change, output the distance between the logistics networks of LLC “Logistics Routes” and LLC “Route Logistics”.
Sample
| input | output |
|---|
7
1 2
1 3
2 4
2 5
3 6
3 7
11
+1 4
+2 7
?
+1 5
?
+2 1
?
-1 4
?
-2 1
?
| 4
3
1
2
4
|
Problem Author: Vadim Barinov
Problem Source: The 2026 ICPC Ural Qualification