Under construction

Still being built. Things may change

Milk Candy

Time limit2sMemory limit512 MB

Summary
Given hints that reveal range sums with costs, and NPCs that force exactly k_i purchases each, find the minimum total cost to determine all n values, or report -1.
Level

Hard9 of 10

Topics
Matrix, Combinatorics, Greedy, Bit manipulation
Solved
No attempts yet

Problem

Calabash is playing an RPG on his computer. There are nn unknown numbers x1,x2,…,xnx_1, x_2, \ldots, x_n and mm NPCs who sell hints. The ii-th NPC sells cic_i hints. Each hint has three integers ljl_j, rjr_j, and wjw_j. Buying it costs wjw_j coins and reveals the value of xlj+xlj+1+⋯+xrjx_{l_j} + x_{l_j+1} + \cdots + x_{r_j}.

The goal is to determine all nn unknown numbers. Calabash buys hints optimally, but each NPC is greedy: Calabash must buy exactly kik_i hints from the ii-th NPC. A single hint cannot be bought more than once.

Find the minimum number of coins Calabash must pay to determine all numbers, or determine that it is impossible.

Input

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

Each test case starts with nn and mm (1≤n,m≤801 \le n, m \le 80). Then, for each NPC, there is a line with cic_i and kik_i (1≤ki≤ci1 \le k_i \le c_i), followed by cic_i lines, each containing ljl_j, rjr_j, and wjw_j (1≤lj≤rj≤n1 \le l_j \le r_j \le n, 1≤wj≤1061 \le w_j \le 10^6).

The sum of all cic_i in a test case is at most 80.

Output

For each test case, print the minimum number of coins on a single line. If there is no solution, print −1-1.

Examples1

  1. Example 1

    Input
    2
    2 2
    1 1
    1 2 1
    3 2
    1 1 10
    2 2 100
    1 2 1000
    2 2
    1 1
    1 1 1
    1 1
    1 1 2
    
    Expected output
    111
    -1