Wire Connection
Time limit1sMemory limit128 MB
Choose the fewest vias so vertical wires from the power line to those vias cross every horizontal wire.
Problem
A digital logic circuit on a printed circuit board (PCB) is a collection of interconnected parts, usually integrated circuit (IC) chips. Each IC chip has several pins that must be connected by wires to pins on other chips or to a power line. Most PCBs are built as a stack of layers. The wiring pattern in each layer consists of horizontal and vertical wires. In a multilayer board, layers are joined by drilled holes called vias, which form conductive paths between layers.
When designing a complex PCB, one special layer is reserved for the power supply. That layer has the following properties.
- A single horizontal power line runs along the bottom of the layer.
- A set of horizontal wires is fixed in advance.
- Several vias are placed on the layer.
- Every horizontal wire must be connected to the power line by vertical wires. Each vertical wire starts at the power line, rises while crossing (and powering) some horizontal wires, and stops at a via.
To keep the design cost effective, we want to minimize the number of vertical wires attached to the power line on this layer.
A vertical wire placed at a via runs from the power line up to that via. It therefore powers exactly the horizontal wires with and .
In figure (a) there are four horizontal wires and seven vias. The minimum number of vertical wires needed to power every horizontal wire is three. Figure (b) shows one layout that uses three vertical wires. Note that a single horizontal wire may be crossed by more than one vertical wire.

(a)

(b)
Given the horizontal wires and the vias, compute the minimum number of vertical wires that connect the power line to all horizontal wires.
Input
The first line contains the number of test cases .
Each test case begins with a line containing two integers and (), where is the number of horizontal wires and is the number of vias.
Each of the next lines describes one horizontal wire with three integers , , , where () is the -coordinate of the wire and , () are the -coordinates of its left and right end points. No two horizontal wires intersect.
Each of the following lines describes one via with two integers , (), which are the - and -coordinates of the via, respectively. At most one via lies on any vertical line, and no via lies on a horizontal wire. The power line lies on the -axis.
Output
For each test case, print a single line containing the minimum number of vertical wires that connect the power line to every horizontal wire. If it is impossible to connect every horizontal wire with at least one vertical wire, print IMPOSSIBLE instead.