Three Bit Computer Strikes Back
Time limit1sMemory limit128 MB
Given up to 5 functions on states 0..n-1, decide whether some composition of them maps every state to 0.
Problem
Byteland's scientists have moved on from the Three Bit Computer (TBC) to a new and far more powerful design: the Quantum Three Bit Computer (QTBC). On a quantum machine, initializing the memory is a completely different challenge, because every operation you perform has side effects that influence all of the memory at once.
To cope with this, the scientists use large-scale controlled impulses (LSCI). A single impulse acts on every memory bit at the same time and in exactly the same way, so it can be described by a function : when impulse is emitted, every bit currently in state moves to state . For example, means every bit in state becomes state .
The scientists can emit different impulses . You may emit them in any order and repeat them as many times as you like; emitting a sequence of impulses applies the corresponding functions one after another to every bit simultaneously.
Determine whether there exists a sequence of impulses that drives every memory bit to state , no matter which state each bit started in. In other words, decide whether some composition of the given functions sends every state in to .
Input
The first line contains a single integer (), the number of test cases.
Each test case begins with a line containing two integers and (, ), where is the number of possible states of a memory bit and is the number of available impulses.
Each of the next lines describes one impulse: the -th of these lines contains integers , separated by single spaces, where each value lies in .
Output
For each test case, print a single line containing YES if it is possible to bring every memory bit to state , or NO otherwise.