Problem C
Terraced fields

Terraced fields with beautiful landscapes in Northwest Vietnam are popular destinations for tourists. At each terraced field selected as a tourist attraction, the local authorities build a staircase alongside the terraced field. The steps are numbered from $1$ to $n$ starting from the bottom of the hill. At steps divisible by $8$ (i.e. steps numbered $8, 16, 24$, etc.) and at the final step (i.e. ${n}^\textrm {th}$ step), the step number is stone engraved as a height indication for tourists. The digits $6$ and $8$ are considered lucky, so people used a precious stone to specifically engrave these digits.

There is a tour that goes to a terraced field having $n$ steps. The price of the tour is the number of precious stone engraved digits on its steps.

For given $n$, your task is to determine the price of the tour.


The input consists of several datasets. The first line of the input contains the number of datasets, which is a positive number and is not greater than $100\, 000$. The following lines describe the datasets.

Each dataset is described by one line containing an integer $n$ $(1<n \leq {10}^{18})$.


For each dataset, output the price of the tour.

Sample Input 1 Sample Output 1

CPU Time limit 1 second
Memory limit 1024 MB
Source The 2016 ACM - ICPC Asia Nha Trang Regional Contest
License Creative Commons License (cc by-sa)

Please log in to submit a solution to this problem

Log in