Merge Intervals

MediumIntervals
Next
IntervalsSorting

Given a list of intervals, merge all that overlap and print the resulting disjoint intervals. Intervals that share just an endpoint, like [1, 4] and [4, 5], count as overlapping and are merged.

Input

Line 1: n. Next n lines: the start and end of each interval.

Output

Print the merged intervals one per line as 'start end', sorted by start.

Constraints

1 <= n <= 10^4 0 <= start <= end <= 10^4

Example 1
Input
4
1 3
2 6
8 10
15 18
Output
1 6
8 10
15 18

[1,3] and [2,6] merge into [1,6].

Example 2
Input
2
1 4
4 5
Output
1 5

They touch at 4, so they merge.

Hints

Merge Intervals - Medium Intervals Problem | Skill Shiksha