


The items belonging to the same group must be ordered adjacent to each other.
The item which does not belong to any group can be placed anywhere.
Each item appearing in BEFORE[i] must be placed before the ith item in the sorted list.
The first line contains an integer ‘T’, which denotes the number of test cases to be run. Then, the T test cases follow.
The first line of each test case contains two space-separated integers, ‘N’ and ‘M’, denoting the number of items and the number of groups respectively.
The second line of each test case contains ‘N’ space-separated integers, where every ith integer denotes the group to which the ith item belongs. If an item does not belong to any group, “-1” is used at its place.
Then ‘N’ lines follow, and each of these lines contains the following :
An integer ‘X’ denoting the number of items that should be placed before the ith item. Then X space-separated integers follow. Each integer, ‘Y’ denotes the item number that should be placed before the ith item
For each test case, print ‘N’ space-separated integers, denoting the items in the required order.
If there is no order of items that can match all the requirements, print a single integer “-1”.
Output for each test case will be printed in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N,M <= 10^5
-1 <= GROUP[I] <= M-1
0 <= X <= N-1
0 <= Y <= N-1
Where ‘GROUP[i]’ is the group to which the ith item belongs.
Time Limit: 1 sec
The approach is to use Topological Sorting to find the required sorted order of groups and also the required sorted order of items within each group. We can assume that each item that does not belong to any group is a part of a new group. Then we can use Topological Sort to sort the groups first. Then we can sort the items within each group. While using Topological Sorting, if we find a cycle in the graph, then we can safely say that there can never be an order of items that can match all the requirements and we can simply print “-1”.