This page is still under construction.

Parts of this page are still being built. What you see may change.

Population Migration

Time limit5sMemory limit256 MB

Summary
Residents buy each job from the priciest affordable worker and leave when daily income falls below outside earnings; count who stays once departures stop.
Level

Medium5 of 10

Topics
Simulation, Sorting
Solved
No attempts yet

Problem

When two countries with very different economies merge, moving from the poorer part to the richer one becomes easy. Immigration quotas and border checks disappear, so leaving is only a matter of packing. Every departure changes the situation of the people who stay. There is one neighbour fewer to sell to, and one competitor fewer as well. Here you simulate that process for a single village.

Every resident works in exactly one type of job and asks a fixed price for it. Every resident also has, for each of the mm types of job, the largest amount of money he is willing to pay for having that job done.

A resident who buys job kk goes to the villager who works in job kk and asks the highest price that is still at most the amount the buyer is willing to pay. People want the best work they can afford rather than the cheapest offer. In two cases the resident does the job alone at no cost. First, the amount he is willing to pay for that job is 0, and then he always does it alone. Second, nobody living in the village works in that job at a price he can afford.

A resident may buy a job from himself, and that purchase counts like any other.

The income of a resident is his own price multiplied by the number of residents who buy his job from him. A resident leaves for the richer part on the day his income is strictly less than the money he could earn there. Living costs do not matter, because he would pay them in the richer part too. When several residents decide to leave on the same day, they all leave together, and everyone still in the village recomputes his income the next day from the neighbours who are left. A resident who has left never comes back, even if his old income would now be enough.

Count the residents who are still in the village once the process stops changing.

Input

The first line contains the number of data sets KK, with K≥1K \ge 1. Then KK data sets follow, each in the form below.

The first line of a data set contains nn and mm, with 0≤n≤10000 \le n \le 1000 and 1≤m≤1001 \le m \le 100, where nn is the number of residents and mm the number of job types. Each of the next nn lines describes one resident and holds m+3m + 3 integers wiw_i, jij_i, cic_i, pi,1p_{i,1}, pi,2p_{i,2}, ..., pi,mp_{i,m}. Here wi≥0w_i \ge 0 is the money resident ii could earn in the richer part, 1≤ji≤m1 \le j_i \le m is the type of job he works in, and ci≥0c_i \ge 0 is the price he asks for his work. The value pi,k≥0p_{i,k} \ge 0 is the largest amount resident ii is willing to pay for getting job kk done, and a value of 0 means resident ii always does job kk alone.

No two residents work in the same job at the same price. That is, if i≠i′i \ne i' and ji=ji′j_i = j_{i'}, then ci≠ci′c_i \ne c_{i'}.

Output

For each data set, print Data Set x: on a line of its own, where xx is the number of the data set counting from 1. On the next line print the number of residents still living in the village once the process stops changing. Print a blank line after each data set.

Examples7

  1. Example 1

    Input
    1
    8 3
    20 1 4 0 1 3
    0 1 10 2 4 4
    100 2 10 5 0 20
    10 3 20 0 5 0
    3 2 3 5 0 6
    3 3 3 3 3 3
    1 1 3 0 10 3
    5 2 4 3 0 17
    
    Expected output
    Data Set 1:
    5
    
  2. Example 2

    Input
    3
    0 1
    0 100
    1 1
    0 1 0 0
    
    Expected output
    Data Set 1:
    0
    
    Data Set 2:
    0
    
    Data Set 3:
    1
    
  3. Example 3

    Input
    2
    1 1
    5 1 5 5
    1 1
    6 1 5 5
    
    Expected output
    Data Set 1:
    1
    
    Data Set 2:
    0
    
  4. Example 4

    Input
    1
    2 1
    1 1 1 0
    0 1 2 0
    
    Expected output
    Data Set 1:
    1
    
  5. Example 5

    Input
    1
    3 2
    0 1 100 1 1
    0 2 50 2 2
    0 1 7 3 3
    
    Expected output
    Data Set 1:
    3
    
  6. Example 6

    Input
    1
    5 1
    10 1 1 0
    10 1 2 0
    10 1 3 0
    10 1 4 0
    0 1 5 5
    
    Expected output
    Data Set 1:
    1
    
  7. Example 7

    Input
    1
    10 1
    0 1 1 0
    2 1 2 1
    3 1 3 2
    4 1 4 3
    5 1 5 4
    6 1 6 5
    7 1 7 6
    8 1 8 7
    9 1 9 8
    1 1 10 9
    
    Expected output
    Data Set 1:
    1