Under construction

Still being built. Things may change

Lucky Numbers

Time limit1sMemory limit128 MB

Summary
Count numbers in [A,B] up to 10^12 expressible as products of one or more lucky numbers made only of digits 4 and 7, across many queries.
Level

Medium6 of 10

Topics
Math, Number theory, Combinatorics
Solved
No attempts yet

Problem

John recently arrived in Bucharest, Eastern Europe, for a regional contest. John is famous for his theory of lucky numbers, which is exactly why the contestants and spectators are so glad to see him.

According to his theory, among the digits 0–9 the digits 4 and 7 are lucky, and all other digits are not. A lucky number is a number whose decimal representation consists only of lucky digits (4 or 7). A great lucky number is a number that can be written as a product of one or more lucky numbers. A lucky number by itself is also considered a great lucky number (as a product of a single lucky number). For example, 47 (4747), 49 (7×77 \times 7), and 112 (4×7×74 \times 7 \times 7) are all great lucky numbers.

Your task is to count the great lucky numbers that are between AA and BB, inclusive. John gives you AA and BB.

Here, a 'digit' means a single decimal digit (0–9), and a 'number' means a natural number.

Input

The first line contains an integer TT, the number of test cases.

Each of the next TT lines contains two integers AA and BB separated by a space.

  • 1≤T≤77771 \le T \le 7777
  • 1≤A≤B≤10121 \le A \le B \le 10^{12}

Output

For each test case, print the number of great lucky numbers between AA and BB, inclusive, one per line, for a total of TT lines.

Hint

When A=1A = 1 and B=100B = 100, the great lucky numbers are 4, 7, 16 (4×44 \times 4), 28 (4×74 \times 7), 44, 47, 49 (7×77 \times 7), 64 (4×4×44 \times 4 \times 4), 74, and 77.

Examples3

  1. Example 1

    Input
    4
    1 2
    88 99
    112 112
    1 100
    
    Expected output
    0
    0
    1
    10
    
  2. Example 2

    Input
    5
    4 4
    7 7
    16 16
    47 47
    77 77
    
    Expected output
    1
    1
    1
    1
    1
    
  3. Example 3

    Input
    3
    4 7
    4 49
    50 100
    
    Expected output
    2
    7
    3