cf:Codeforces Round 879 (Div. 2) B. Maximum Strength

Fedya is playing a new game called "The Legend of Link", in which one of the character's abilities is to combine two materials into one weapon. Each material has its own strength, which can be represented by a positive integer xx. The strength of the resulting weapon is determined as the sum of the absolute differences of the digits in the decimal representation of the integers at each position.

Formally, let the first material have strength X=x1x2…xnX=x1x2…xn, and the second material have strength Y=y1y2…ynY=y1y2…yn. Then the strength of the weapon is calculated as |x1−y1|+|x2−y2|+…+|xn−yn||x1−y1|+|x2−y2|+…+|xn−yn|. If the integers have different lengths, then the shorter integer is padded with leading zeros.

Fedya has an unlimited supply of materials with all possible strengths from LL to RR, inclusive. Help him find the maximum possible strength of the weapon he can obtain.

An integer C=c1c2…ck¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯C=c1c2…ck¯ is defined as an integer obtained by sequentially writing the digits c1,c2,…,ckc1,c2,…,ck from left to right, i.e. 10k−1⋅c1+10k−2⋅c2+…+ck10k−1⋅c1+10k−2⋅c2+…+ck.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1≤t≤5001≤t≤500). The description of the test cases follows.

The first line of each test case contains two integers LL and RR (1≤L≤R<101001≤L≤R<10100) — the decimal representation of the integers representing the minimum and maximum strength of the materials that Fedya has. It is guaranteed that the integers LL and RR do not contain leading zeros.

Note that the input data may not fit into standard 3232-bit or 6464-bit integer data types.

Output

For each test case print one integer — the maximum possible strength of the weapon that Fedya can obtain from the given materials.

Example

input

Copy

 
  

6

53 57

179 239

13 37

132228 132228

54943329752812629795 55157581939688863366

88 1914

output

Copy

4
19
11
0
163
28

Note

In the first test case, the weapon made from materials with strengths 5353 and 5757 will have the maximum possible strength: |5−5|+|3−7|=4|5−5|+|3−7|=4.

In the second test case, the maximum strength is achieved with materials with strengths 190190 and 209209: |1−2|+|9−0|+|0−9|=19|1−2|+|9−0|+|0−9|=19.

In the fourth test case, there is only one valid strength, so the answer is 00.

In the sixth test case, the maximum strength is achieved with materials with strengths 19091909 and 9090: |1−0|+|9−0|+|0−9|+|9−0|=28|1−0|+|9−0|+|0−9|+|9−0|=28. Note that the shorter integer was padded with leading zeros.

#include 
using namespace std;
string a, b;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t;
    cin >> t;
    int cnt;
    while (t--)
    {
        cnt = 0;
        cin >> a >> b;

        if(a == b)
        {
            cout << "0" << endl;
            continue;
        }


        if(a.size() == b.size())
        {
            int index = 0;
            for(int i = 0;i < a.size(); i++)
            {
                if(a[i] != b[i])
                {
                    index = i;
                    break;
                }

            }
            cnt += (a.size() - index - 1) * 9;
            cnt += b[index] - a[index];
            cout << cnt << endl;
        }
        else if(a.size() != b.size())
        {
            cnt += (b.size() - 1) * 9;

            cnt += b[0] - '0';

            cout << cnt << endl;
        }


    }
    return 0;
}

你可能感兴趣的:(数学建模,算法,matlab)