#include <bits/stdc++.h>
#define ll long double
#define pb                      push_back
#define MOD                     1000000007
#define endl                    "\n"

using namespace std;
int main() {
		ll t;
	cin >> t;
	while(t--)
	{
	  ll a, b, c, d;
	  cin>>a>>b>>c>>d;
	  if(d>c && b<a)
	 { cout<<-1<<endl;
	  continue;
	 }
	 else
	 {
	     ll e= a-b;
	     if(e<=0)
	     { long long int j= (long long int)b;
	         cout<<j<<endl;
	         continue;
	     }
	     else
	     {
	         ll f= c-d;
	         ll g= ceil(e/f);
	         ll h= b+ g*c;
	       unsigned long long int i = (unsigned long long int) h;
	         cout<<i<<endl;
	         continue;
	     }
	 }
	}
}


#include <bits/stdc++.h>
#define ll long double
#define pb                      push_back
#define MOD                     1000000007
#define endl                    "\n"

using namespace std;
int main() {
		ll t;
	cin >> t;
	while(t--)
	{
	  ll a, b, c, d;
	  cin>>a>>b>>c>>d;
	  if(d>c && b<a)
	 { cout<<-1<<endl;
	  continue;
	 }
	 else
	 {
	     ll e= a-b;
	     if(e<=0)
	     { long long int j= (long long int)b;
	         cout<<j<<endl;
	         continue;
	     }
	     else
	     {
	         ll f= c-d;
	         ll g= ceil(e/f);
	         ll h= a+ g*c;
	        long long int i = (long long int) h;
	         cout<<i<<endl;
	         continue;
	     }
	 }
	}
}


#include <bits/stdc++.h>
#define ll double
#define pb                      push_back
#define MOD                     1000000007
#define endl                    "\n"

using namespace std;
int main() {
		ll t;
	cin >> t;
	while(t--)
	{
	  ll a, b, c, d;
	  cin>>a>>b>>c>>d;
	  if(d>c && b<a)
	 { cout<<-1<<endl;
	  continue;
	 }
	 else
	 {
	     ll e= a-b;
	     if(e<=0)
	     { int j= (int)b;
	         cout<<j<<endl;
	         continue;
	     }
	     else
	     {
	         ll f= c-d;
	         ll g= ceil(e/f);
	         ll h= a+ g*c;
	        int i = (int) h;
	         cout<<i<<endl;
	         continue;
	     }
	 }
	}
}


#include <bits/stdc++.h>
#define ll long long int
#define pb                      push_back
#define MOD                     1000000007
#define endl                    "\n"

using namespace std;
int main() {
		ll t;
	cin >> t;
	while(t--)
	{
	  ll a, b, c, d;
	  cin>>a>>b>>c>>d;
	  if(d>c && b<a)
	 { cout<<-1<<endl;
	  continue;
	 }
	 else
	 {
	     ll e= a-b;
	     if(e<=0)
	     { //long long int j= (long long int)b;
	         cout<<b<<endl;
	         continue;
	     }
	     else
	     {
	         ll f= c-d;
	         ll g= (e/f);
	         if(e%f!=0)
	         g = g+1;
	         ll h= b+ g*c;
	        //long long int i = (long long int) h;
	         cout<<h<<endl;
	         continue;
	     }
	 }
	}
}


#include <iostream>
#define ll long long

using namespace std;

int main(){
    ll a, b, c, d;
    cin>>a>>b>>c>>d;
    if(b >= a)
        cout<<b;
    else if(d >= c)
        cout<<-1;
    else{
        cout<<((a-b)/(c-d) + 1)*c + b);
    }
    cout<<"\n";
    return 0;
}


#include <iostream>
#include <cmath>

#define ll long long

using namespace std;

int main(){
    ll a, b, c, d;
    cin>>a>>b>>c>>d;
    if(b >= a)
        cout<<b;
    else if(d >= c)
        cout<<-1;
    else{
        cout<<(ceil((a-b)/(c-d))*c + b);
    }
    cout<<"\n";
    return 0;
}


#include <iostream>
#include <cmath>

#define ll long long

using namespace std;

int main(){
    int t;
    cin>>t;
    while(t--){
        ll a, b, c, d;
        cin>>a>>b>>c>>d;
        if(b >= a)
            cout<<b;
        else if(d >= c)
            cout<<-1;
        else{
            cout<<(ceil((a-b)/(c-d))*c + b);
        }
        cout<<"\n";
    }
    return 0;
}


#include <iostream>
#include <cmath>

#define ll long long

using namespace std;

int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
    int t;
    cin>>t;
    while(t--){
        ll a, b, c, d;
        cin>>a>>b>>c>>d;
        if(b >= a)
            cout<<b;
        else if(d >= c)
            cout<<-1;
        else{
            ll ans = (a-b)/(c-d);
            ans *= c;
            ans += b;
            if((a-b)%(c-d) != 0){
                ans += c;
            }
            cout<<ans;
        }
        cout<<"\n";
    }
    return 0;
}


#include <iostream>
#define ll long long

using namespace std;

int main(){
    ll a, b, c, d;
    cin>>a>>b>>c>>d;
    if(b >= a)
        cout<<b;
    else if(d >= c)
        cout<<-1;
    else{
        cout<<(((a-b)/(c-d) + 1)*c + b);
    }
    cout<<"\n";
    return 0;
}


#include <iostream>
#define ll long long

using namespace std;

int main(){
    ll a, b, c, d;
    cin>>a>>b>>c>>d;
    if(b >= a)
        cout<<b;
    else if(d >= c)
        cout<<-1;
    else{
        cout<<((a-b)/(c-d) + 1)*d + b);
    }
    cout<<"\n";
    return 0;
}


#include <iostream>
#define ll long long

using namespace std;

int main(){
    ll a, b, c, d;
    cin>>a>>b>>c>>d;
    if(b >= a)
        cout<<b;
    else if(d >= c)
        cout<<-1;
    else{
        cout<<(((a-b)/(c-d) + 1)*d + b);
    }
    cout<<"\n";
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#define debug(x) cout << #x << " :: "<< x <<"\n";
#define debug2(x,y) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\n";
#define debug3(x,y,z) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return c*c;
	else
		return c*c*a;
}

int main()
{
    boost
    ll t,a,b,c,d;
    cin >> t;
    while(t--)  {
        cin >> a >> b >> c >> d;
        ll dif=c-d;
        ll ans=0,cnt=0;
        ans+=b;
        a-=b;
        if( dif<=0 && a>0 )    {
            cout << "-1\n";
            continue;
        }
        if( dif==0 )    {
            cout << ans << "\n";
            continue;
        }
        cnt=( (a/dif) + ((a%dif)>0) );
        ans+=(cnt*c);
        cout << ans << "\n" ;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#define debug(x) cout << #x << " :: "<< x <<"\n";
#define debug2(x,y) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\n";
#define debug3(x,y,z) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return c*c;
	else
		return c*c*a;
}

int main()
{
    boost
    ll t,a,b,c,d;
    cin >> t;
    while(t--)  {
        cin >> a >> b >> c >> d;
        ll dif=c-d;
        ll ans=0,cnt=0;
        ans+=b;
        a-=b;
        if( dif<=0 && a>0 )    {
            cout << "-1\n";
            continue;
        }
        if( dif<=0 || a<=0 )    {
            cout << ans << "\n";
            continue;
        }
        cnt=( (a/dif) + ((a%dif)>0) );
        ans+=(cnt*c);
        cout << ans << "\n" ;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#define debug(x) cout << #x << " :: "<< x <<"\n";
#define debug2(x,y) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\n";
#define debug3(x,y,z) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return c*c;
	else
		return c*c*a;
}

int main()
{
    boost
    ll t,a,b,c,d;
    cin >> t;
    while(t--)  {
        cin >> a >> b >> c >> d;
        ll dif=c-d;
        if( dif<=0 && b<a ) {
            cout << "-1\n";
            continue;
        }
        ll ans=0,cnt=0;
        ans+=b;
        a-=b;
        while( a>0 )    {
            a-=dif;
            cnt++;
        }
        ans+=(cnt*c);
        cout << ans << "\n" ;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#define debug(x) cout << #x << " :: "<< x <<"\n";
#define debug2(x,y) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\n";
#define debug3(x,y,z) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return c*c;
	else
		return c*c*a;
}

int main()
{
    boost
    ll t,a,b,c,d;
    cin >> t;
    while(t--)  {
        cin >> a >> b >> c >> d;
        ll dif=c-d;
        ll ans=0,cnt=0;
        ans+=b;
        a-=b;
        if( dif<=0 && a>0 )    {
            cout << "-1\n";
            continue;
        }
        while( a>0 && dif>0 )    {
            a-=dif;
            cnt++;
        }
        ans+=(cnt*c);
        cout << ans << "\n" ;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#define debug(x) cout << #x << " :: "<< x <<"\n";
#define debug2(x,y) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\n";
#define debug3(x,y,z) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return c*c;
	else
		return c*c*a;
}

int main()
{
    boost
    ll t,a,b,c,d;
    cin >> t;
    while(t--)  {
        cin >> a >> b >> c >> d;
        ll dif=c-d;
        if( dif<=0 && b<a ) {
            cout << "-1\n";
            continue;
        }
        ll ans=0,cnt=0;
        ans+=b;
        a-=b;
        if( dif<=0 && a>0 )    {
            cout << "-1\n";
            continue;
        }
        while( a>0 )    {
            a-=dif;
            cnt++;
        }
        ans+=(cnt*c);
        cout << ans << "\n" ;
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define pb(x) push_back(x);
#define mp(x,y) make_pair(x,y)
#define mem(dp,a) memset(dp,a,sizeof dp);
#define X first
#define Y second
typedef long long int ll;
typedef pair<ll,ll> pp;

#define debug(x) cout << #x << " :: "<< x <<"\n";
#define debug2(x,y) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\n";
#define debug3(x,y,z) cout << #x << " :: "<< x << "\t" << #y << " :: " << y << "\t" << #z << " :: " << z << "\n";
#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set tree< pp , null_type,less< pp >, rb_tree_tag,tree_order_statistics_node_update>

ll power(ll a,ll b)
{
	if(b==0)
		return 1;
	ll c=power(a,b/2);
	if(b%2==0)
		return c*c;
	else
		return c*c*a;
}

int main()
{
    boost
    ll t,a,b,c,d;
    cin >> t;
    while(t--)  {
        cin >> a >> b >> c >> d;
        ll dif=c-d;
        if( dif<=0 && b<a ) {
            cout << "-1\n";
            continue;
        }
        ll ans=0,cnt=0;
        ans+=b;
        a-=b;
        if( dif==0 && a>0 )    {
            cout << "-1\n";
            continue;
        }
        while( a>0 )    {
            a-=dif;
            cnt++;
        }
        ans+=(cnt*c);
        cout << ans << "\n" ;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        if(a<=b)
        {
            cout<<b<<endl;
        }
        else if(a>b and c<=b)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            ll n1=a-b;
            ll n2=c-d;
            if((n1/n2)*n2==n1)
            {
                cout<<(n1/n2)*c+b<<endl;
            }
            else
            {
                cout<<(n1/n2+1)*c+b<<endl;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        if(a<=b)
        {
            cout<<b<<endl;
        }
        else if(c<=d)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            ll n1=a-b;
            ll n2=c-d;
            if((n1/n2)*n2==n1)
            {
                cout<<(n1/n2)*c+b<<endl;
            }
            else
            {
                cout<<(n1/n2+1)*c+b<<endl;
            }
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        if(a<=b)
        {
            cout<<b<<endl;
        }
        else if(c<=b)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            ll n1=a-b;
            ll n2=c-d;
            cout<<((n1+n2-1)/n2)*c+b<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        if(a<=b)
        {
            cout<<b<<endl;
        }
        else if(c<=b)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            ll n1=a-b;
            ll n2=c-d;
            ll ans=b;
            n1=((a-b+n2-1)/n2)*c;
            ans+=n1;
            cout<<ans<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pqb             priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
#define FIO             ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;

int32_t main()
{
	FIO;
	w(t)
	{
		int a, b, c, d; cin >> a >> b >> c >> d;

		if (a <= b)
			cout << b << '\n';

		else if (d >= c)
			cout << -1 << '\n';

		else
		{
			int ans = b;
			int more = a - b;
			int one = c - d;
			more = ((a - b + one - 1) / one) * c;

			ans += more;

			cout << ans << '\n';
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define ll long long int

int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll a,b,c,d;
        cin>>a>>b>>c>>d;
        if(a<=b)
        {
            cout<<b<<endl;
        }
        else if(a>b and c<=b)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            ll n1=a-b;
            ll n2=c-d;
            cout<<((n1+n2-1)/n2)*c+b<<endl;
        }
    }
    return 0;
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
int main()
{
    int t = int(input())
for _ in range(t):
	a, b, c, d = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	a -= b
	dif = c - d
	print(b + (a + dif - 1) // dif * c)
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
using lli = long long int ;
using ld = long double ;
lli tests = 0 ;

void Merge(lli * arrL , lli l , lli * arrR , lli r , lli * arr)
{
    lli i = 0 , j = 0 , k = 0 ;
    while((j < l) && (k < r))
    {
        if(arrL[j] <= arrR[k])
        {
            arr[i] = arrL[j] ;
            j++ ;
        }
        else
        {
            arr[i] = arrR[k] ;
            k++ ;
        }
        i++ ;
    }
    if(j < l)
    {
        for(i ; i < l + r ; i++)
        {
            arr[i] = arrL[j] ;
            j++ ;
        }
    }
    if(k < r)
    {
        for(i ; i < l + r ; i++)
        {
            arr[i] = arrR[k] ;
            k++ ;
        }
    }
}

void mergeSort(lli * arr , lli inp)
{
    if(inp < 2)
        return ;
    lli n = inp/2 ;
    lli arrL[n] , arrR[inp - n] ;
    for(lli i = 0 ; i < n ; i++)
        arrL[i] = arr[i] ;

    for(lli i = n ; i < inp ; i++)
        arrR[i - n] = arr[i] ;

    mergeSort(arrL , n) ;
    mergeSort(arrR , inp - n) ;
    Merge(arrL , n , arrR , inp - n , arr) ;
}

int main()
{
    cin >> tests ;
    for(lli j = 0 ; j < tests ; j++)
    {
        ld a = 0 , b = 0 , c = 0 , d = 0 ;
        cin >> a >> b >> c >> d ;

        if(b >= a)
        {
            cout << b << endl ;
        }
        else
        {
            a -= b ;
            if(c - d <= 0)
            {
                cout << -1 << endl ;
                continue ;
            }
            else
            {
                ld tmp = ceil(a / (c - d)) ;
                c *= tmp ;
                cout << (lli)(c + b) << endl ;
            }
        }
    }
    return 0 ;
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
int main()
{
    int t;
    cin>>t;
    for(int l=0; l<t; l++)
    {
        double a=0, b=0, c=0, d=0, ans=0;
        double x=0;
        cin>>a>>b>>c>>d;
        if(b>=a)
        {
            cout<<b<<endl;
        }
        else if((c-d)<0)
        {
            cout<<-1<<endl;
        }
        else
        {
            x=((a-b)/(c-d));
            x=ceil(x);
            ans= b + x*c;
            cout<<int(ans)<<endl;
        }

    }
    return 0;
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
int main()
{
    int t;
    cin>>t;
    for(int l=0; l<t; l++)
    {
        double a=0, b=0, c=0, d=0, ans=0;
        double x=0;
        cin>>a>>b>>c>>d;
        if(b>=a)
        {
            cout<<b<<endl;
        }
        else if((c-d)<0)
        {
            cout<<-1<<endl;
        }
        else
        {
            x=((a-b)/(c-d));
            x=ceil(x);
            ans= b + x*c;
            cout<<(long long int)ans<<endl;
        }

    }
    return 0;
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
int main()
{
    t = int(input())
for _ in range(t):
	a, b, c, d = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	a -= b
	dif = c - d
	print(b + (a + dif - 1) // dif * c)
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
int main()
{
    int t;
    cin>>t;
    for(int l=0; l<t; l++)
    {
        double a=0, b=0, c=0, d=0;
        int ans=0;
        double x=0;
        cin>>a>>b>>c>>d;
        if(b>=a)
        {
            cout<<b<<endl;
        }
        else if((c-d)<0)
        {
            cout<<-1<<endl;
        }
        else
        {
            x=((a-b)/(c-d));
            x=ceil(x);
            ans= b + x*c;
            cout<<ans<<endl;
        }

    }
    return 0;
}


#include<iostream>
#include<cmath>
#include <bits/stdc++.h>
#include<string>
#include<numeric>
using namespace std ;
using lli = long long int ;
using ld = long double ;
lli tests = 0 ;

void Merge(lli * arrL , lli l , lli * arrR , lli r , lli * arr)
{
    lli i = 0 , j = 0 , k = 0 ;
    while((j < l) && (k < r))
    {
        if(arrL[j] <= arrR[k])
        {
            arr[i] = arrL[j] ;
            j++ ;
        }
        else
        {
            arr[i] = arrR[k] ;
            k++ ;
        }
        i++ ;
    }
    if(j < l)
    {
        for(i ; i < l + r ; i++)
        {
            arr[i] = arrL[j] ;
            j++ ;
        }
    }
    if(k < r)
    {
        for(i ; i < l + r ; i++)
        {
            arr[i] = arrR[k] ;
            k++ ;
        }
    }
}

void mergeSort(lli * arr , lli inp)
{
    if(inp < 2)
        return ;
    lli n = inp/2 ;
    lli arrL[n] , arrR[inp - n] ;
    for(lli i = 0 ; i < n ; i++)
        arrL[i] = arr[i] ;

    for(lli i = n ; i < inp ; i++)
        arrR[i - n] = arr[i] ;

    mergeSort(arrL , n) ;
    mergeSort(arrR , inp - n) ;
    Merge(arrL , n , arrR , inp - n , arr) ;
}

int main()
{
    cin >> tests ;
    for(lli j = 0 ; j < tests ; j++)
    {
        ld a = 0 , b = 0 , c = 0 , d = 0 ;
        cin >> a >> b >> c >> d ;

        if(b >= a)
        {
            cout << b << endl ;
        }
        else
        {
            a -= b ;
            if(c - d <= 0)
            {
                cout << -1 << endl ;
                continue ;
            }
            else
            {
                ld tmp = ceil(a / (c - d)) ;
                c *= tmp ;
                cout << setprecision(50) << (c + b) << endl ;
            }
        }
    }
    return 0 ;
}


t = int(input())
for _ in range(t):
	a, b, c, d = map(int, input().split())
	if b >= a:
		print(b)
		continue
	if c <= d:
		print(-1)
		continue
	a -= b
	dif = c - d
	print(b + (a + dif - 1) // dif * c)


#include <iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	for (int i = 0;i<t;i++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		long long  ts=b,tt=b;
		if (c-d<0 && ts<a){
			cout << -1 << endl;
		}else{
			while (ts<a){
				ts+=c-d;
				tt+=c;
			}
			cout << tt << endl;
		}

	}
}


#include <iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	for (int i = 0;i<t;t++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		long long  ts=b,tt=b;
		if (c-d<0 && ts<a){
			cout << -1 << endl;
		}else{
			while (ts<a){
				ts+=c-d;
				tt+=c;
			}
		}
		cout << tt << endl;
	}
}


#include <iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	for (int i = 0;i<t;i++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		long long  ts=b,tt=b;
		if (c-d<=0 && ts<a){
			cout << -1 << endl;
		} else if(b>=a){
			cout << b << endl;
		}else{
			long long  cr;
			if ((a-b)%(c-d)==0){
				cr = (a-b)/(c-d);
			}else{
				cr = (a-b)/(c-d)+1;
			}
			cout << ts+c*cr << endl;
		}

	}
}


#include <iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	for (int i = 0;i<t;i++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		long long  ts=b,tt=b;
		if (c-d<0 && ts<a){
			cout << -1 << endl;
		}else{
			while (ts<a){
				ts+=c-d;
				tt+=c;
			}
		}
		cout << tt << endl;
	}
}


#include <iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	for (int i = 0;i<t;i++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		long long  ts=b,tt=b;
		if (c-d<0 && ts<a){
			cout << -1 << endl;
		} else if(ts>=a){
			cout << ts << endl;
		}else{
			//while (ts<a){
			//	ts+=c-d;
			//	tt+=c;
			//}
			long long  cr;
			if ((a-b)%(c-d)==0){
				cr = (a-b)/(c-d);
			}else{
				cr = (a-b)/(c-d)+1;
			}
			cout << ts+c*cr << endl;
		}

	}
}


#include <iostream>
using namespace std;
int main(){
	int t;
	cin >> t;
	for (int i = 0;i<t;i++){
		int a,b,c,d;
		cin >> a >> b >> c >> d;
		long long  ts=b,tt=b;
		if (c-d<0 && ts<a){
			cout << -1 << endl;
		} else if(ts==a){
			cout << ts << endl;
		}else{
			//while (ts<a){
			//	ts+=c-d;
			//	tt+=c;
			//}
			long long  cr;
			if ((a-b)%(c-d)==0){
				cr = (a-b)/(c-d);
			}else{
				cr = (a-b)/(c-d)+1;
			}
			cout << ts+c*cr << endl;
		}

	}
}


#include <cinttypes>
#include <iostream>

using namespace std;

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

    int n;
    cin >> n;
    while(n--) {
        int a, b, c, d;
        int64_t cnt = 0;
        cin >> a >> b >> c >> d;
        if(a>b && c<=d) {
            cout << "-1\n";
        }
        else {
            cnt += b;
            a -= b;
            while(a > 0) {
                cnt += c;
                a -= c-d;
            }
            cout << cnt << '\n';
        }
    }
}


#include <cinttypes>
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int n;
    cin >> n;
    while(n--) {
        int64_t a, b, c, d;
        cin >> a >> b >> c >> d;

        if(a <= b) cout << b << '\n';
        else if(c <= d) cout << "-1\n";
        else cout << b + c * (int)ceil((a-b)*1.0/(c-d)) << endl;
    }
}


#include <cinttypes>
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    int n;
    cin >> n;
    while(n--) {
        int64_t a, b, c, d;
        cin >> a >> b >> c >> d;

        if(a > b && c <= d) cout << "-1\n";
        else if(c == d) cout << b << "\n";
        else cout << b + c * (int)ceil((a-b)*1.0/(c-d)) << endl;
    }
}


#include <cinttypes>
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    while(n--) {
        int64_t a, b, c, d;
        cin >> a >> b >> c >> d;

        if(a > b && c <= d) cout << "-1\n";
        else if(a <= b) cout << b << "\n";
        else cout << b + c * (int)ceil((a-b)*1.0/(c-d)) << endl;
    }
}


#include <cinttypes>
#include <iostream>

using namespace std;

int main() {

    int n;
    cin >> n;
    while(n--) {
        int a, b, c, d;
        int64_t cnt = 0;
        cin >> a >> b >> c >> d;

        if(a <= b) cout << b << '\n';
        else if(c <= d) cout << "-1\n";
        else {
            cnt += b; a -= b;
            int64_t times = a/(c-d);
            cnt += c*times;
            a = a%(c-d);
            if(a > 0) cnt += c;
            cout << cnt << '\n';
        }
    }
}


#include <cinttypes>
#include <iostream>

using namespace std;

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

    int n;
    scanf("%d", &n);
    while(n--) {
        int a, b, c, d;
        int64_t cnt = 0;
        cin >> a >> b >> c >> d;
        if(b<a && c<=d) {
            printf("-1\n");
            continue;
        }
        cnt += b;
        a -= b;
        while(a > 0) {
            cnt += c;
            a -= c-d;
        }
        cout << cnt << '\n';
    }
}


#include <cinttypes>
#include <iostream>

using namespace std;

int main() {

    int n;
    cin >> n;
    while(n--) {
        int a, b, c, d;
        int64_t cnt = 0;
        cin >> a >> b >> c >> d;

        if(a>b && c<=d) {
            cout << "-1\n";
        }
        else if(c == d) {
            cout << b << '\n';
        }
        else {
            cnt += b; a -= b;
            int64_t times = a/(c-d);
            cnt += c*times;
            a = a%(c-d);
            if(a > 0) cnt += c;
            cout << cnt << '\n';
        }
    }
}


#include <cinttypes>
#include <iostream>
#include <cmath>

using namespace std;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    while(n--) {
        int64_t a, b, c, d;
        cin >> a >> b >> c >> d;

        if(a > b && c <= d) cout << "-1\n";
        else if(c <= d) cout << b << "\n";
        else cout << b + c * (int)ceil((a-b)*1.0/(c-d)) << endl;
    }
}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0;
		cin>>a>>b>>c>>d;
		if(b>=a) cout<<b<<endl;
		else if(a==b)
		{
                cout<<a<<endl;
		}
		else if(d>=c) cout<<"-1"<<endl;
		else
            {
                cnt=0;
                //sum=b;
            for(long long int i=1;i<10000000;i++)
            {
                 ti=b+i*(c-d);
                 cnt++;
                 //cout<<ti<<endl;
                 if(ti>=a) break;
            }
            sum=b+(cnt*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0;
		cin>>a>>b>>c>>d;
		if(b>a) cout<<b<<endl;
		else if(a==b)
		{
                cout<<a<<endl;
		}
		else if(d>c) cout<<"-1"<<endl;
		else
            {
                cnt=0;
                //sum=b;
            for(int i=1;i<1000000;i++)
            {
                 ti=b+i*(c-d);
                 cnt++;
                 //cout<<ti<<endl;
                 if(ti>=a) break;
            }
            sum=b+(cnt*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0;
		cin>>a>>b>>c>>d;
		if(b>=a) cout<<b<<endl;
		else if(a==b)
		{
                cout<<a<<endl;
		}
		else if(d>=c) cout<<"-1"<<endl;
		else
            {
                cnt=0;
                //sum=b;
            for(long long int i=1;i<1000000;i++)
            {
                 ti=b+i*(c-d);
                 cnt++;
                 //cout<<ti<<endl;
                 if(ti>=a) break;
            }
            sum=b+(cnt*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0,ti=0;
		cin>>a>>b>>c>>d;
		if(b>=a) cout<<b<<endl;
		else if(d>=c) cout<<"-1"<<endl;
		else
            {
                ti=(a-b)/(c-d);
                if(((a-b)%(c-d))) ti++;
            sum=b+(ti*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0,n=100;
		cin>>a>>b>>c>>d;
		if(b>a) cout<<b<<endl;
		else if(d>c) cout<<"-1"<<endl;
		else if(a==b&&b==c)
		{
		    if(c==d&&d==a)
                cout<<a<<endl;
		}
		else
            {
                cnt=0;
                //sum=b;
            for(int i=1;i<100;i++)
            {
                 ti=b+i*(c-d);
                 cnt++;
                 //cout<<ti<<endl;
                 if(ti>=a) break;
            }
            sum=b+(cnt*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0,t=0;
		cin>>a>>b>>c>>d;
		if(b>=a) cout<<b<<endl;
		else if(d>=c) cout<<"-1"<<endl;
		else
            {
                t=(a-b)/(c-d);
                t++;
            sum=b+(t*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0,n=100;
		cin>>a>>b>>c>>d;
		if(b>a) cout<<b<<endl;
		else if(d>c) cout<<"-1"<<endl;
		else if(a==b&&b==c)
		{
		    if(c==d&&d==a)
                cout<<a<<endl;
		}
		else
            {
                cnt=0;
                //sum=b;
            for(int i=1;i<1000000;i++)
            {
                 ti=b+i*(c-d);
                 cnt++;
                 //cout<<ti<<endl;
                 if(ti>=a) break;
            }
            sum=b+(cnt*c);
            cout<<sum<<endl;
		}
	}

}


#include <iostream>
#define LOOP(i,n) for(int i=1;i<n;i++)
using namespace std;

main()
{

	long long int a,b,c,d,t,ti,cnt=0;
	cin>>t;
	while(t--)
	{
	    long long int sum=0;
		cin>>a>>b>>c>>d;
		if(b>=a) cout<<b<<endl;
		else if(a==b)
		{
                cout<<a<<endl;
		}
		else if(d>=c) cout<<"-1"<<endl;
		else
            {
                cnt=0;
                //sum=b;
            for(int i=1;i<1000000;i++)
            {
                 ti=b+i*(c-d);
                 cnt++;
                 //cout<<ti<<endl;
                 if(ti>=a) break;
            }
            sum=b+(cnt*c);
            cout<<sum<<endl;
		}
	}

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;

    while(t--){
       long a , b , c , d;
       cin >> a >> b >> c >> d;
       long count = 0;
       long temp = c-d;

       if(b >= a){
           cout << b << endl;
           continue;
       }
       if(c <= d){
           cout << "-1" << endl;
           continue;
       }
       count = (a-b)/(c-d) + min((long)1, (a-b) % (c-d));
       cout << b+(count)*c << endl;
    }
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;

    while(t--){
       long long a , b , c , d;
       cin >> a >> b >> c >> d;
       long long count = 0;
       long long temp = c-d;

       if(b >= a){
           cout << b << endl;

       }else if(c <= d){
           cout << "-1" << endl;

       }else{
       count = (a-b)/(c-d) + min((long long)1, (a-b) % (c-d));
       cout << b+(count)*c << endl;
       }
    }
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;

    while(t--){
       long long a , b , c , d;
       cin >> a >> b >> c >> d;
       long long count = 0;
       long long temp = c-d;
       if(a == b){
           cout << a << endl;
           continue;
       }
       if(b > a){
           cout << b << endl;
           continue;
       }
       if(c <= d){
           cout << "-1" << endl;
           continue;
       }
       while(temp < a-b){
           temp += c-d;
           count++;
       }
       cout << b+(count+1)*c << endl;;
    }
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;

    while(t--){
       long a , b , c , d;
       cin >> a >> b >> c >> d;
       long count = 0;
       long temp = c-d;

       if(b >= a){
           cout << b << endl;
           continue;a
       }
       if(c <= d){
           cout << "-1" << endl;
           continue;
       }
       count = (a-b)/(c-d) + min((long)1, (a-b) % (c-d));
       cout << b+(count)*c << endl;;
    }
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;

    while(t--){
       long a , b , c , d;
       cin >> a >> b >> c >> d;
       long count = 0;
       long temp = c-d;

       if(b >= a){
           cout << b << endl;
           continue;a
       }
       if(c <= d){
           cout << "-1" << endl;
           continue;
       }
       count = (a-b)/(c-d) + min((long)1, (a-b) % (c-d));
       cout << b+(count)*c << endl;
    }
	return 0;

}


#include <bits/stdc++.h>
using namespace std;

int main() {
    int t;
    cin >> t;

    while(t--){
       long a , b , c , d;
       cin >> a >> b >> c >> d;
       long count = 0;
       long temp = c-d;

       if(b >= a){
           cout << b << endl;

       }else if(c <= d){
           cout << "-1" << endl;

       }else{
       count = (a-b)/(c-d) + min((long)1, (a-b) % (c-d));
       cout << b+(count)*c << endl;
       }
    }
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>=c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      sum==b;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if( a>b && d>=c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if( (b-a)+d>=c )
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>=c)
    cout<<-1<<endl;
  else
  {  ll s2=0;
      a-=b;
      s2=b;
      if(a%((c-d))==0)
      {
          s2+= c*(a/(c-d));
          cout<<s2<<endl;
      }
      else
      {
          ll mm = (a/(c-d))+1;
          s2+= c*mm;
          cout<<s2<<endl;
      }
  }
}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>c || b>c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>c && b>c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      sum=b;
      while(sum<a)
      {   sum+=(c-d);
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>=c)
    cout<<-1<<endl;
  else
  {  ll sum=b,s2=0,cs=0;
      s2 = b;
//      s2+=  c*(a/(c-d));
      while(sum<a)
      {
          sum+= (c-d);
          cs++;
      }
    s2 += cs*c;
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if( (a-b)>=0 && d>=c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t;
cin>>t;
while(t--)
{
  ll a,b,c,d;
  cin>>a>>b>>c>>d;
  if(a<=b)
    cout<<b<<endl;
  else if(d>c && b>c)
    cout<<-1<<endl;
  else
  {  ll sum=0,s2=0;
      a-=b;
      s2 = b;
      while(a>0)
      {
          s2+=c;
          a = a-(c-d);
      }
    cout<<s2<<endl;
  }

}

return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
	ll a,b,c,d;
	cin>>a>>b>>c>>d;
	if(b<a&&c<d)
	cout<<"-1"<<"\n";
	else if(b>=a)
	cout<<b<<"\n";
	else
	{
		cout<<(((a%(c-d))+1)*c)+b<<"\n";
	}
}
}


#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>

using namespace std;

typedef long long ll;

void calc() {
    string tmp;
    for (int i = 0; i < 9; i++) {
        cin >> tmp;
        tmp[i] = tmp[i] == '9' ? '1' : tmp[i] + 1;
        cout << tmp << endl;
    }
}

int main() {
    ll n;
    cin >> n;
    for (ll i = 0; i < n; i++) {
        calc();

    }
    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>

using namespace std;

typedef long long ll;

void calc() {
    string tmp;
    for (int i = 0; i < 9; i++) {
        cin >> tmp;
        tmp[i] = tmp[i] == '9' ? '0' : tmp[i] + 1;
        cout << tmp << endl;
    }
}

int main() {
    ll n;
    cin >> n;
    for (ll i = 0; i < n; i++) {
        calc();

    }
    return 0;
}


#include <iostream>
#include <set>
#include <vector>
#include <map>
#include <string>

using namespace std;

typedef long long ll;

void calc() {
    string tmp;
    for (int i = 0; i < 9; i++) {
        cin >> tmp;
        if (i + 2 % 3 == 0) {
            for (int j = 0; j < 9; j++) {
                if (j + 2 % 3 == 0) {
                    tmp[j] = tmp[j] == '9' ? '1' : tmp[j] + 1;
                }
            }
        }
        cout << tmp << endl;

    }
}

int main() {
    ll n;
    cin >> n;
    for (ll i = 0; i < n; i++) {
        calc();

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 15;
const int M = 1e9+7;
int n,m,k,ok;

char s[maxn][maxn];

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    int T;
    cin>>T;
    while(T--)
    {
        for(int i = 1; i <= 9; i++)
        {
            cin>>s[i];
        }
        for(int i = 1; i <= 9; i++)
        {
            int k = i+1;
            if(i == 9) k = 1;
            for(int j = 0; j < 9; j++)
            {
                if(s[i][j] == i+'0')
                {
                    s[i][j] = k+'0';
                }
            }
        }
        for(int i = 1; i <= 9; i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 10;
const int M = 1e9+7;
int n,m,k,ok;

char s[maxn][maxn];

int a[maxn] = {0,2,3,4,5,6,7,8,9,1};

void solve(int x,int y,int z)
{
    for(int i = x; i <= x+3; i++)
    {
        for(int j = y; j <= y+3; j++)
        {
            if(s[i][j] == '0'+z)
            {
                s[i][j] = '0'+a[z];
            }
        }
    }
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    n = 9;
    int T;
    cin>>T;
    while(T--)
    {
        for(int i = 1; i <= n; i++)
        {
            cin>>s[i];
        }
        int tmp = 0;
        for(int i = 1; i <= 9; i+=3)
        {
            for(int j = 0; j < n; j+=3)
            {
                ++tmp;
                solve(i,j,tmp);
            }
        }
        for(int i = 1; i <= n; i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 20;
const int M = 1e9+7;
int n,m,k,ok;

char s[maxn][maxn];

int a[maxn] = {0,2,3,4,5,6,7,8,9,1};

void solve(int x,int y,int z)
{
    for(int i = x; i <= x+3; i++)
    {
        for(int j = y; j <= y+3; j++)
        {
            if(s[i][j] == '0'+z)
            {
                s[i][j] = '0'+a[z];
            }
        }
    }
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    n = 9;
    int T;
    cin>>T;
    while(T--)
    {
        for(int i = 1; i <= n; i++)
        {
            cin>>s[i];
        }
        int tmp = 0;
        for(int i = 1; i <= 9; i+=3)
        {
            for(int j = 0; j < 9; j+=3)
            {
                ++tmp;
                solve(i,j,tmp);
            }
        }
        for(int i = 1; i <= n; i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 20;
const int M = 1e9+7;
int n,m,k,ok;

char s[maxn][maxn];

int a[maxn] = {0,2,3,4,5,6,7,8,9,1};

void solve(int x,int y,int z)
{
    for(int i = x; i <= x+3; i++)
    {
        for(int j = y; j <= y+3; j++)
        {
            if(s[i][j] == '0'+z)
            {
                s[i][j] = '0'+a[z];
            }
        }
    }
}

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    n = 9;
    int T;
    cin>>T;
    while(T--)
    {
        for(int i = 1; i <= n; i++)
        {
            cin>>s[i];
        }
        for(int i = 1; i <= 9; i+=3)
        {
            for(int j = 0; j < 9; j+=3)
            {
                solve(i,j,9);
            }
        }
        for(int i = 1; i <= n; i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 15;
const int M = 1e9+7;
int n,m,k,ok;

char s[maxn][maxn];

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    int T;
    cin>>T;
    while(T--)
    {
        for(int i = 1; i <= 9; i++)
        {
            cin>>s[i];
        }
        for(int i = 1; i <= 9; i++)
        {
            int k = i^1;
            if(i == 1) k = 9;
            if(i == 9) k = 1;
            for(int j = 0; j < 9; j++)
            {
                if(s[i][j] == i+'0')
                {
                    s[i][j] = k+'0';
                }
            }
        }
        for(int i = 1; i <= n; i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
#define pii pair<int,int>
#define int long long
const int inf = 0x3f3f3f3f;
const int maxn = 15;
const int M = 1e9+7;
int n,m,k,ok;

char s[maxn][maxn];

signed main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("data.in", "r", stdin);
#endif
    int T;
    cin>>T;
    while(T--)
    {
        for(int i = 1; i <= 9; i++)
        {
            cin>>s[i];
        }
        for(int i = 1; i <= 9; i++)
        {
            int k = i^1;
            if(i == 1) k = 9;
            if(i == 9) k = 1;
            for(int j = 0; j < 9; j++)
            {
                if(s[i][j] == i+'0')
                {
                    s[i][j] = k+'0';
                }
            }
        }
        for(int i = 1; i <= 9; i++)
        {
            cout<<s[i]<<endl;
        }
    }
    return 0;
}


//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<fstream>
#include <set>
//freopen(".in","r",stdin);
//freopen(".out","w",stdout);
const int  MAXN =1e5+10;
const int mod = 1e9+7 ;
#define eps 1e-8
#define PI 3.1415926535898
#define lowbit(x) (x&(-x))
#define inf 0x3f3f3f3f3f
const long long INF = 0x7f7f7f7f7f7f7f7f;
using namespace std ;
//set<ll>::iterator it;
typedef long long ll;
void solve()
{
	int a[100][100];
	int i,j;
	for(i=1;i<=9;i++)
	{
		for(j=1;j<=9;j++)
		{
		scanf("%d",&a[i][j]);
		}
	}

	for(i=1;i<=9;i++)
	{
		for(j=1;j<=9;j++)
		{
			printf("%d",a[i][j]);
		}
		printf("\n");
	}
}
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a[9][9];
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cin>>a[i][j];
			}
		}
		bool col[9];
		bool block[3][3];
		for(int i=0;i<9;i++)
		{
		    col[i]=true;
		}
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				block[i][j]=true;
			}
		}

		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				if(col[j]==true && block[i/3][j/3]==true && a[i][j]!=9)
				{
					a[i][j]=a[i][j]+1;
					col[j]=false;
					block[i/3][j/3]=false;
					break;
				}
			}
		}
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a[9][9];
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cin>>a[i][j];
			}
		}
		bool col[9];
		bool block[3][3];
		for(int i=0;i<9;i++)
		{
		    col[i]=true;
		}
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				block[i][j]=true;
			}
		}

		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				if(col[j]==true && block[i/3][j/3]==true )
				{
					a[i][j]=(a[i][j]==9)?8:(a[i][j]+1);
					col[j]=false;
					block[i/3][j/3]=false;
					break;
				}
			}
		}
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a[9][9];
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cin>>a[i][j];
			}
		}
		bool col[9];
		bool block[3][3];
		for(int i=0;i<9;i++)
		{
		    col[i]=true;
		}
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				block[i][j]=true;
			}
		}

		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				if(col[j]==true && block[i/3][j/3]==true)
				{
					a[i][j]=(a[i][j]!=9)?a[i][j]+1:8;
					col[j]=false;
					block[i/3][j/3]=false;
					break;
				}
			}
		}
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		int a[9][9];
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cin>>a[i][j];
			}
		}
		int curr=1;
		for(int i=0;i<9;i++)
		{
			if(a[i][i]==curr)
			{
				a[i][i]=curr+1;
			}
			else
			{
				a[i][i]=curr;
			}
		}
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		char a[9][9];
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cin>>a[i][j];
			}
		}
		bool col[9];
		bool block[3][3];
		for(int i=0;i<9;i++)
		{
		    col[i]=true;
		}
		for(int i=0;i<3;i++)
		{
			for(int j=0;j<3;j++)
			{
				block[i][j]=true;
			}
		}

		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				if(col[j]==true && block[i/3][j/3]==true )
				{
					a[i][j]=(a[i][j]=='9')?'8':(a[i][j]+1);
					col[j]=false;
					block[i/3][j/3]=false;
					break;
				}
			}
		}
		for(int i=0;i<9;i++)
		{
			for(int j=0;j<9;j++)
			{
				cout<<a[i][j];
			}
			cout<<endl;
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;cin>>t;
	while(t--){
		vector<vector<char>> a(9,vector<char>(9));
		for(int i=0;i<9;i++) for(int j=0;j<9;j++) cin>>a[i][j];
		for(int i=0;i<9;i++) for(int j=0;j<9;j++) if(a[i][j]==1) a[i][j] = 2;
		for(int i=0;i<9;i++){
		for(int j=0;j<9;j++) cout<<a[i][j];
		cout<<"\n";
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;cin>>t;
	while(t--){
		vector<vector<char>> a(9,vector<char>(9));
		for(int i=0;i<9;i++) for(int j=0;j<9;j++) cin>>a[i][j];
		for(int i=0;i<8;i++){
		swap(a[i][i],a[i+1][i+1]);
		}
		for(int i=0;i<9;i++){
		for(int j=0;j<9;j++) cout<<a[i][j];
		cout<<"\n";
		}
	}
}


#include<bits/stdc++.h>
using namespace std;

int main(){
	int t;cin>>t;
	while(t--){
		vector<vector<char>> a(9,vector<char>(9));
		for(int i=0;i<9;i++) for(int j=0;j<9;j++) cin>>a[i][j];
		for(int i=0;i<9;i++) for(int j=0;j<9;j++) if(a[i][j]=='1') a[i][j] = '2';
		for(int i=0;i<9;i++){
		for(int j=0;j<9;j++) cout<<a[i][j];
		cout<<"\n";
		}
	}
}


#include<bits/stdc++.h>
#include<math.h>
#include <vector>
using namespace std;
using ll = long long;

int main()
{
    ll t;
    cin>>t;
    vector<ll>vec;
    while(t--)
    {
        ll a,i,j;
        for(i=0; i<9; i++)
        {
            cin>>a;
            if(i==0)
            {
                if(a+10>=1000000000)
                {
                    a=a-10;
                }
                else
                {
                    a=a+10;
                }
            }
           // cout<<a;

            vec.push_back(a);
           // cout<<vec[a]<<endl;
        }

         /*for (auto it = vec.begin(); it != vec.end(); ++it)
        cout <<*it<<endl;;*/

        for(i=0; i<9; i++)
        {
            cout<<vec[i]<<endl;
        }
        vec.clear();
    }
    return 0;
}


#include<bits/stdc++.h>
#define FasterIO     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
    FasterIO
    int t;cin>>t;
    while(t--){
        string rafi[9];
        for(int i=0; i<9; i++)cin>>rafi[i];
        rafi[0][0]=rafi[0][1];rafi[1][3]=rafi[1][4];rafi[2][6]=rafi[2][7];rafi[3][1]=rafi[3][2];rafi[4][4]=rafi[4][5];rafi[5][7]=rafi[5][8];rafi[6][2]=rafi[6][1];rafi[7][5]=rafi[7][4];rafi[8][8]=rafi[8][7];
    for(int i=0; i<9; i++)cout<<rafi[i]<<endl;}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define FasterIO     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int main()
{
    FasterIO
    int t;
    cin>>t;
    while(t--)
    {
        string a[9];
       for(int i=0;i<9;i++)
           cin>>a[i];
       for(int i=0;i<9;i++)
       {
           for(int j=0;j<9;j++)
               if(a[i][j]=='1'){
                   a[i][j]='2';break;}
       }
       for(int i=0;i<9;i++)
       {
           cout<<a[i]<<"\n";
       }
    }
    return 0;
}


#include<bits/stdc++.h>
#define FasterIO     ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
using namespace std;
int main()
{
    FasterIO
    int t;
    cin>>t;
    while(t--)
    {
        string rafi[9];
        for(int i=0; i<9; i++)
        {
            cin>>rafi[i];
        }
        rafi[0][0]=rafi[0][1];
        rafi[1][3]=rafi[1][4];
        rafi[2][6]=rafi[2][7];
        rafi[3][1]=rafi[3][2];
        rafi[4][4]=rafi[4][5];
        rafi[5][7]=rafi[5][8];
        rafi[6][2]=rafi[6][1];
        rafi[7][5]=rafi[7][4];
        rafi[8][8]=rafi[8][7];

        for(int i=0; i<9; i++)
        {
            cout<<rafi[i]<<endl;
        }

    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;cin>>t;
    while(t--){
        string a[9];
       for(int i=0;i<9;i++)
           cin>>a[i];
       for(int i=0;i<9;i++){
           for(int j=0;j<9;j++)
               if(a[i][j]=='1'){
                   a[i][j]='2';break;}}
       for(int i=0;i<9;i++)
           cout<<a[i]<<"\n";}
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a[9];
       for(int i=0;i<9;i++)
           cin>>a[i];
       for(int i=0;i<9;i++)
       {
           for(int j=0;j<9;j++)
               if(a[i][j]=='8')
                   a[i][j]='9';break;
       }
       for(int i=0;i<9;i++)
       {
           cout<<a[i]<<"\n";
       }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a[9];
       for(int i=0;i<9;i++)
           cin>>a[i];
       for(int i=0;i<9;i++)
       {
           for(int j=0;j<9;j++)
               if(a[i][j]=='1'){
                   a[i][j]='2';break;}
       }
       for(int i=0;i<9;i++)
       {
           cout<<a[i]<<"\n";
       }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,i,j;

    cin>>t;

    while(t--)
    {
        string arr[99];

        for(i=0; i<9; i++)
        {
            cin>>a[i];
        }
        for(i=0; i<9; i++)
        {
            j=2*(i%2)+i/2;
            if(arr[i][j]<'9')
            {
                arr[i][j]++;
            }
            else
            {
                arr[i][j]--;
            }
        }
        for(i=0; i<9; i++)
        {
            cout<<arr[i]<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
#include <vector>
using namespace std;
using ll = long long;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll n,i,len=0;
        cin>>n;
        ll high=0;
        vector<ll>roy;
        ll a[n+20];

        set<ll>rafi;
        for(i=0; i<n; i++)
        {
            cin>>a[i];

            bool practice=false;
            if(rafi.count(a[i]))
            {
                practice = true;
            }
            if(practice == false)
            {
                roy.push_back(a[i]);
            }
            rafi.insert(a[i]);
            /*if(practice==true)
            {
                nc++;
            }*/

        }
        if(n==1)
        {
            cout<<0<<endl;
        }
        else
        {
            len=roy.size();
            /*for(i=0; i<len; i++)
            {
                cout<<roy[i]<<endl;
            }*/

            vector<bool> visited(n, false);

            // Traverse through array elements and
            // count frequencies
            for (i = 0; i < n; i++)
            {

                // Skip this element if already processed
                if (visited[i] == true)
                    continue;

                // Count frequency
                ll count = 1;
                for (int j = i + 1; j < n; j++)
                {
                    if (a[i] == a[j])
                    {
                        visited[j] = true;
                        count++;
                    }
                }
                if(count>high)
                {
                    high=count;
                }
            }
            if(high==0)
            {
                cout<<1<<endl;
            }
            else
            {
                if(n%2==0)
                {
                    if(high>=len)
                    {
                        cout<<len<<endl;
                    }
                    else
                    {
                        cout<<high<<endl;
                    }
                }
                else
                {
                    if(high>len)
                    {
                        cout<<len<<endl;
                    }
                    else if(len>high)
                    {
                        cout<<high<<endl;
                    }
                    else
                    {
                        cout<<len-1<<endl;
                    }
                }

            }
        }    //  cout<<high<<endl;

    }
    return 0;
}


///***###                               بسم الله الرحمن الرحيم                            ###***///

/*
                    Author          : Borhan Uddin Rafi
                    University      : Begum Rokeya University,Rangpur.
                    Date            : 07 August,2020;
*/

///************************Start Of Template*************************///

#include<iostream>
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#define Read_Fast    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define PI acos(-1)
#define all(a) a.begin(),a.end()
#define f first
#define s second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define f0(i,n) for(i=0;i<n;i++)
#define f1(i,n) for(i=1;i<n;i++)
#define f1n(i,n) for(i=1;i<=n;i++)
#define szf sizeof()
#define sz size()
#define ln length()
#define mem(x,r) memset(x,r,sizeof(x))
#define inf 1000000000000
using namespace std;
using ll=long long;
const int N=1e3+4;

map<string,string>maap;

//////graph////////
vector<int>circle;
bool vis[N][N];
int parent[N];
int chk[N];
int color[N];
int tim[2][N];
int dis[N];
int position[N];
vector<int>adj[N];
vector<int>adj1[N];
//vector<pair<ll,ll>>adj[N];
vector<int>graph[N];
bool has_cycle;
int maxdis,maxnode,Totnode,depth=1;
bool ok;
queue<int>q;
stack<int>stk;
vector<int>solution;
int indegree[N];
int go[N];
int to[N];
map<int,int>mp1;

//////graph////////

/*** this solution will not work if freopen  terminal are open................
    If you close that freopen then its work here.
    But before submission you need to open freopen line when submit .text file***/

////calculate the path //////

//////////main function////////

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    Read_Fast;
    int t;
    cin>>t;
    int cas=0;
    while(t--)
    {
        ll n,m,i,j,cnt1=0,cnt2=0,cnt=0,even=0,odd=0,len,k,r,l,z=0,x=0,y=0,flag=0,sum=0;
        ll a=0,b=0,c=0,d=0,ans=0,rem,quot,zero=0,fst=0,null=0,snd=0,lst=0,rone=0,one=0,pos=0,neg=0,mn=INT_MAX,mx=INT_MIN;

        char ch;

        ////Time/////
        int h1,h2,m1,m2,h;

        ////motion ////
        int velo1,velo2,ac1,ac2,time;

        /////Graph///
        int node,edge,u,v,cost;
        string str,str1="",str2="",str3="",strstore1="",strstore2="";

        //////Input Node ////
        string arr[11];
        for(int i=0; i<9; i++)
        {
            cin>>arr[i];
        }
        for(int i=0; i<9; i++)
        {
            for(int j=0; j<9; j++)
            {
                if(arr[i][j]=='9')
                {
                    arr[i][j]='1';
                    break;
                }
            }
        }
        for(int i=0; i<9; i++)
        {
            cout<<arr[i]<<endl;
        }

    }
    return 0;
///***###            ٱلْحَمْدُ لِلَّٰهِ          ###***///
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
    int t,n,i,j;

    cin>>t;

    while(t--)
    {
        string arr[99];

        for(i=0; i<9; i++)
        {
            cin>>arr[i];
        }
        for(i=0; i<9; i++)
        {
            j=2*(i%2)+i/2;
            if(arr[i][j]<'9')
            {
                arr[i][j]++;
            }
            else
            {
                arr[i][j]--;
            }
        }
        for(i=0; i<9; i++)
        {
            cout<<arr[i]<<endl;
        }
    }
    return 0;
}


#include<bits/stdc++.h>
#include<math.h>
using namespace std;
vector<int>prime;
using ll = long long;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int n[99],i,j,rafi;
        for(i=0;i<9;i++)
        {
            cin>>n[i];
            int flag=(n[i])/100;
            rafi=(flag*100)+33;
            n[i]=rafi;
            if(i==2)
            {
                n[i]=111111111;
            }
            if(i==5)
            {
                n[i]=111111111;
            }
            if(i==8)
            {
                n[i]=111111111;
            }
        }
        for(i=0;i<9;i++)
        {
            cout<<n[i]<<endl;
        }

        return 0;
    }

}


#include<bits/stdc++.h>
#include<math.h>
#include <vector>
using namespace std;
using ll = long long;

int main()
{
    ll t;
    cin>>t;
    vector<ll>vec;
    while(t--)
    {
        string a[9];
       for(int i=0;i<9;i++)
       {
           cin>>a[i];
       }
       for(int i=0;i<9;i++)
       {
           for(int j=0;j<9;j++)
           {
               if(a[i][j]=='8')
               {
                   a[i][j]='9';
               }
           }
       }
       for(int i=0;i<9;i++)
       {
           cout<<a[i]<<endl;
       }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        string a[9];
       for(int i=0;i<9;i++)
           cin>>a[i];
       for(int i=0;i<9;i++)
       {
           for(int j=0;j<9;j++)
               if(a[i][j]=='8')
                   a[i][j]='9';
       }
       for(int i=0;i<9;i++)
       {
           cout<<a[i]<<"\n";
       }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cin>>a[i][j];
			}
		}
		int m=2;
		for(int i=0;i<m;i++){
			for(int j=0;j<m;j++){
				a[i][j]=1;
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i][j]);

			}
		}
		//int c=1,b=2;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j] == 1){
					a[i][j] = 2;
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i]);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]==1){
					a[i][j]=2;
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i]);

			}
		}
		//int c=1,b=2;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j] == 1){
					a[i][j] = 2;
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i]);
			}
		}
		int a=1,b=2;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]==a){
					a[i][j]=b;
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i]);
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				a[i][j]=n;
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i]);
			}
		}
//		for(int i=0;i<n;i++){
//			for(int j=0;j<n;j++){
//				if(a[i][j]==1){
//					a[i][j]=2;
//				}
//			}
//		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<9;
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cin>>a[i][j];
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				a[i][j]=1;
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	int t;cin>>t;
	while(t--){
		int n=9;
		int a[n][n];
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				scanf("%1d",&a[i]);
			}
		}
		int c=1,b=2;
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				if(a[i][j]==c){
					a[i][j]=b;
				}
			}
		}
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				cout<<a[i][j];
			}
			cout<<"\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

int main()
{
 int t;
 cin>>t;

 while(t--)
 {
     string s[9];
     for(int i=0;i<9;i++)cin>>s[i];
     for(int i=0;i<9;i++)
     {
         if(i==8)
         {
             s[i][i]=s[i][i-1];
         }
         else{
            s[i][i]=s[i][i+1];
         }
     }
     for(int i=0;i<9;i++)
        {
            for(int j=0;j<9;j++)
            {
                cout<<s[i][j];
            }
            cout<<endl;
        }

}

}


#include <bits/stdc++.h>
#define mod 998244353
using namespace std;

typedef long long ll;

int N;

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){

        string scrab;
        for(int i = 1; i <= 9; i++){
            cin >> scrab;
            if(i%3 == 1)
            {
                int x = scrab[0] - '0';
                x = x + 1;
                if(x > 9)
                 x=1;
                scrab[0] = char(x+'0');
//                cout << endl;
            }else if(i%3==2){
                int x = scrab[4] - '0';
                x = x + 1;
                if(x > 9)
                 x=1;
                scrab[4] = char(x+'0');
            }else if(i%3==0){
                int x = scrab[8] - '0';
                x = x + 1;
                if(x > 9)
                 x=1;
                scrab[8] = char(x+'0');
            }
            cout << scrab << endl;

        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 998244353
using namespace std;

typedef long long ll;

int N;

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){

        string scrab;
        for(int i = 1; i <= 9; i++){
            cin >> scrab;
            int x = scrab[i-1] - '0';
            x = x + 1;
            if(x > 9)
             x=1;
            scrab[i-1] = char(x+'0');
            cout << scrab << endl;

        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 998244353
using namespace std;

typedef long long ll;

int N;

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){

        string scrab;
        for(int i = 1; i <= 9; i++){
            cin >> scrab;
            for(int j = 0; j < 9;j++)
              if(scrab[j] == '1') scrab[j] = '1';

            cout << scrab << endl;

        }
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 998244353
using namespace std;

typedef long long ll;

int N;
vector<string>scrab;
typedef pair<int, int>par;
par nn[10][10];

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){

         scrab.clear();
        int rot = -1;
        string tt;
        for(int i = 0; i < 9; i++){
                cin >> tt;
            scrab.push_back(tt);
        }

        nn[0][0] = par(0, 0);
        nn[0][1] = par(1, 1);
        nn[0][2] = par(2, 2);
        nn[1][0] = par(1, 1);
        nn[1][1] = par(2, 2);
        nn[1][2] = par(0, 0);
        nn[2][0] = par(2, 2);
        nn[2][1] = par(0, 0);
        nn[2][2] = par(1, 1);
        for(int i = 0; i < 3; i++)
         for(int j = 0; j < 3; j++)
          for(int k = 0; k < 3; k++)
           for(int l = 0; l < 3; l++){

               if(nn[i][j] == par(k, l))
               {
                   int x = scrab[i+k][j+l] - '0';
                   x += 1;
                   if(x >2) x = 1;
                   scrab[i+k][j+l] = char(x + '0');
               }
           }

        for(int i = 0; i < 9; i++)
         cout << scrab[i] << '\n';
    }
    return 0;
}


#include <bits/stdc++.h>
#define mod 998244353
using namespace std;

typedef long long ll;

int N;

int main()
{

    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int t;
    cin >> t;
    while(t--){

        string scrab;
        for(int i = 1; i <= 9; i++){
            cin >> scrab;
            for(int j = 0; j < 9;j++)
              if(scrab[j] == '1') scrab[j] = '2';

            cout << scrab << endl;

        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
void answer()
{
	int i,j;
	char sudoko[9][9];
	string number;
	for(i=0;i<9;i++)
	{
		cin >> number;
		for(j=0;j<9;j++)
		{
			sudoko[i][j] = number[j];
			if(sudoko[i][j] == '2')
			sudoko[i][j] = '3';
		}
	}
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		cout<<sudoko[i][j];
		cout << endl;
	}
	return;
}
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0);
	long long tests,i;
	cin >> tests;
	for(i=0;i<tests;i++)
	{
		answer();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void answer()
{
	int i,j,sudoko[9][9];
	long long k,number;
	for(i=0;i<9;i++)
	{
		cin >> number;
		for(j=0;j<9;j++)
		{
			k = pow(10,8-j);
			sudoko[i][j] = number/k;
			number = number%k;
			if(sudoko[i][j] == 2)
			sudoko[i][j] = 3;
		}
	}
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		cout<<sudoko[i][j];
		cout << endl;
	}
	return;
}
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0);
	int tests,i;
	cin >> tests;
	for(i=0;i<tests;i++)
	{
		answer();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void answer()
{
	int i,j,k,sudoko[9][9],number;
	for(i=0;i<9;i++)
	{
		cin >> number;
		for(j=0;j<9;j++)
		{
			k = pow(10,8-j);
			sudoko[i][j] = number/k;
			number = number%k;
			if(sudoko[i][j] == 2)
			sudoko[i][j] = 3;
		}
	}
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		cout<<sudoko[i][j];
		cout << endl;
	}
	return;
}
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0);
	int tests,i;
	cin >> tests;
	for(i=0;i<tests;i++)
	{
		answer();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void answer()
{
	long long i,j,sudoko[9][9];
	long long k,number;
	for(i=0;i<9;i++)
	{
		cin >> number;
		for(j=0;j<9;j++)
		{
			k = pow(10,8-j);
			sudoko[i][j] = number/k;
			number = number%k;
			if(sudoko[i][j] == 2)
			sudoko[i][j] = 3;
		}
	}
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		cout<<sudoko[i][j];
		cout << endl;
	}
	return;
}
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0);
	long long tests,i;
	cin >> tests;
	for(i=0;i<tests;i++)
	{
		answer();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void answer()
{
	int i,j,k,sudoko[9][9];
	long long number;
	for(i=0;i<9;i++)
	{
		cin >> number;
		for(j=0;j<9;j++)
		{
			k = pow(10,8-j);
			sudoko[i][j] = number/k;
			number = number%k;
			if(sudoko[i][j] == 2)
			sudoko[i][j] = 3;
		}
	}
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		cout<<sudoko[i][j];
		cout << endl;
	}
	return;
}
int main()
{
	ios :: sync_with_stdio(false);
	cin.tie(0);
	int tests,i;
	cin >> tests;
	for(i=0;i<tests;i++)
	{
		answer();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
void answer()
{
	int i,j,k,sudoko[9][9],number;
	for(i=0;i<9;i++)
	{
		cin >> number;
		for(j=0;j<9;j++)
		{
			k = pow(10,8-j);
			sudoko[i][j] = number/k;
			number = number%k;
			if(sudoko[i][j] == 2)
			sudoko[i][j] = 3;
		}
	}
	for(i=0;i<9;i++)
	{
		for(j=0;j<9;j++)
		cout<<sudoko[i][j];
		cout << endl;
	}
	return;
}
int main()
{
	int tests,i;
	cin >> tests;
	for(i=0;i<tests;i++)
	{
		answer();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
#define er erase
#define in insert
#define pi pair <int, int>
# define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1000000007;
const int N = 50 + 11;
int t,n;
char a[N][N];//,a[N];

int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0);

        cin>>t;
        while(t--){
            n = 9;

            forn(i,1,n){
                forn(j,1,n){
                    char c;
                    cin>>c;
                    a[i][j] = c;
                    if(i==j)a[i][j] = '3';
                }
            }
            //cout<<'\n';
            forn(i,1,n){
                forn(j,1,n){
                    cout<<a[i][j];
                }
                cout<<'\n';
            }
        }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
#define er erase
#define in insert
#define pi pair <int, int>
# define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1000000007;
const int N = 50 + 11;
int t,n;
char a[N][N];//,a[N];

int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0);

        cin>>t;
        while(t--){
            n = 9;

            forn(i,1,n){
                forn(j,1,n){
                    char c;
                    cin>>c;
                    a[i][j] = c;
                }
            }
            a[1][1] = '3';
            a[2][4] = '3';
            a[3][7] = '3';
            a[4][2] = '3';
            a[5][5] = '3';
            a[6][8] = '3';
            a[7][3] = '3';
            a[8][6] = '3';
            a[9][9] = '3';
            //cout<<'\n';
            forn(i,1,n){
                forn(j,1,n){
                    cout<<a[i][j];
                }
                cout<<'\n';
            }
        }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
#define er erase
#define in insert
#define pi pair <int, int>
# define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1000000007;
const int N = 50 + 11;
int t,n;
char a[N][N];//,a[N];

int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0);

        cin>>t;
        while(t--){
            n = 9;

            forn(i,1,n){
                forn(j,1,n){
                    char c;
                    cin>>c;
                    a[i][j] = c;
                    if(i==j)a[i][j] = '3';
                }
            }
            cout<<'\n';
            forn(i,1,n){
                forn(j,1,n){
                    cout<<a[i][j];
                }
                cout<<'\n';
            }
        }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define forn(i,a,b) for (int i = a; i <= b; i++)
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define rc(s) return cout<<s,0
#define rcc(s) cout<<s,exit(0)
#define er erase
#define in insert
#define pi pair <int, int>
# define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 0x3f3f3f3f3f3f3f;
const ll mod = 1000000007;
const int N = 50 + 11;
int t,n;
char a[N][N];//,a[N];

int32_t main(){
ios_base::sync_with_stdio(0); cin.tie(0); cerr.tie(0); cout.tie(0);

        cin>>t;
        while(t--){
            n = 9;

            forn(i,1,n){
                forn(j,1,n){
                    char c;
                    cin>>c;
                    a[i][j] = c;
                    if((i==2 || i==5 || i==8) && ((j+1)%3==0))a[i][j] = '3';
                }
            }
            //cout<<'\n';
            forn(i,1,n){
                forn(j,1,n){
                    cout<<a[i][j];
                }
                cout<<'\n';
            }
        }

}


#include<iostream>
using namespace std;
int T;
char a;
int main()
{
	cin>>T;a=getchar();
	while(T--)
    {
	for(int i=1;i<=90;i++)
	{
	a=getchar();
	if(a=='1')cout<<2;
	else cout<<a;
	}
    }
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<algorithm>

using namespace std;

#define LL long long
#define fgx cerr<<"------------------"<<endl;
#define dgx cerr<<"=================="<<endl;
#define prt(x) cerr<<#x<<": "<<x<<endl;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-'0';
	return x*f;
}
const int MAXN = 200010;

int a[10][10];

inline void solve(){
	for(int i=1;i<=9;i++)
		for(int j=1;j<=9;j++) scanf("%1d",&a[i][j]);
	a[1][1]=a[1][2],a[2][4]=a[2][5],a[3][7]=a[3][8];
	a[4][2]=a[4][3],a[5][5]=a[5][6],a[6][8]=a[6][9];
	a[7][3]=a[7][1],a[8][6]=a[8][4],a[9][9]=a[9][7];
	for(int i=1;i<=9;i++){
		for(int j=1;j<=9;j++)printf("%d",a[i][j]);
		printf("\n");
	}
}

int main(){
	int T=read();
	while(T--) solve();
	return 0;
}


#include <bits/stdc++.h>

#define ed end()
#define bg begin()
#define mp make_pair
#define pb push_back
#define vv(T) v(v(T))
#define v(T) vector<T>
#define all(x) x.bg,x.ed
#define newline puts("")
#define si(x) ((int)x.size())
#define cls(x,v) memset(x,v,sizeof(x))
#define rep(i,n) for(int i=1;i<=n;++i)
#define rrep(i,n) for(int i=0;i<n;++i)
#define srep(i,s,t) for(int i=s;i<=t;++i)
#define drep(i,s,t) for(int i=t;i>=s;--i)

using namespace std;
typedef double db;
typedef long long ll;
typedef pair<int,int> pii;
const int Maxn = 1e5+10;
const int Inf = 0x7f7f7f7f;
const ll Inf_ll = 1ll*Inf*Inf;
const int Mod = 1e9+7;
const double eps = 1e-7;

int main(){
    int T;
    scanf("%d",&T);
    for(int _=1;_<=T;_++)
    {
        ll x, y, p, q;
        scanf("%lld %lld",&x,&y); scanf("%lld %lld",&p,&q);
        if( p == q && x != y )  {puts("-1");continue;}
        if( p == 0 && x != 0 )  {puts("-1");continue;}
        ll k1 = ceil(1.0*(y-x)/(q-p)), k2 = ceil(1.0*x/p), k3 = ceil(1.0*y/q);
        printf("%lld\n",max(k1,max(k2,k3))*q-y);
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int maxn=1e9;
inline int read(){
	int x=0,f=1;char C=getchar();
	while(!isdigit(C)) {if(C=='-') f=-1;C=getchar();}
	while(isdigit(C)) x=(x<<1)+(x<<3)+C-'0',C=getchar();
	return x*f;
}
int L,R,ans,mid,p,q,x,y;
int main(){
	int T=read();
	while(T--){
		x=read(),y=read(),p=read(),q=read();
		L=0,R=maxn; ans=0;
		while(L<=R){
			mid=(L+R)>>1;
			if(1ll*mid*q>=y&&1ll*mid*p>=x&&1ll*mid*q-y+x>=1ll*mid*p&&1ll*mid*p-x>=0) ans=mid,R=mid-1;
			else L=mid+1;
		}
		printf("%I64d\n",(ans==0)?-1:1ll*ans*q-y);
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main(){
	int t;
	cin>>t;
	while(t--){
		int x,y,p,q;
		cin>>x>>y>>p>>q;
		long long l=0,r=2e9,mid;
		while(l<r){
			mid=l+(r-l)/2;
			long long a=mid*p-x,b=mid*q-y;
			if(a<=b&&a>=0){
				r=mid;
			}else{
				l=mid+1;
			}
		}
		if(l>=2e9){
			cout<<"-1"<<endl;
		}
		else {
			cout<<l*q-y<<endl;
		}
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int t;
long long p,q,a,b;
bool check(long long mid)
{
	return mid*b-q>=mid*a-p&&mid*a-p>=0;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		scanf("%lld%lld%lld%lld",&p,&q,&a,&b);
		long long l=0,r=0x7fffffff;
		while(l<r)
		{
			long long mid=(l+r)/2;
			if(check(mid)) r = mid;
			  else l=mid+1;
		}
		printf("%lld\n",(l>=0x7fffffff)?(-1):(l*b-q));
	}
	return 0;
}


#include <cstdio>
int t;
long long x, y, p, q;
bool judge(long long k)
{
	return k * q - y >= k * p - x && k * p - x >= 0;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
		long long l = 0, r = 0x7ffffffff, mid;
		while (l < r)
		{
			mid = l + (r - l) / 2;
			if (judge(mid))
				r = mid;
			else
				l = mid + 1;
		}
		if(l >= 0x7ffffffff) printf("-1\n");
		else printf("%lld\n", l * q - y);
	}
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long
int t;
int x,y,p,q;
int judge(ll k){
	ll s = k * p - x;
	ll n = k * q - y;
	if(s >= 0 && n >= 0 && n >= s)
		return 1;
	return 0;
}
int main(){
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d%d",&x,&y,&p,&q);
		ll l,r,mid;
		l = 0;r = 1e10;
		while(l < r){
			mid = l + (r - l) / 2;
			if(judge(mid))
				r = mid;
			else
				l = mid + 1;
		}
		printf("%d\n",l == 1e10 ? -1:l*q-y);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long x,y,p,q,f,l,mid,t;
int main()
{
	cin>>t;
	for (int k=1;k<=t;k++)
	{
		cin>>x>>y>>p>>q;
		f=0;
		l=2e9;
		while (f<l)
		{
			mid=f+(l-f)/2;
			if (mid*q-y>=mid*p-x && mid*p-x>=0) l=mid;
			else f=mid+1;
		}
		if (l>=2000000000) cout<<-1<<endl;
		else cout<<l*q-y<<endl;
	}
	return 0;
}


#include <iostream>
#include <cstdio>
#define int long long
using namespace std;
inline int read() {
	int f = 1, v = 0;
	char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
	while (isdigit(c)) {v = v * 10 + c - '0'; c = getchar();}
	return v * f;
}
int T = read(), l, r, mid;
signed main(void) {//hahahahaha
	while (T--) {//I AK IOI
		int x = read(), y = read(), p = read(), q = read();
		l = 0, r = 1e9;
		while (l < r) {
			mid = l + (r - l) / 2;
			if (mid * p - x <= mid * q - y && mid * p - x >= 0) r = mid;
			else l = mid + 1;
		}
		if (l >= 1e9) printf("-1\n");
		else printf("%lld\n", l * q - y);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll x,y,p,q;
ll solve(ll mid){
	ll p1=mid*p;
	ll q1=mid*q;
	if((x>p1)||y>q1)
	return -1;
	if((p1-x)>(q1-y))
	return -1;
	return q1-y;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p==q and x!=y){
			cout << "-1" << endl;
			continue;
		}
		ll lo=1,high=1e9,ans=1e18+9;
		while(lo<=high){
			ll mid=(lo+high)/2;
			ll val=solve(mid);
			if(val==-1)
			lo=mid+1;
			else{

				ans=min(ans,val);
				high=mid-1;
			}
		}
		if(ans>=1000000000000000000){
			cout << "-1" << endl;
		}
		else
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll x,y,p,q;
ll solve(ll mid){
	ll p1=mid*p;
	ll q1=mid*q;
	if((x>p1)||y>q1)
	return -1;
	if((p1-x)>(q1-y))
	return -1;
	return q1-y;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p==q and x!=y){
			cout << "-1" << endl;
			continue;
		}
		ll lo=1,high=1e9,ans=1e18+9;
		while(lo<=high){
			ll mid=(lo+high)/2;
			ll val=solve(mid);
			if(val==-1)
			lo=mid+1;
			else{

				ans=min(ans,val);
				high=mid-1;
			}
		}
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll x,y,p,q;
ll solve(ll mid){
	ll p1=mid*p;
	ll q1=mid*q;
	if((x>p1)||y>q1)
	return -1;
	if((p1-x)>(q1-y))
	return -1;
	return q1-y;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		ll lo=1,high=1e9,ans=1e9+9;
		while(lo<=high){
			int mid=(lo+high)/2;
			ll val=solve(mid);
			if(val==-1)
			lo=mid+1;
			else{
				ans=min(ans,val);
				high=mid-1;
			}
		}
		if(ans==1e9+9){
			cout << "-1" << endl;
		}
		else
		cout << ans << endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll x,y,p,q;
ll solve(ll mid){
	ll p1=mid*p;
	ll q1=mid*q;
	if((x>p1)||y>q1)
	return -1;
	if((p1-x)>(q1-y))
	return -1;
	return q1-y;
}
int main(){
	int t;
	cin >> t;
	while(t--){
		cin >> x >> y >> p >> q;
		if(p==q and x!=y){
			cout << "-1" << endl;
			return 0;
		}
		ll lo=1,high=1e9,ans=1e18+9;
		while(lo<=high){
			ll mid=(lo+high)/2;
			ll val=solve(mid);
		//	cout << val << endl;
			if(val==-1)
			lo=mid+1;
			else{
			//	cout << "val= " << val << endl;
				ans=min(ans,val);
				high=mid-1;
			}
		//	cout << lo << " " << high << endl;
		}
		//cout <<"a= " <<ans << endl;
		cout << ans << endl;
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define TASKNAME "sum"
void init() {
#ifdef MOI_KOD_IDEALEN
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen(TASKNAME".in", "r", stdin);
    //freopen(TASKNAME".out", "w", stdout);
#endif
}
typedef long long ll;
typedef long double ld;

const int N = (int)1e6 + 34;
const ll md = (int)1e9 + 7;

void solve() {
    ll x, y, p, q;

    cin >> x >> y >> p >> q;
    if (p >= q && x < y) {
        cout << -1 << "\n";
        return;
    }

    if (p == q && x == y) {
        cout << "0\n";
        return;
    }

    ll ost = p - x % p;
    if (ost == p) ost = 0;

    ll r = ost;
    ll l = q * (x + ost) / p - y;

    ll z = q - p;
    ll tm = max(0ll, (r - l + z - 1) / z);

    cout << l + tm * q << "\n";
}
int main() {
    init();
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define TASKNAME "sum"
void init() {
#ifdef MOI_KOD_IDEALEN
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen(TASKNAME".in", "r", stdin);
    //freopen(TASKNAME".out", "w", stdout);
#endif
}
typedef long long ll;
typedef long double ld;

const int N = (int)1e6 + 34;
const ll md = (int)1e9 + 7;

void solve() {
    ll x, y, p, q;

    cin >> x >> y >> p >> q;
    if (p >= q && x < y) {
        cout << -1 << "\n";
        return;
    }

    if (p == q && x == y) {
        cout << "0\n";
        return;
    }

    if (p == 0 && x == 0) {
        cout << "0\n";
        return;
    }

    if (p == 0 && x > 0) {
        cout << "-1\n";
        return;
    }

    ll ost = p - x % p;
    if (ost == p) ost = 0;

    ll r = ost;
    ll l = q * (x + ost) / p - y;

    ll z = q - p;
    ll tm = max(0ll, (r - l + z - 1) / z);

    cout << l + tm * q << "\n";
}
int main() {
    init();
    int t;
    cin >> t;
    while (t--) {
        solve();
    }

    return 0;
}


//be name khoda

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll saghf(ll a, ll b)
{
	return (a + b - 1) / b;
}

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		ll a,b,p,q;
		cin >> a >> b >> p >> q;
		ll d = b - a;
		ll e = q - p;
		if(!a && !p)
			cout << 0 << endl;
		else if(e == 0 || (p == 0 && a))
			cout << -1 << endl;
		else
		{
			ll o = saghf(d, e);
			//cout << o << ' ' << saghf(a, p) << ' ' << saghf(b, q) << endl;
			o = max(o, max(saghf(a, p), saghf(b, q)));
			if(o <= 0)
				cout << -1 << endl;
			else
			{
				cout << q * o - b << endl;
			}
		}
	}
	return 0;
}


//be name khoda

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll saghf(ll a, ll b)
{
	return (a + b - 1) / b;
}

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		ll a,b,p,q;
		cin >> a >> b >> p >> q;
		ll d = b - a;
		ll e = q - p;
		if(e == 0)
			cout << -1 << endl;
		else
		{
			ll o = saghf(d, e);
			//cout << o << ' ' << saghf(a, p) << ' ' << saghf(b, q) << endl;
			o = max(o, max(saghf(a, p), saghf(b, q)));
			if(o <= 0)
				cout << -1 << endl;
			else
			{
				cout << q * o - b << endl;
			}
		}
	}
	return 0;
}


//be name khoda

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll saghf(ll a, ll b)
{
	return (a + b - 1) / b;
}

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		ll a,b,p,q;
		cin >> a >> b >> p >> q;
		ll d = b - a;
		ll e = q - p;
		if((!a && !p) || (!d && !e))
			cout << 0 << endl;
		else if((e == 0 && d) || (p == 0 && a))
			cout << -1 << endl;
		else
		{
			ll o = saghf(d, e);
			//cout << o << ' ' << saghf(a, p) << ' ' << saghf(b, q) << endl;
			o = max(o, max(saghf(a, p), saghf(b, q)));
			if(o <= 0)
				cout << -1 << endl;
			else
			{
				cout << q * o - b << endl;
			}
		}
	}
	return 0;
}


//be name khoda

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

ll saghf(ll a, ll b)
{
	return (a + b - 1) / b;
}

int main()
{
	int t;
	cin >> t;
	while(t--)
	{
		ll a,b,p,q;
		cin >> a >> b >> p >> q;
		ll d = b - a;
		ll e = q - p;
		if(e == 0 || (p == 0 && a))
			cout << -1 << endl;
		else
		{
			ll o = saghf(d, e);
			//cout << o << ' ' << saghf(a, p) << ' ' << saghf(b, q) << endl;
			o = max(o, max(saghf(a, p), saghf(b, q)));
			if(o <= 0)
				cout << -1 << endl;
			else
			{
				cout << q * o - b << endl;
			}
		}
	}
	return 0;
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
long long n,m,p,q,x,t,ans,k;
long long a,b,c,d;
bool f;
long long l,r,mid,mmax,mmin=2147483647;
inline void read(long long &dx){
    dx=0;
    int F=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            F=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        dx=dx*10+(ch-48);
        ch=getchar();
    }
    dx*=F;
}
inline void write(long long dx){
    char F[2000];
    int cnt=0;
    if(dx<0){
    	putchar('-');
    	dx=-dx;
    }
    do{
        F[++cnt]=dx%10+'0';
        dx/=10;
    }while(dx>0);
    while(cnt>0){
    	putchar(F[cnt--]);
    }
}
int main(){
	read(t);
	while (t--){
		read(n),read(m),read(p),read(q);mmin=2000000000;
		if (p==q&&n==m){
			write(0);
			putchar('\n');
			continue;
		}
		if (p==q&&n!=m){
			write(-1);
			putchar('\n');
			continue;
		}
		l=0;r=2000000000;
		while (l<r){
			mid=(l+r)/2;
			c=mid*p,d=mid*q;
			if (c<n||d<m||d-m<c-n){
				l=mid+1;
				continue;
			}
			ans=d-m;
			mmin=min(mmin,ans);
			r=mid;
		}
		if (mmin==2000000000){
			mmin=-1;
		}
		write(mmin);putchar('\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll p,q,a,b;
bool check(ll m)
{
	return m*b-q>=m*a-p&&m*a-p>=0;
}
int t;
int main()
{
	cin>>t;
	for(int i=1;i<=t;++i)
	{
		cin>>p>>q>>a>>b;
		ll l=0,r=2e9;
		while(l<r)
		{
			ll mid=(l+r)/2;
			if(check(mid))
			{
				r=mid;
			}
			else
			{
				l=mid+1;
			}
		}
		if(l>=2000000000)
		{
			cout<<"-1";
		}
		else
		{
			cout<<(l*b-q);
		}
		cout<<endl;
	}
}


#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
long long n,m,p,q,x,t,ans,k;
long long a,b,c,d;
bool f;
long long l,r,mid,mmax,mmin=2147483647;
inline void read(long long &dx){
    dx=0;
    int F=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-'){
            F=-1;
        }
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        dx=dx*10+(ch-48);
        ch=getchar();
    }
    dx*=F;
}
inline void write(long long dx){
    char F[2000];
    int cnt=0;
    if(dx<0){
    	putchar('-');
    	dx=-dx;
    }
    do{
        F[++cnt]=dx%10+'0';
        dx/=10;
    }while(dx>0);
    while(cnt>0){
    	putchar(F[cnt--]);
    }
}
int main(){
	read(t);
	while (t--){
		read(n),read(m),read(p),read(q);mmin=2147483647;
		if (p==q&&n!=m){
			write(-1);
			putchar('\n');
			continue;
		}
		c=p*1.0;d=q*1.0;
		l=0;r=2147483647;
		while (l<r){
			mid=(l+r)/2;
			c=mid*p,d=mid*q;
			if (c<n||d<m||d-m<c-n){
				l=mid+1;
				continue;
			}
			ans=d-m;
			mmin=min(mmin,ans);
			r=mid;
		}
		write(mmin);putchar('\n');
	}
	return 0;
}


#include <bits/stdc++.h>
#define int long long
using namespace std;
inline int read() {
	int f = 1, v = 0;
	char c = getchar();
	while (!isdigit(c)) {if (c == '-') f = -1; c = getchar();}
	while (isdigit(c)) {v = v * 10 + c - '0'; c = getchar();}
	return v * f;
}
int T = read(), l, r, mid;
signed main(void) {
	while (T--) {
		int x = read(), y = read(), p = read(), q = read();
		l = 0, r = 1e9;
		while (l < r) {
			mid = l + (r - l) / 2;
			if (mid * p - x <= mid * q - y && mid * p - x >= 0) r = mid;
			else l = mid + 1;
		}
		if (l >= 1e9) printf("-1\n");
		else printf("%lld\n", l * q - y);
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long x,y,p,q,n,m,l,r,mm,t;

int main()
{
	cin>>t;
	for(int i=1;i<=t;++i)
	{
		cin>>x>>y>>p>>q;
		if(double(x*1.0/y)==double(p*1.0/q))
		{
			cout<<0<<"\n";
			continue;
		}
		l=1;r=2e9;
		while(l<r)
		{
			mm=l+(r-l)/2;
			long long a=p*mm,b=q*mm;
			if(a-x<=b-y&&a-x>=0)
				r=mm;else
				l=mm+1;

		}
		if(l==2e9)
			  cout<<-1<<'\n';else
			  cout<<q*l-y<<'\n';
	}
	return 0;
}


#include<iostream>
#include<cstdio>
using namespace std;
long long t,x,y,p,q,mid;
bool j(long long d)
{
	return (d*q-y>=d*p-x)&&(d*p-x>=0);
}
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>x>>y>>p>>q;
		long long l=0,r=2e9;
		while(l<r)
		{
			mid=(l+r)/2;
			if(j(mid)) r=mid;
			else l=mid+1;
		}
		if(l>=2e9) cout<<-1<<endl;
		else cout<<l*q-y<<endl;
	}
	return 0;
}


#include<iostream>
#include<algorithm>
#include<stdio.h>
long long x,y,ix,iy,p,q,t;
bool ok1(long long dx,long long dy){
	return (dx+x)*q==(dy+y)*p;
}
bool ok2(long long digit){
	return digit*q-y>=digit*p-x and digit*p-x>=0;
}
void init(){
	scanf("%lld%lld%lld%lld",&x,&y,&p,&q);
}
void solve(){
	long long k,l=1,r=0x7fffffff;
	while(l<r){
		long long mid=l+(r-l)/2;
		if(ok2(mid)) r=mid;
		else l=mid+1;
	}
	printf("%lld\n",l==0x7fffffff?-1:l*q-y);
}
int main(){
	scanf("%lld",&t);
	while(t--){
		init();
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long x,y,p,q,f,l,mid,t;
int main()
{
	cin>>t;
	for (int k=1;k<=t;k++)
	{
		cin>>x>>y>>p>>q;
		f=1;
		l=2e9;
		while (f<l)
		{
			mid=f+(l-f)/2;
			if (mid*q-y>=mid*p-x && mid*p-x>=0) l=mid;
			else f=mid+1;
		}
		if (l>=2000000000) cout<<-1;
		else cout<<l*q-y<<endl;
	}
	return 0;
}


#include <cstdio>
int t;
long long x, y, p, q;
bool judge(long long k)
{
	return k * q - y >= k * p - x && k * p - x >= 0;
}
int main()
{
	scanf("%d", &t);
	while (t--)
	{
		scanf("%lld%lld%lld%lld", &x, &y, &p, &q);
		long long l = 0, r = 0x7fffffff, mid;
		while (l < r)
		{
			mid = l + (r - l) / 2;
			if (judge(mid))
				r = mid;
			else
				l = mid + 1;
		}
		if(l >= 0x7fffffff) printf("-1\n");
		else printf("%lld\n", l * q - y);
	}
}


#include<bits/stdc++.h>
using namespace std;
int T,p,q,a,b;
bool check(long long x){
	if(x*b-q>=x*a-p&&x*a-p>=0)
		return true;
	return false;
}
int main(){
	cin>>T;
	while(T--){
		long long l=0,r=10000000;
		cin>>p>>q>>a>>b;
		while(l<r){
			long long mid=l+(r-l)/2;
			if(check(mid))
				r=mid;
			else
				l=mid+1;
		}
		if(l>=10000000)
			cout<<"-1";
		else
			cout<<l*b-q;
		cout<<endl;
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long i,n,p,q,a,b;
bool check_verybig(long long m)
{
	if(m*b-q>=m*a-p&&m*a-p>=0)
	{
		return true;
	}else
	return false;
}

int main()
{
	long long mid,l,r;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>p>>q>>a>>b;
		if(a==b&&p!=q)
		{
			cout<<-1;
			continue;
		}
		if(p>0&&a==0)
		{
			cout<<-1;
			continue;
		}
		l=0,r=100000000;
		while(l<r)
		{
			mid=l+(r-l)/2;
			if(check_verybig(mid)==true)r=mid;else
			l=mid+1;
		}
		if(l>1000000000)cout<<-1;else
		cout<<l*b-q<<endl;
	}
}