Outsourcing

Time limit1sMemory limit128 MB

Summary
Given two directed labeled graphs (factories) with start and final nodes, decide whether the two sets of label sequences realizable as paths from start to final are identical.
Level

Hard9 of 10

Topics
Graph, DFS, String matching, Implementation
Solved
No attempts yet

Problem

Mr. Cooper manufactures science-fiction action figures and believes his local factory is too expensive. Having heard that workers abroad are far cheaper and more dedicated, he wants to move production to a low-wage country (to "outsource" it).

Before switching, he must be sure the new factory can build exactly the same kinds of action figures as the current one. Manufacturing is organized around assembly stations and transfer stations. An assembly station takes parts from a transfer station, performs a single operation on them, and delivers the result to a transfer station. Every factory has one starting transfer station that supplies raw parts and one final transfer station that collects finished figures.

A given kind of action figure requires a specific sequence of operations s1,s2,…,sℓs_1, s_2, \dots, s_\ell. A factory can build that figure if there exist transfer stations t0,t1,…,tℓt_0, t_1, \dots, t_\ell such that t0t_0 is the starting station, tℓt_\ell is the final station, and for every 1≤i≤ℓ1 \le i \le \ell there is an assembly station that takes parts from ti−1t_{i-1}, performs operation sis_i, and delivers to tit_i.

Hence Mr. Cooper wants to know whether the local factory and the foreign factory can produce exactly the same sorts of action figures. He realizes that answering this may be an involved challenge, so he hires you to develop a computer solution. For each pair of factories, decide whether the local factory and the foreign factory can build exactly the same set of action figures.

Input

The first line contains an integer tt, the number of test cases (0<t≤1000 < t \le 100).

Each test case begins with a line of six integers M1 N1 K1 M2 N2 K2M_1\ N_1\ K_1\ M_2\ N_2\ K_2: the local factory has M1M_1 assembly stations, N1N_1 transfer stations, and K1K_1 distinct operations, and the foreign factory has M2M_2 assembly stations, N2N_2 transfer stations, and K2K_2 distinct operations (1≤M1,M2≤1051 \le M_1, M_2 \le 10^5; 1≤N1,N2≤2501 \le N_1, N_2 \le 250; 1≤K1,K2≤2501 \le K_1, K_2 \le 250).

In each factory the transfer stations are numbered 00 to N−1N-1; station 00 is the starting station and station N−1N-1 is the final station. The next M1M_1 lines describe the local factory's assembly stations, one per line, as three integers Tin Tout ST_{in}\ T_{out}\ S: the station takes parts from transfer station TinT_{in}, delivers the result to transfer station ToutT_{out}, and performs operation SS (0≤S≤K1−10 \le S \le K_1 - 1). For any single transfer station, no two of its outgoing assembly stations perform the same operation. The following M2M_2 lines describe the foreign factory's assembly stations in the same format.

Output

For each test case print one line: eligible if the local and foreign factories can build exactly the same set of action figures, or not eligible otherwise.

Examples1

  1. Example 1

    Input
    2
    3 4 2 3 4 3
    0 2 1
    1 3 0
    2 3 0
    0 2 1
    2 1 2
    2 3 0
    3 3 2 2 2 2
    0 1 0
    1 1 0
    1 2 1
    0 0 0
    0 1 1
    
    Expected output
    eligible
    not eligible