Graphic Madness

Time limit1sMemory limit128 MB

Summary
Decide whether a Hamiltonian cycle exists in a combined structure formed by two trees (with socket/processor degree rules) joined by a perfect matching between their sockets.
Level

Hard8 of 10

Topics
Graph, Tree, Math, Combinatorics
Solved
No attempts yet

Problem

In Byteland there are two leading video-card manufacturers, Bitotronics and 3D-Bytes. Each of their top cards is built from many nodes joined by wires that carry the processed signal. There are two kinds of node, sockets and processors, and the wiring of a single card always satisfies:

  • every socket is joined to exactly one processor and to no other socket;
  • every processor is joined to at least two other nodes;
  • between any two nodes there is exactly one path of wires — that is, the wires of one card form a tree.

Bitthew has bought one card of each brand. By coincidence the two cards have the same number of sockets, so he joins every socket of the Bitotronics card to a distinct socket of the 3D-Bytes card with a cable (a one-to-one pairing of the sockets of the two cards).

He now wants to send a signal along a closed route that uses wires and cables, visits every node of both cards exactly once, and returns to its starting node (each two consecutive nodes on the route, and also the first and last, must be joined directly by a wire or a cable). Help Bitthew decide whether such a route exists.

Input

The first line contains the number of test cases TT. Each test case is given as follows.

The first line of a test case contains three integers kk, nn, mm (2≤k≤10002 \le k \le 1000, 1≤n≤10001 \le n \le 1000, 1≤m≤10001 \le m \le 1000): the number of sockets on each card, the number of processors on the Bitotronics card, and the number of processors on the 3D-Bytes card. The nodes are named:

  • Bitotronics sockets: AS1,AS2,…,ASkAS1, AS2, \dots, ASk
  • Bitotronics processors: AP1,AP2,…,APnAP1, AP2, \dots, APn
  • 3D-Bytes sockets: BS1,BS2,…,BSkBS1, BS2, \dots, BSk
  • 3D-Bytes processors: BP1,BP2,…,BPmBP1, BP2, \dots, BPm

The next n+k−1n + k - 1 lines each contain the names of two distinct Bitotronics nodes joined directly by a wire. The following m+k−1m + k - 1 lines describe, in the same format, the wires of the 3D-Bytes card. The last kk lines each contain the names of two sockets on different cards joined by a cable; every socket appears on exactly one such line.

All tokens are separated by whitespace and may be read one after another.

Output

For each test case output a single line containing YES if a closed route with the required property exists, and NO otherwise.

Only this yes/no decision is required — you do not have to print an explicit route.

Examples3

  1. Example 1

    Input
    1
    2 1 11
    AS1 AP1
    AS2 AP1
    BS1 BP1
    BS2 BP11
    BP1 BP2
    BP2 BP3
    BP3 BP4
    BP4 BP5
    BP5 BP6
    BP6 BP7
    BP7 BP8
    BP8 BP9
    BP9 BP10
    BP10 BP11
    AS1 BS2
    BS1 AS2
    
    Expected output
    YES
    
  2. Example 2

    Input
    1
    2 1 1
    AS1 AP1
    AS2 AP1
    BS1 BP1
    BS2 BP1
    AS1 BS1
    AS2 BS2
    
    Expected output
    YES
    
  3. Example 3

    Input
    1
    3 1 1
    AS1 AP1
    AS2 AP1
    AS3 AP1
    BS1 BP1
    BS2 BP1
    BS3 BP1
    AS1 BS1
    AS2 BS2
    AS3 BS3
    
    Expected output
    NO