问题 I: Far Away from Home

内存限制:512 MB 时间限制:4 S
题面:传统 评测方式:文本比较 上传者:
提交:1 通过:1

题目描述

You have decided to move your house to a new place, by a straight road. There are n stores lying on the road. The i-th store has a distance ��xi to the leftmost of the road.

There are c types of groceries you need to buy. For each type, your cost to buy it is the distance between your house and the nearest store which sells this. Your total cost is the sum of costs of each type.

Note that even you may buy some types of groceries in the same store, you still need to calculate the distance multiple times.

You need to choose a place for your house to minimize the total cost.

输入格式

Each test contains multiple test cases. The first line contains an integer T (1≤�≤51T5) denoting the number of test cases.

For each test case, the first line contains two integers �,�n,c (1≤�≤1051n1051≤�≤5⋅1051c5105).

The next n lines each, contains two integers ��xi and ��ti first (1≤��≤1091xi109��≥1ti1), denoting the coordinate of store i and the number of types of groceries store i sells, and following ��ti distinct integers ��,1,��,2,⋯,��,��ai,1,ai,2,,ai,ti (1≤��,�≤�1ai,jc), denoting the types of groceries store i sells. It is guaranteed that 1,2,⋯,�1,2,,c each appears at least once in all the types the stores sell.

For each test case, it is guaranteed that ∑��≤5⋅105ti5105.

输出格式

For each test case, output one line with an integer, denoting the minimum total cost.

输入样例 复制

1
4 4
1 1 4
5 1 4
9 3 1 3 4
2 2 2 3

输出样例 复制

7