This page is still under construction.

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

Superstitious Skylab Tower

Time limit1sMemory limit128 MB

Summary
Given a valid floor label with no digit 4 and no substring 13, count how many forbidden numbers precede it, then multiply by the floor height.
Level

Medium7 of 10

Topics
Math, Dynamic programming
Solved
No attempts yet

Problem

By the 22nd century, structural engineering has advanced far enough to build orbital towers (space elevators) whose heights reach thousands of kilometers into space.

A team of scientists is planning one such tower as a research station that extends beyond geostationary orbit. Its floors are numbered sequentially starting from 0. Many of the scientists, however, are deeply superstitious: they refuse to use any floor whose number contains the digit 4 anywhere, or contains 13 as a substring. Numbers such as 4, 13, 14, 24, 40, 113, 130, and 413 are therefore all forbidden.

To keep every usable floor in service, the design team simply skips every forbidden number when it labels the floors. The labels run:

0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 15, 16, ...

(4 is skipped; 13 and 14 are skipped; and so on.)

Every floor is the same fixed height hh above the one directly below it, and the ground floor sits at height 0. Hence the floor at physical position pp (counting positions from 0 at ground level) stands at height p×hp \times h. Note that a floor's label and its physical position differ once numbers start being skipped: the physical position of a label is how many valid labels come before it.

Given a floor's displayed label and the per-floor height, compute the exact height of that floor above the ground.

Input

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

Each of the next TT lines contains two integers kk and hh:

  • kk (0<k≤10120 < k \le 10^{12}) — the displayed label of the floor whose height is requested.
  • hh (0<h≤1,000,0000 < h \le 1{,}000{,}000) — the height of each floor.

Every given label kk is guaranteed to be valid: it never contains the digit 4 or the substring 13.

Output

For each test case, print the height of the requested floor on its own line.

Examples1

  1. Example 1

    Input
    5
    1 3
    5 3
    15 3
    6 888
    12 888
    
    Expected output
    3
    12
    36
    4440
    9768