This page is still under construction.

Parts of this page are still being built. What you see may change.

The Bike Trip

Time limit3sMemory limit128 MB

Summary
Starting from place 1, follow staged runs of typed directed roads and list every possible end place.
Level

Medium7 of 10

Topics
Matrix, Graph
Solved
No attempts yet

Problem

The weather is mild and dry, perfect for a bike trip! Hektor grabbed a map of the nearby towns and the roads connecting them (bridges, viaducts, dirt tracks, and so on) and started planning a route, but in an unusual way.

Instead of writing down which places he would visit, Hektor wrote down, in order, the types of road he would ride along. Every road has a type number, and his plan is a list of stages, where each stage means "ride xx roads of type yy in a row".

Because only the road types are fixed, the actual sequence of visited places is not unique, so many different routes may match the plan. Hektor wants to know where his trip could end. The trip always starts at place 11. Following the plan exactly, find every place where the trip could end.

Input

The first line contains the number of test cases ZZ (1≤Z≤51 \le Z \le 5). The descriptions of the ZZ test cases follow.

Each test case begins with a line containing two integers nn and mm (1≤n≤601 \le n \le 60, 1≤m≤40001 \le m \le 4000): the number of places and the number of roads on the map. Each of the next mm lines contains three integers aa, bb, and cc (1≤a,b≤n1 \le a, b \le n, 1≤c≤1001 \le c \le 100), meaning that from place aa you can travel to place bb along a road of type cc. No two roads share the same start place and end place at the same time.

After the map, a line contains one integer dd (1≤d≤1001 \le d \le 100). Each of the next dd lines contains two integers xx and yy (0≤x≤1090 \le x \le 10^9, 1≤y≤1001 \le y \le 100), meaning that in this stage Hektor rides xx roads of type yy.

Output

For each test case, print two lines. The first line contains a single integer: the number of places where Hektor's trip could end. The second line lists those places in increasing order.

Hint

On the first sample map, the plan leaves no choice: Hektor rides through the places 1,2,3,1,2,3,41, 2, 3, 1, 2, 3, 4 in order, so the trip must end at place 44.

On the same map, a plan that rides four roads of type 11 from the start allows two routes, 1,2,3,4,11, 2, 3, 4, 1 and 1,2,3,4,21, 2, 3, 4, 2, so the trip can end at place 11 or place 22.

Examples4

  1. Example 1

    Input
    2
    4 6
    1 2 1
    2 3 1
    3 4 1
    4 1 1
    3 1 2
    4 2 1
    3
    2 1
    1 2
    3 1
    4 6
    1 2 1
    2 3 1
    3 4 1
    4 1 1
    3 1 2
    4 2 1
    1
    4 1
    
    Expected output
    1
    4
    2
    1 2
    
  2. Example 2

    Input
    1
    3 3
    1 2 1
    2 3 1
    3 1 1
    1
    0 1
    
    Expected output
    1
    1
    
  3. Example 3

    Input
    1
    2 2
    1 1 1
    1 2 2
    2
    1000000000 1
    1 2
    
    Expected output
    1
    2
    
  4. Example 4

    Input
    1
    5 5
    1 2 1
    1 3 1
    2 4 1
    3 4 1
    3 5 1
    2
    1 1
    1 1
    
    Expected output
    2
    4 5