This page is still under construction.

Parts of this page are still being built. What you see may change.

Elevator Hall Number

Time limit8sMemory limit512 MB

Summary
Count how many distinct decimal strings can be formed by concatenating one chosen floor from each of N elevators, where each floor is between 1 and 99.
Level

Medium6 of 10

Topics
Brute force, Hash map, Implementation, Combinatorics
Solved
No attempts yet

Problem

JAG (Japanese Alumni Group) is a mysterious organization headquartered in a high-rise building somewhere in Tokyo. The building has NN elevators, and the ii-th elevator stops at every floor from low_ilow\_i to high_ihigh\_i (1≤i≤N1 \le i \le N).

X, a new JAG staff member, reached the elevator hall of the building to visit the headquarters. While waiting for an elevator after pressing the button, X noticed that the display showing the current floor of each elevator is somewhat unusual. When the ii-th elevator is on floor a_ia\_i, the display shows one number obtained by listing a_1,a_2,…,a_Na\_1, a\_2, \ldots, a\_N in this order and concatenating them in decimal notation, without leading zeros and without spaces. For example, when N=3N = 3 and the elevators are on floors 1010, 22, and 1111 in order, the display shows 1021110211.

X became curious about how many different numbers can appear on the display. Your task is to write a program that computes this count.

Input

The input consists of multiple datasets, each in the following format.

NN

low_1low\_1 high_1high\_1

...

low_Nlow\_N high_Nhigh\_N

The first line of a dataset contains the integer NN (2≤N≤62 \le N \le 6), the number of elevators. The ii-th of the following NN lines contains two integers low_ilow\_i and high_ihigh\_i (1≤low_i≤high_i≤991 \le low\_i \le high\_i \le 99), the range of floors served by the ii-th elevator.

The end of the input is indicated by a line containing a single zero.

Output

For each dataset, output on one line the number of different numbers that can appear on the display.

Examples1

  1. Example 1

    Input
    2
    1 11
    1 11
    3
    10 10
    2 2
    11 11
    4
    89 91
    1 12
    1 12
    89 91
    5
    1 8
    2 76
    10 19
    6 16
    33 42
    6
    10 59
    20 69
    30 79
    40 89
    50 99
    50 99
    0
    
    Expected output
    120
    1
    1278
    659520
    15625000000