C. Wrong Addition

Problem - C - Codeforces

output

standard output

Tanya is learning how to add numbers, but so far she is not doing it correctly. She is adding two numbers aa and bb using the following algorithm:

  1. If one of the numbers is shorter than the other, Tanya adds leading zeros so that the numbers are the same length.
  2. The numbers are processed from right to left (that is, from the least significant digits to the most significant).
  3. In the first step, she adds the last digit of aa to the last digit of bb and writes their sum in the answer.
  4. At each next step, she performs the same operation on each pair of digits in the same place and writes the result to the left side of the answer.

For example, the numbers a=17236a=17236 and b=3465b=3465 Tanya adds up as follows:

+17236034651106911+17236034651106911

  • calculates the sum of 6+5=116+5=11 and writes 1111 in the answer.
  • calculates the sum of 3+6=93+6=9 and writes the result to the left side of the answer to get 911911.
  • calculates the sum of 2+4=62+4=6 and writes the result to the left side of the answer to get 69116911.
  • calculates the sum of 7+3=107+3=10, and writes the result to the left side of the answer to get 106911106911.
  • calculates the sum of 1+0=11+0=1 and writes the result to the left side of the answer and get 11069111106911.

As a result, she gets 11069111106911.

You are given two positive integers aa and ss. Find the number bb such that by adding aa and bb as described above, Tanya will get ss. Or determine that no suitable bb exists.

Input

The first line of input data contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases.

Each test case consists of a single line containing two positive integers aa and ss (1≤a<s≤10181≤a<s≤1018) separated by a space.

Output

For each test case print the answer on a separate line.

If the solution exists, print a single positive integer bb. The answer must be written without leading zeros. If multiple answers exist, print any of them.

If no suitable number bb exists, output -1.

Example

input

Copy

6
17236 1106911
1 5
108 112
12345 1023412
1 11
1 20

output

Copy

3465
4
-1
90007
10
-1

Note

The first test case is explained in the main part of the statement.

In the third test case, we cannot choose bb that satisfies the problem statement.

#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<deque>
#include<cmath>
#include<string.h>
using namespace std;
// ctrl+shift+C 注释
//ctrl+shift+x 取消
#define int long long
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define fast ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//int k = min_element(a + 1, a + 1 + n) - a
typedef long long ll;
typedef pair<int,int> PII;
const int N=2e5+10;
const ll M=1e18+10;
const int mod=1e9+7;
int aa[N],b[N];
priority_queue<int,vector<int>,greater<int> >pq;
set<int>se;
map<int,char>mp;
queue<int>qu;
vector<int>v;
deque<int>de;
//struct Range
//{
//    int l,r;
//    bool operator< (const Range &w)const
//    {
//        return l<w.l;
//    }
//}range[N];
string s,a;
void solve()
{
    v.clear();
    a.clear();
    s.clear();
    cin>>a>>s;
    if(a.size()>s.size())
    {
        cout<<-1<<endl;
        return;
    }
    for(int i=a.size()-1,j=s.size()-1;j>=0;j--)
    {
        if(j==0&&i>0)//不可能a还有,s没有值了
        {
            cout<<-1<<endl;
            return;
        }
        else if(j&&i<0)//s比a长
        {
            v.push_back(s[j]-'0');
        }
        else if(s[j]>=a[i])//直接减
        {
            v.push_back(s[j]-a[i]);
            //cout<<s[j]<<" "<<a[i]<<endl;
        }
        else if(s[j]<a[i])//一看就是太小了要进位
        {
            j--;
            int res=(s[j]-'0')*10+s[j+1]-'0';
            if(res-(a[i]-'0')>9||res-(a[i]-'0')<0)
            {
                cout<<-1<<endl;
                return;
            }
            v.push_back(res-(a[i]-'0'));
            //cout<<s[j]<<s[j+1]<<"   "<<a[i]<<endl;
        }
        if(j==0&&i>0)//再判断一次
        {
            cout<<-1<<endl;
            return;
        }
        if(i>0)//a太短了,此时a为‘0’
        {
           i--;
        }
        else if(i<=0&&j)
        {
            a[i]='0';
        }
    }
    int f=0;
    reverse(v.begin(),v.end());
    for(int i=0;i<v.size();i++)
    {
         if(v[i])
         {
             f=1;
             cout<<v[i];
         }
         if(v[i]==0&&f)//防止前导零
         {
            cout<<v[i];
         }
    }
    cout<<endl;
}
signed main()
{
   int t=1;
   cin>>t;
   while(t--)
   {
       solve();
   }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值