PCK lives near the Aizu Station and goes to school near the Kitakata Station. His father works at Shiokawa Station, which is located between Aizu and Kitakata Stations.
This morning his father forgot his lunch box, so PCK is planning to take the train from Aizu Station first, then get off at Shiokawa Station, give his lunch box to his father at the station, and then take the train again to Kitakata Station. However, I don't want to miss school if I do that.
You are given the time when PCK must arrive at Kitakata Station, the daily departure schedule of the train that leaves Aizu Station for Siokawa Station, and the daily departure schedule of the train that leaves Siokawa Station for Kitakata Station. Given these timetables, create a program to determine whether PCK can arrive at Kitakata Station by the time he must have arrived there after giving his lunch to his father at Siokawa Station.
Note that the train that departs from Aizu Station and arrives at Siokawa Station and the train that departs from Siokawa Station and arrives at Kitakata Station both take exactly 10 minutes to arrive. Also, it will take 1 minute for PCK to get off at Siokawa Station, hand over his lunch box, and change trains.
Input is given in the following format.
$h$ $m$ $N$ $a_1$ $b_1$ $a_2$ $b_2$ : $a_N$ $b_N$ $M$ $c_1$ $d_1$ $c_2$ $d_2$ : $c_M$ $d_M$
In the first line, you are given two integers $h$ ($5 \leq h \leq 12$) and $m$ ($0 \leq m \leq 59$) that represent the time $h$ hour $m$ minutes that PCK must be at the Kitakata station.
In the next line, the number of trains $N$ ($1 \leq N \leq 20$) departing from Aizu Station and going to Shiokawa Station is given. In the following $N$ lines, the departure times of the trains departing from Aizu Station and going to Shiokawa Station are given in the order of earliest to latest. However, no two trains leave at the same time. The departure time of the $i$th train from Aizu Station consists of two integers $a_i$ ($5 \leq a_i \leq 12$) and $b_i$ ($0 \leq b_i \leq 59$), indicating that the $i$th train will depart at $a_i$:$b_i$.
In the next line, the number of trains leaving Siokawa Station for Kitakata Station, $M$ ($ 1 \leq M \leq 20$), is given. In the following $M$ lines, the departure times of the trains departing from Siokawa Station and going to Kitakata Station are given in order from the earliest to the latest. However, no more than two trains can have the same departure time. The departure time of the $i$th train from Siokawa Station consists of two integers $c_i$ ($5 \leq c_i \leq 12$) and $d_i$ ($0 \leq d_i \leq 59$), indicating that the $i$th train will depart at $c_i$:$d_i$.
Output 1 if PCK can arrive at Kitakata Station by the time he is supposed to, or 0 if he cannot.
8 0 3 5 10 6 10 7 10 2 5 15 6 12
1
8 0 3 7 40 7 45 7 50 3 5 15 6 12 7 50
0