Jump

Tiles on a grid give energy when visited; jump only right or up at cost B, and maximize the energy left on reaching tile N.

Medium7Dynamic programmingGraphSortingNo attempts yetTime limit2sMemory limit512 MB

Problem

There are NN tiles on a two-dimensional plane. Tile ii sits at (xi,yi)(x_i, y_i) and carries one energy bottle that refills your jumping energy by eie_i.

You jump to the right or up, and nowhere else. A jump from tile (x1,y1)(x_1, y_1) to tile (x2,y2)(x_2, y_2) is allowed when y1=y2y_1 = y_2 and x1<x2x_1 < x_2 (to the right), or when x1=x2x_1 = x_2 and y1<y2y_1 < y_2 (up). The two tiles do not have to be adjacent. Any tile that lies in that direction on the same row or the same column is a legal landing spot. You cannot jump to a place with no tile.

Every jump costs BB energy. You cannot jump while your energy is smaller than BB. The moment you land on a new tile you take its energy bottle and your energy grows by eie_i.

You start on tile 11 with energy e1e_1. Jump from tile 11 to tile NN so that the energy left when you arrive at tile NN is as large as possible.

Write a program that finds that maximum energy.

Input

The first line contains the number of test cases TT. (1T101 \le T \le 10)

The first line of each test case contains two integers NN and BB. (2N3000002 \le N \le 300000, 1B10001 \le B \le 1000)

The next NN lines describe tile 11 through tile NN in order. Each line contains three integers xix_i, yiy_i, eie_i. (0xi,yi1000000 \le x_i, y_i \le 100000, 0ei10000 \le e_i \le 1000)

No two tiles share a coordinate. At least one sequence of jumps from tile 11 to tile NN obeys the energy rule.

Output

For each test case, print on one line the maximum energy you can hold when you arrive at tile NN.