Under construction

Still being built. Things may change

Strange Draft

Interview

Time limit1sMemory limit1024 MB

Summary
For each test case, find the K consecutive players whose best defensive ability at each of the nine positions sums to the largest total.
Level

Medium5 of 10

Topics
Sliding window, Queue, Array
Solved
No attempts yet

Problem

In 2150, the Kureyo Baseball Organization (hereafter KBO) approved the founding of its 100th team. Until then, players were picked one at a time in zigzag order based on the previous season's standings. But as the number of teams grew, the draft took too long, so KBO announced the following new draft rules.

  • The NN players who applied for the draft are arranged in an arbitrary order that considers factors such as school, tournament results, and physical traits.
  • Each player is described by an integer PiP_i (1≤Pi≤91 \le P_i \le 9), their fixed defensive position, and an integer AiA_i, their defensive ability.
  • When it is a team's turn, it picks KK consecutive players from the list and removes those players from the list.
  • The KK selected players must include at least one player who can play each of the nine defensive positions.

You are the manager of the Twins, who finished last the previous season. To fix the team's chronic defensive weakness, you plan to rebuild around defensive ability next season, so this draft matters a lot. You make the first pick of KK players.

Given the defensive position and defensive ability of each of the NN players, listed in the order of the applicant list, write a program that picks KK players so that the sum of the best defensive ability at each position is maximized. The draft is always possible.

Input

The input consists of TT test cases. The first line contains TT.

The first line of each test case contains the number of applicants NN and the number of players KK that can be picked at once, separated by a space. (9≤K≤N≤100 0009 \le K \le N \le 100\,000) Then NN lines follow, each containing the defensive position PiP_i (1≤Pi≤91 \le P_i \le 9) and defensive ability AiA_i (1≤Ai≤1001 \le A_i \le 100) of each player, separated by a space.

Output

For each test case, print on its own line the maximum possible sum of the best defensive ability at each position that can be obtained in this draft.

Examples1

  1. Example 1

    Input
    2
    10 9
    1 1
    2 1
    3 1
    4 1
    5 1
    6 1
    7 1
    8 1
    9 1
    1 100
    17 12
    1 1
    2 1
    3 1
    4 1
    5 1
    6 1
    7 1
    8 1
    9 1
    8 2
    7 3
    6 4
    5 5
    4 6
    3 7
    2 8
    1 9
    
    Expected output
    108
    45