A Midsummer Night's Dream

Simulate sightings and potion timings to determine who each dosed person first sees, then count mutual pairs.

Medium7SimulationImplementationSortingGeometryNo attempts yetTime limit2sMemory limit512 MB

Problem

In the comedy A Midsummer Night's Dream, Helena loves Demetrius, Demetrius loves Hermia, and Hermia loves Lysander, who loves her back. Hermia's father wants to force her to marry Demetrius, so she and Lysander run away. Demetrius chases them, and Helena chases Demetrius. Once the fairies hear about this mess, they decide to spend part of their own love potion on the humans instead. The potion, applied to the eyelids of a sleeping person, makes that person fall in love with the first person they see after waking up. Puck, the sprite who applies the potion, cannot tell the humans apart yet and puts it on the wrong people.

Count how many real couples come out of Puck's mistakes. A couple is two people who love each other.

For every person you are given the places where the person was seen and when, plus the time the potion was applied. You are also given the distance dd that people can see. From the moment the potion is applied to person vv, vv falls in love with the first person whose distance to vv is at most dd. Someone already inside the range of vv at the exact moment Puck applies the potion counts as a candidate. If several people are inside the range of vv at that same first moment, vv falls in love with the closest one. The input never makes two candidates equally close. Distance is Euclidean, and a distance of exactly dd is inside the range. Nobody falls in love with themselves, and if nobody ever comes inside the range, vv loves nobody.

Input

The first line contains KK (K1K \ge 1), the number of data sets. KK data sets follow, each in this form.

The first line of a data set contains two integers nn and dd. 1n1001 \le n \le 100 is the number of people and 0d1000 \le d \le 100 is the distance people can see.

Then come the descriptions of the nn people, numbered 11 through nn. For person ii, one line contains two integers pip_i and mim_i. 1pi104-1 \le p_i \le 10^4 is the time when the potion was applied to person ii, and pi=1p_i = -1 means the potion was never applied to ii. 0mi1000 \le m_i \le 100 is the number of locations at which ii was seen. The next line contains the three integers xi,jx_{i,j}, yi,jy_{i,j}, ti,jt_{i,j} repeated mim_i times. 1000xi,j,yi,j1000-1000 \le x_{i,j}, y_{i,j} \le 1000 are the coordinates of the jjth sighting of ii, and ti,j104t_{i,j} \le 10^4 is the time of that sighting. For each person the triples are sorted by increasing ti,jt_{i,j}.

Person ii stays at (xi,j,yi,j)(x_{i,j}, y_{i,j}) from time ti,jt_{i,j} until the next sighting at time ti,j+1t_{i,j+1}, or until time is over if there is no next sighting. Before the first sighting, person ii is somewhere far away: nobody sees ii and ii sees nobody. A person with mi=0m_i = 0 stays far away the whole time.

No two potion applications happen at the same time, and no potion application happens at the same time as a sighting. Two different people can be sighted at the same time.

Output

For each data set, output Data Set x: on a line by itself, where xx is the number of the data set, counting from 11. Then, on a line by itself, output the number of couples, that is the number of pairs of people who love each other. Follow each data set with a blank line.