eolymp
bolt
Try our new interface for solving problems
Problems

Export Estimate

Export Estimate

Luka owns a geographic data company that maintains a detailed city map and exports the data to interested parties. Often, clients do not want the complete map. Instead, they want a simplified map containing only major streets.

City map is an undirected graph consisting of n intersections denoted with integers from 1 to n and m two-way streets. Each street is assigned a priority - a non-negative integer. When requesting a map, the client selects a threshold priority p. The original map is then copied and converted to the exported map using the following procedure:

  1. All streets whose priority is lower than p are deleted.

  2. For each intersection i from 1, 2, . . . , n (processed in that order):

(a) If the intersection i is not connected to any streets it is deleted.

(b) If the intersection i is connected to exactly two different streets x and y leading to intersections a and b both different from i then the intersection i is contracted using the following procedure:

  • Streets x and y are deleted.
  • Intersection i is deleted.
  • New street z connecting intersections a and b is added.

prb7776.gif

Illustration of the second example with the threshold priority 95

Initially, the map does not contain loops (loop is a street that connects an intersection to itself) or parallel edges (more than one street between the same pair of intersections), but the loops and parallel edges may form during the contraction procedure. Notice that, in the step 2. (b) above, neither x nor y can be a loop (both a and b have to be different from i), but the newly added street z could be a loop (it is possible that a and b are same).

Given a map and a sequence of incoming export requests, for each request find the number of intersections and the number of streets in the exported map.

Input

The first line contains two integers n (1n300 000) and m (1m300 000) – the number of intersections and the number of streets, respectively. Each of the following m lines contains three integers a, b and p (1a, bn, 0p300 000) which describe a street with priority p connecting intersections a and b. No street connects an intersection to itself. There is at most one street between every two intersections.

The following line contains an integer q (1q300 000) - the number of export requests. The following line contains q integers. The k-th integer tk (0tk300 000) is the threshold priority of the k-th request.

Output

Output should consist of q lines. The k-th line should contain two integers - the number of intersections and the number of streets, respectively, in the exported map for the k-th request.

Time limit 1 second
Memory limit 128 MiB
Input example #1
6 7
1 2 20
2 3 80
2 5 100
3 5 50
3 4 100
5 6 90
4 6 100
4
25 75 85 95
Output example #1
2 3
1 1
2 1
4 2
Input example #2
10 14
2 7 150
1 2 100
2 3 150
3 1 200
1 4 60
4 5 20
2 5 100
5 6 90
6 7 120
7 5 130
6 8 50
8 9 200
9 10 200
10 7 200
5
300 50 95 100 110
Output example #2
0 0
6 9
4 5
4 5
5 4
Source 2015 ACM Central Europe (CERC), Zagreb, November 13-15, Problem E