LogiQuest

426. Interval List Intersections

MediumIntervalsTwo Pointers

Two lists of closed intervals are each sorted and pairwise disjoint. Print their intersection, one interval per line, or NONE.

Input format

Line 1: n, then n lines "start end" Then m, then m lines "start end"

Output format

One "start end" per line, or NONE.

Example 1

Input

4
0 2
5 10
13 23
24 25
4
1 5
8 12
15 24
25 26

Output

1 2
5 5
8 10
15 23
24 24
25 25

Example 2

Input

2
1 3
5 9
0

Output

NONE

Constraints

0 ≤ n, m ≤ 1000

Read input from standard input and print the answer to standard output. Your code is judged against 4 test cases, most of them hidden.

Loading the MyLoginn IDE…