ExpertAlgorithms50 points

Merge overlapping room bookings

A coworking space stores room bookings as time ranges in minutes since midnight. To print a clean schedule, overlapping or touching bookings must be merged into one block.

Task: the first line holds n. Each of the next n lines has a start and an end (start end, start < end), in any order. Merge ranges that overlap or touch (for example 60-120 and 120-180 become 60-180). Print the merged ranges sorted by start, one per line as start end.

Sample test

Input

4
60 120
100 150
200 240
150 180

Expected output

60 180
200 240

Your answer is also checked against 2 hidden tests.