This page is still under construction.

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

It Can Be Arranged

Time limit2sMemory limit128 MB

Summary
Find the fewest rooms for daily courses that each need several parallel rooms, where a room can run course j after course i only if cleaning ends first.
Level

Medium7 of 10

Topics
Graph, Intervals, Math
Solved
No attempts yet

Problem

Every year several universities host national programming contests. Dhaka holds an ICPC regional contest every year, and one or two teams from it go to the ICPC World Finals.

After watching those contests, MMR (Mission Maker Rahman) decided to open a programming school. The school teaches NN courses, and every course meets every day, because a programmer must not forget dynamic programming while learning computational geometry. Course ii starts at time AiA_i and finishes at time BiB_i, and both endpoints count as class time. Course ii has SiS_i registered students, and no student is registered for two courses.

MMR wants to hire rooms in a building called Sentinel Tower. One room holds at most MM students, so course ii occupies ⌈Si/M⌉\lceil S_i / M \rceil rooms at the same time and runs the same class separately in each of them.

Programmers are restless and they leave a mess. Once course ii finishes in a room, cleaning that room before course jj starts there takes cleanij\text{clean}_{ij} time. So course jj can follow course ii directly in the same room only when Bi+cleanij<AjB_i + \text{clean}_{ij} < A_j.

Every course repeats at the same time each day, so you plan a single day. Find the minimum number of rooms MMR has to hire.

Input

The first line has the number of test cases TT. (T≤100T \le 100)

The first line of each test case has the number of courses NN and the capacity of one room MM. (1≤N≤1001 \le N \le 100, 1≤M≤100001 \le M \le 10000)

Each of the next NN lines has the start time AiA_i, the finish time BiB_i, and the number of registered students SiS_i of course ii. (0≤Ai≤Bi≤1070 \le A_i \le B_i \le 10^7, 1≤Si≤100001 \le S_i \le 10000)

Each of the next NN lines has one row of the cleaning time matrix. The jj-th integer of the ii-th row is cleanij\text{clean}_{ij}. (0≤cleanij≤1070 \le \text{clean}_{ij} \le 10^7, cleanii=0\text{clean}_{ii} = 0)

Output

For each test case print one line in the form Case x: y, where xx is the test case number starting from 1 and yy is the minimum number of rooms to hire.

Examples1

  1. Example 1

    Input
    3
    1 5
    1 60 12
    0
    4 1
    1 100 10
    50 130 3
    150 200 15
    80 170 7
    0 2 3 4
    5 0 7 8
    9 10 0 12
    13 14 15 0
    2 1
    1 10 1
    12 20 1
    0 2
    5 0
    
    Expected output
    Case 1: 3
    Case 2: 22
    Case 3: 2