Similar Sets
Contest: 2020-2021 ICPC, NERC, Southern and Volga Russian Regional Contest (Online Mirror, ICPC Rules)
Difficulty rating: 2300
Tags: data structures, graphs, implementation

You are given $$$n$$$ sets of integers. The $$$i$$$-th set contains $$$k_i$$$ integers.

Two sets are called similar if they share at least two common elements, i. e. there exist two integers $$$x$$$ and $$$y$$$ such that $$$x \ne y$$$, and they both belong to each of the two sets.

Your task is to find two similar sets among the given ones, or report that there is no such pair of sets.

Input format:
The first line contains a single integer $$$t$$$ ($$$1 \le t \le 50000$$$) — the number of test cases. Then $$$t$$$ test cases follow.

The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^5$$$) the number of given sets. The following $$$n$$$ lines describe the sets. The $$$i$$$-th line starts with an integer $$$k_i$$$ ($$$2 \le k_i \le 10^5$$$) — the number of integers in the $$$i$$$-th set. Then $$$k_i$$$ integers $$$a_{i,1}$$$, $$$a_{i,2}$$$, ..., $$$a_{i,k_i}$$$ ($$$1 \le a_{i,j} \le 10^9$$$) follow — the elements of the $$$i$$$-th set. It is guaranteed that all elements in each set are different.

The total number of elements in all sets in all test cases is not greater than $$$2\cdot 10^5$$$.

Output format:
For each test case, print the answer on a single line.

If there is no pair of similar sets, print -1.

Otherwise, print two different integers — the indices of the similar sets. The sets are numbered from $$$1$$$ to $$$n$$$ in the order they are given in the input. If there are multiple answers, print any of them.

Constraints:
Time limit: 1.0 s
Memory limit: 512.0 MB

Example input:
3
4
2 1 10
3 1 3 5
5 5 4 3 2 1
3 10 20 30
3
4 1 2 3 4
4 2 3 4 5
4 3 4 5 6
2
3 1 3 5
3 4 3 2

Example output:
2 3 
1 2 
-1

Editorial:
Let's define $$$D$$$ = $$$m^{1/2}$$$, where $$$m$$$ is the total amount of integers. Now let's call a set of integers 'large' if its size is at least $$$D$$$ and 'small' otherwise.
Firstly let's check whether there is a large set that has two common integers with some other (small or large) set. Let's try to find a similar set for each large set $$$S$$$ separately. For each integer $$$x$$$ in $$$S$$$ we may set flag $$$w[x]$$$=1. Now for each other set $$$T$$$ with size $$$k$$$ we may calculate amount of integers $$$x$$$ in $$$T$$$ that satisfy condition $$$w[x]$$$=1 (i.e. amount of common integers with $$$S$$$) using $$$O(k)$$$ operations. So, we will use $$$O(m)$$$ operations for each large set $$$S$$$. Since $$$D$$$=$$$m^{1/2}$$$ we will have at most $$$m/D$$$ = $$$D$$$ large sets and will perform $$$O(D*m) = O(m^{3/2})$$$ operations.
Now we have to check whether there are two similar small sets. For each small set $$$S$$$ let's find all pairs of integers $$$(x, y)$$$, $$$x < y$$$ in this set. If there are two equal pairs in different sets, that sets are similar. To find such pairs we may just create a separate array for each integer $$$x$$$ and add all values $$$y$$$ to it. It is possible to check whether there are two equal integers in array in $$$O(k)$$$ operations, where $$$k$$$ is the size of this array. Since the size of each small set is at most $$$D$$$ we will have at most $$$D*k$$$ pairs for each set of size $$$k$$$ and at most $$$D*m$$$ pairs in total. So we will perform $$$O(D*m)$$$ = $$$O(m^{3/2})$$$ operations.