Under construction

Still being built. Things may change

Gotta Catch 'Em All

Interview

Time limit2sMemory limit512 MB

Summary
Given N Pokemon points with types, find the smallest axis-parallel square that contains Pokemon of at least K different types.
Level

Medium6 of 10

Topics
Binary search, Sliding window, Two pointers
Solved
No attempts yet

Problem

Ash Ketchum is a Pokemon trainer who wants to catch every Pokemon in the world. He has caught most of them and needs K more different types to complete his set.

Ash lives in a 2D grid world. Pokemons spawn at integer coordinates, and several Pokemons can share the same type. Ash needs K different types, not any K Pokemons.

Ash throws a large square net from the sky. A Pokemon is caught if it lies inside the net or on one of its edges. Each side of the square is parallel to the x-axis or the y-axis. Find the minimum side length of a square that contains Pokemons of K different types. The net must have positive area, so the side must be positive.

Input

The first line contains a single integer T, the number of test cases (1≤T≤1001 \le T \le 100).

Each test case starts with a line containing two integers N and K. N is the number of Pokemons (2≤N≤10002 \le N \le 1000), and K is the number of types (2≤K≤1002 \le K \le 100).

The next N lines each contain three integers X, Y, and Z: the x-coordinate, the y-coordinate, and the type of a Pokemon. −1,00,000,000≤X≤1,00,000,000-1,00,000,000 \le X \le 1,00,000,000, −1,00,000,000≤Y≤1,00,000,000-1,00,000,000 \le Y \le 1,00,000,000, and 1≤Z≤K1 \le Z \le K.

Output

For each test case, print a single line containing the minimum side of a square that contains Pokemons of K different types.

Examples1

  1. Example 1

    Input
    2
    5 2
    0 0 1
    0 1 1
    0 2 1
    2 0 2
    2 1 2
    3 3
    0 0 1
    1 1 2
    2 2 3
    
    Expected output
    2
    2