A - Printing Books (SDUT 2018 Autumn Individual Contest - F)

滴答滴答---题目链接

Nour likes comic books so much that she decided to write her own comic book. Once she had this amazing idea, she started working on it immediately.

After a few days she finished her very first comic book. Since Nour is a workaholic, she wrote a huge comic book during these days with a lot of pages. Since Nour likes to be special in everything she started numbering her comic book from number X. In other words the first page has number X written on it, the second page has number X + 1 written on it, and so on.

Like we all know, Nour enjoys giving her friend Ahmed a hard time. That's why one day on their way to work, she asked her friend Ahmed the following question:

"Say Ahmed, can you guess the number of pages of my new comic book? I used a total number of digits equal to N, and started numbering my comic book from the number X, or say that such a thing is impossible. Since you are only an experimental physicist I'll give you an example: the number 99 has 2 digits on it. So if I wrote only 2 pages starting from the number 99, I would have used 5 digits to number my comic book".

Ahmed is so busy driving, and also he is really holding his nerves not to throw Nour out of the window right now. So he turned to you to help him solve this idiotic question from his annoying friend.

Input

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

Each line of the following T lines describes a single test case. Each test case contains 2 space separated integers N, X (1 ≤ N, X ≤ 1015).

Output

For each test case print a single line, containing a single integer, denoting the number of pages in Nour's comic book, or print -1 if such a thing is impossible.

Example

Input

2
11 5
12 5

Output

8
-1
#include 
#include
#include
#include
using namespace std;
int di(long long n)
{
    int c=0;

    while(n)
    {
        c++;
        n/=10;
    }
    return c;
}
int main()
{
    int t;
    int x;
    long long s,n,k;
    long long a[100];
    a[0]=0;
    for(int i=1; i<=17; i++)
        a[i]=a[i-1]*10+9;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld%lld",&k,&n);
        x=di(n);
        s=0;
        while(1)
        {
            if(k>(a[x]-n+1)*x)
            {
                s+=a[x]-n+1;
                k-=(a[x]-n+1)*x;
                n=a[x]+1;
                x++;
            }
            else
            {
                if(k%x==0)
                {
                    printf("%lld\n",k/x+s);
                }
                else printf("-1\n");
                break;
            }
        }
    }
    return 0;
}

 

你可能感兴趣的:(VJ)