K-equivalence

Time limit1sMemory limit128 MB

Problem

Consider a set $K$ of positive integers.

Let $p$ and $q$ be two non-zero decimal digits. We call them $K$-equivalent when the following holds:

For every $n \in K$, replacing a single digit $p$ by $q$, or a single digit $q$ by $p$, in the decimal representation of $n$ always yields a number that is again an element of $K$.

For example, if $K$ is the set of integers divisible by $3$, then the digits $1$, $4$, and $7$ are $K$-equivalent: replacing a $1$ by a $4$ in the decimal representation of a number never changes its divisibility by $3$.

$K$-equivalence is an equivalence relation on the digits (it is reflexive, symmetric, and transitive).

You are given a finite set $K$ expressed as a union of pairwise-disjoint finite intervals of positive integers. Find the equivalence classes of the digits $1$ through $9$.

Input

The first line contains $n$, the number of intervals whose union forms $K$ ($1 \le n \le 10000$).

Each of the next $n$ lines contains two positive integers $a_i$ and $b_i$ that describe the interval $[a_i, b_i]$ (all integers $x$ with $a_i \le x \le b_i$), where $1 \le a_i \le b_i \le 10^{18}$. Moreover, for every $i$ with $2 \le i \le n$, $a_i \ge b_{i-1} + 2$ (so the intervals are pairwise disjoint and given in increasing order).

Output

Represent each equivalence class as the concatenation of its digits in ascending order.

Print all equivalence classes of the digits $1$ through $9$, one per line, sorted lexicographically.