eolymp
bolt
Try our new interface for solving problems
Problems

Bracelet Crossings

Bracelet Crossings

Bessie the cow enjoys arts and crafts. In her free time, she has made n bracelets, conveniently numbered 1..n. The i-th bracelet is painted color i out of a set of n different colors. After constructing the bracelets, Bessie placed them on a table for display (which we can think of as the 2D plane). She was careful to arrange the bracelets to satisfy the following three conditions:

  • Every bracelet was a single closed polygonal chain - a series of vertices (points) connected sequentially by line segments, where the first and last points are the same (Feel welcome to consult the wikipedia page for more detail: polygonal chain),
  • No bracelet intersected itself (this corresponds to a "simple" polygonal chain); and
  • No two bracelets intersected.

Unfortunately, right after Bessie arranged the bracelets in such a careful manner, Farmer John drove by in his tractor, shaking the table and causing the bracelets to shift around and possibly break into multiple (not necessarily closed or simple) polygonal chains! Afterward, Bessie wanted to check whether the three conditions above still held. However, it was dark, so she couldn't see the bracelets anymore.

Fortunately, Bessie had a flashlight. She chose m vertical lines x = 1, x = 2, ..., x = m and for each line, she swept the beam of the flashlight along that line from y = −∞ to y = , recording the colors of all bracelets she saw in the order they appeared. Luckily, no beam crossed over any vertex of any polygonal chain or two line segments at the same time. Furthermore, for each beam, every color that appeared appeared exactly twice.

Can you help Bessie use this information to determine whether it is possible that the bracelets still satisfy all three of the conditions above?

Input

The first line of contains the number of test cases t (1t50).

The first line of each test case contains two integers n (1n50) and m (1m50). Each test case then contains m additional lines. For each i from 1 to m, the i-th additional line contains an integer ki (0ki2n, ki even), followed by ki integers ci1, ci2, ..., ciki (cij ∈ [1, n], every cij appears zero or two times). This means that when Bessie swept her flashlight from (i, −∞) to (i, ), she encountered the colors ci1, ci2, ..., ciki in that order.

Output

For each test case, print YES if it is possible for the three conditions above to be satisfied. Otherwise, print NO.

Example

An example of a feasible bracelet configuration for the first case is:

prb11227.gif

For the fourth case, a feasible arrangement is the following:

prb11227_1.gif

Time limit 1 second
Memory limit 128 MiB
Input example #1
5

1 2
2 1 1
2 1 1

1 3
2 1 1
0
2 1 1

2 1
4 1 2 1 2

4 2
6 1 2 2 3 3 1
6 1 2 4 4 2 1

2 2
4 1 1 2 2
4 2 2 1 1
Output example #1
YES
NO
NO
YES
NO
Source 2021 USACO December, Gold