CF847A Union of Doubly Linked Lists

Description

Doubly linked list is one of the fundamental data structures. A doubly linked list is a sequence of elements, each containing information about the previous and the next elements of the list. In this problem all lists have linear structure. I.e. each element except the first has exactly one previous element, each element except the last has exactly one next element. The list is not closed in a cycle. In this problem you are given $ n $ memory cells forming one or more doubly linked lists. Each cell contains information about element from some list. Memory cells are numbered from $ 1 $ to $ n $ . For each cell $ i $ you are given two values: - $ l_{i} $ — cell containing previous element for the element in the cell $ i $ ; - $ r_{i} $ — cell containing next element for the element in the cell $ i $ . If cell $ i $ contains information about the element which has no previous element then $ l_{i}=0 $ . Similarly, if cell $ i $ contains information about the element which has no next element then $ r_{i}=0 $ . ![](https://cdn.luogu.com.cn/upload/vjudge_pic/CF847A/91459fe713a7cb4d7c970fe35f97e0c90bbcdc07.png)Three lists are shown on the picture.For example, for the picture above the values of $ l $ and $ r $ are the following: $ l_{1}=4 $ , $ r_{1}=7 $ ; $ l_{2}=5 $ , $ r_{2}=0 $ ; $ l_{3}=0 $ , $ r_{3}=0 $ ; $ l_{4}=6 $ , $ r_{4}=1 $ ; $ l_{5}=0 $ , $ r_{5}=2 $ ; $ l_{6}=0 $ , $ r_{6}=4 $ ; $ l_{7}=1 $ , $ r_{7}=0 $ . Your task is to unite all given lists in a single list, joining them to each other in any order. In particular, if the input data already contains a single list, then there is no need to perform any actions. Print the resulting list in the form of values $ l_{i} $ , $ r_{i} $ . Any other action, other than joining the beginning of one list to the end of another, can not be performed.

Input Format

The first line contains a single integer $ n $ ( $ 1

Output Format

Print $ n $ lines, the $ i $ -th line must contain two integers $ l_{i} $ and $ r_{i} $ — the cells of the previous and the next element of list for cell $ i $ after all lists from the input are united in a single list. If there are many solutions print any of them.