310230: CF1801C. Music Festival

Memory Limit:512 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

C. Music Festivaltime limit per test1 secondmemory limit per test512 megabytesinputstandard inputoutputstandard output The boy Vitya loves to listen to music very much. He knows that $n$ albums are due to be released this Friday, $i$-th of which contains $k_i$ tracks. Of course, Vitya has already listened to all the tracks, and knows that in the $i$-th album, the coolness of the $j$-th track is equal to $a_{i,j}$.

Vitya has a friend Masha, whom he really wants to invite to the festival, where his favorite bands perform. However, in order for a friend to agree, she must first evaluate the released novelties. Vitya knows that if Masha listens to a track that was cooler than all the previous ones, she will get 1 unit of impression. Unfortunately, albums can only be listened to in their entirety, without changing the songs in them in places.

Help Vitya find such an order of albums so that Masha's impression turns out to be as much as possible, and she definitely went to the festival with him.

Input

Each test consists of multiple test cases. The first line contains a single integer t ($1 \le t \le 200\,000$) — the number of test cases. The description of test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 200\,000$) — number of albums.

The album descriptions follow. Each album description consists of two lines:

The first line contains a single integer $k_i$ ($1 \le k_i \le 200\,000$) — the number of tracks in the $i$th album.

The following line contains $k_i$ integers $a_{i, 1},\ a_{i, 2},\ a_{i, 3},\ \ldots,\ a_{i, k_i}$ ($1 \le a_{i,j} \le 200\,000$) — the coolness of the tracks in the $i$ album.

Denote for $\sum k_i$ the sum of all $k_i$. It is guaranteed that $\sum k_i \le 200\,000$.

Output

For each test case print the singular number  — the maximum impression that Masha can get.

Example

Input
2
4
5
4 9 4 6 8
1
7
2
8 6
1
1
4
2
3 4
2
1 8
2
2 8
2
7 9
Output
4
4
Note

In the first test example, the optimal order is listening to the 4th, 2nd, 3rd and 1st albums.

In this case, Masha will listen to the tracks in the following order: 1; 7; 8, 6; 4, 9, 4, 6, 8 and will receive 4 units of impression.

In the second test example, you must first listen to the 1st, then the 4th, and in any order the 2nd and 3rd. In this case, Masha will get the maximum impression, and for every song in the 1st and 4th albums and nothing for the 2nd and 3rd.

Input

题意翻译

- 给你 $n$ 个数列,你要把它们按任意顺序首尾连接起来。 - 最大化连接后能成为严格前缀最大值的数的数量。 - 多组测试数据,$1 \leq n \leq 2 \times 10^5$。

Output

题目大意:
Vitya非常喜欢听音乐。他了解到本周五将有n张专辑发布,其中第i张专辑包含k_i首曲目。当然,Vitya已经听过所有曲目,并且知道第i张专辑中第j首曲目的酷炫程度是a_{i,j}。Vitya想邀请他的朋友Masha去他最喜欢的乐队演出的音乐节,但Masha必须先评价这些新曲目。如果Masha听的曲目比之前所有的都酷,她会得到1单位的印象。不幸的是,专辑只能完整地听,不能更换曲目顺序。帮助Vitya找出一个专辑的顺序,使得Masha的印象尽可能大,这样她就会和他一起去音乐节。

输入输出数据格式:
输入:
- 第一行包含一个整数t(1≤t≤200,000)——测试用例的数量。
- 每个测试用例的第一行包含一个整数n(1≤n≤200,000)——专辑的数量。
- 接下来是专辑的描述。每个专辑描述包含两行:
- 第一行包含一个整数k_i(1≤k_i≤200,000)——第i张专辑的曲目数量。
- 下一行包含k_i个整数a_{i,1}, a_{i,2}, a_{i,3}, ..., a_{i,k_i}(1≤a_{i,j}≤200,000)——第i张专辑中曲目的酷炫程度。
- 所有k_i的和不超过200,000。

输出:
- 对于每个测试用例,输出一个数字——Masha可以得到的最多的印象单位数。

示例:
输入:
```
2
4
5
4 9 4 6 8
1
7
2
8 6
1
1
2
3 4
2
1 8
2
2 8
2
7 9
```
输出:
```
4
4
```
注意:
- 在第一个测试用例中,最佳顺序是先听第4、2、3、1张专辑。这样Masha将按照以下顺序听曲目:1;7;8, 6;4, 9, 4, 6, 8,并将获得4单位的印象。
- 在第二个测试用例中,必须先听第1张,然后是第4张,第2张和第3张可以任意顺序。在这种情况下,Masha将获得最大的印象,对于第1和第4张专辑的每首曲目都有印象,而第2和第3张专辑没有印象。题目大意: Vitya非常喜欢听音乐。他了解到本周五将有n张专辑发布,其中第i张专辑包含k_i首曲目。当然,Vitya已经听过所有曲目,并且知道第i张专辑中第j首曲目的酷炫程度是a_{i,j}。Vitya想邀请他的朋友Masha去他最喜欢的乐队演出的音乐节,但Masha必须先评价这些新曲目。如果Masha听的曲目比之前所有的都酷,她会得到1单位的印象。不幸的是,专辑只能完整地听,不能更换曲目顺序。帮助Vitya找出一个专辑的顺序,使得Masha的印象尽可能大,这样她就会和他一起去音乐节。 输入输出数据格式: 输入: - 第一行包含一个整数t(1≤t≤200,000)——测试用例的数量。 - 每个测试用例的第一行包含一个整数n(1≤n≤200,000)——专辑的数量。 - 接下来是专辑的描述。每个专辑描述包含两行: - 第一行包含一个整数k_i(1≤k_i≤200,000)——第i张专辑的曲目数量。 - 下一行包含k_i个整数a_{i,1}, a_{i,2}, a_{i,3}, ..., a_{i,k_i}(1≤a_{i,j}≤200,000)——第i张专辑中曲目的酷炫程度。 - 所有k_i的和不超过200,000。 输出: - 对于每个测试用例,输出一个数字——Masha可以得到的最多的印象单位数。 示例: 输入: ``` 2 4 5 4 9 4 6 8 1 7 2 8 6 1 1 2 3 4 2 1 8 2 2 8 2 7 9 ``` 输出: ``` 4 4 ``` 注意: - 在第一个测试用例中,最佳顺序是先听第4、2、3、1张专辑。这样Masha将按照以下顺序听曲目:1;7;8, 6;4, 9, 4, 6, 8,并将获得4单位的印象。 - 在第二个测试用例中,必须先听第1张,然后是第4张,第2张和第3张可以任意顺序。在这种情况下,Masha将获得最大的印象,对于第1和第4张专辑的每首曲目都有印象,而第2和第3张专辑没有印象。

加入题单

算法标签: