This page is still under construction.

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

Torrent

Time limit2sMemory limit128 MB

Summary
Hwiwon collects all n pieces from seeds with fixed online windows at one piece per second and reports the earliest time the file is complete, or -1.
Level

Medium7 of 10

Topics
Graph, Binary search, Intervals
Solved
No attempts yet

Problem

The torrent program Hwiwon uses splits a single file into several pieces and shares it piece by piece. While a seed that holds some of those pieces is online, Hwiwon receives pieces from that seed and puts the file together. A seed hands the pieces it holds to other users during the time it stays online.

Hwiwon wants one file that is split into nn pieces. Each seed comes with its online window and the pieces it holds, and the pieces held by everyone other than Hwiwon never change. Find the minimum time Hwiwon needs to receive every piece of the file.

Seeds hold different pieces and they come online at different times. Receiving one piece takes 1 second, so Hwiwon cannot receive pieces from two seeds during the same second. For example, if a seed arrives at second 0 and leaves at second 3, Hwiwon can receive at most 3 pieces from it. Hwiwon is online from second 0 onward.

All pieces are numbered from 1 to nn.

Input

The first line contains the number of test cases TT.

The first line of each test case contains the number of pieces the file is split into, nn (1≤n≤1001 \le n \le 100), and the number of seeds sharing the pieces, mm (1≤m≤1001 \le m \le 100). Each of the next mm lines describes one seed with the time it comes online t1t_1 (0≤t1≤1000 \le t_1 \le 100), the time it leaves t2t_2 (t1≤t2≤100t_1 \le t_2 \le 100), the number of pieces it holds aa (0≤a≤n0 \le a \le n), and then the aa piece numbers qiq_i (1≤qi≤n1 \le q_i \le n, 1≤i≤a1 \le i \le a).

Output

For each test case, print on one line the minimum time needed to receive the whole file. If no further seed comes online and some piece is still missing, print -1.

Examples1

  1. Example 1

    Input
    2
    3 2
    1 3 1 1
    0 3 3 1 2 3
    5 3
    1 3 2 5 3
    2 10 3 2 3 4
    9 11 1 1
    
    Expected output
    3
    10