LearnToCP
Sign in
Navigation
HomeRoadmapProblemsAbout Us
Theory
Contest Knowledge
Selecting an IDEInteractive TasksOutput-Only Tasks
Basics
Your First ProgramData types and IOC++ syntaxModuloFunctionsVectorsMatricesTime Complexity
Sorting
SortingCounting sortRadix Sort
Optimization Techniques
Two PointersSum of numbers 1 to nPrefix sumBinary SearchGreedyBinary Search FunctionsBinary Search by AnswerDivide and Conquer
Binary Numbers
Binary NumbersNumbers in codeBitwise OperationsBitmasks
Math
Binary ExponentiationPrime NumbersPrime FactorizationGCD and LCMSieve of EratosthenesModified Sieve
Data Structures
StringsStackQueueMapsSetsPriority QueueCustom Criteria for FunctionsSegment TreesFenwick TreesSparse TablesUnion FindSqrt Decomposition
Combinatorics
Addition PrincipleMultiplication PrincipleCombinatoric ObjectsInclusion Exclusion Principle
Geometry
Geometry BasicsVectorsCross and Dot ProductLinesPolygonsAnglesPoint in PolygonDistances and Intersection PointsConvex HullCircles
Recursion
PointersRecursionGenerating Combinatoric Objects
Dynamic Programming
About DPDP problemsTree DPBitmask DP
Graph Theory
GraphsDFS and BFSShortest PathsTreesTopological SortingDijkstra's AlgorithmMinimum Spanning TreesShortest Path Algorithms
Advanced Graph Theory
BiconnectivityStrongly Connected ComponentsBipartite GraphGraph FlowAugmenting PathsFlow - Minimum Cut DualityHeavy-Light DecompositionCentroid Decomposition
Advanced Data Structures
2D and 3D Segment TreesLazy PropagationImplicit Segment TreesPersistent Segment TreesLowest Common AncestorTrieBalanced Binary Search TreesMo's Algorithm

Bus Routes

MediumProblem #53
Time LimitMemoryInputOutput
1 s64 MBstdinstdout

Joining every pair of stops that share a line is too many pairs. Count how many.

The bus lines of a city are known. Every line is a list of stops, and its buses drive the route in both directions - so once you are on a bus you may get off at any other stop of that same line. One such trip is called a ride, and changing to another bus starts a new ride.

Your task is to find the smallest number of rides needed to travel from a given starting stop to a given final stop.

Input

First line of input will be a single integer t - the number of testcases.
First line of each testcase contains two integers s and n - the number of stops in the city and the number of bus lines. The stops are numbered 1 to s.
In the next n lines will be one bus line each: first the number m of stops on its route, and then m distinct stop numbers.
Last line of the testcase contains two integers a and b - the starting and the final stop.

Output

For every testcase print a single line with the smallest number of rides needed to get from stop a to stop b. If it cannot be done, print −1. If a and b are the same stop the answer is 0, since no ride is needed.

Example

Input
1
7 2
3 1 2 7
3 3 6 7
1 6
Output
2

Board the first bus at stop 1 and stay on it until stop 7, then change to the second bus, which carries you to stop 6. Two rides, and there is no way to do it in one - no single line holds both stop 1 and stop 6.

Constraints

1≤t≤1000
1≤s≤105
1≤n≤105
1≤m
1≤a,b≤s
the stops listed for one bus line are distinct
the sum of s over all testcases does not exceed 2⋅105
the sum of all m over all testcases does not exceed 2⋅105


This problem was adapted, with permission, from Autobuske rute, authored by Društvo matematičara Srbije and Fondacija Petlja.

Submit your solution

Sign in to submit your solution and track your progress.

Sign in to submit