# Help me in solving FIND_X problem

### My issue

what is the problem here?

### My code

``````#include <iostream>
using namespace std;

int main() {
int t;
cin>>t;
while(t--){
int a,b,c,d;
cin>>a>>b>>c>>d;
int x=1;
while((a+x)%b!=(c+x)%d){
x++;
}
cout<<x<<endl;
}
return 0;
}

``````

Problem Link: FIND_X Problem - CodeChef

@viga
brute force will not work for this problem .
plzz refer the following solution for better understand of optimal solution

``````#include <iostream>
#include<bits/stdc++.h>
#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;
ll r1,r2;
r1=a%b;r2=c%d;
ll d1=b-r1,d2=d-r2;
if(d1>1 && d2>1)
{
cout<<"1"<<endl;
}
else if(d1==d2)
{
cout<<"1"<<endl;
}
else
{
// ll i=1,m1=min(b,d);
//// a++;c++;
// while(1)
// {
//     if((a+i)%b==(c+i)%d)
//     {
//         cout<<i<<endl;
//         break;
//     }
//     else
//     {
//         i++;
//     }
// }
ll g1=__gcd(b,d);
ll l1=(b*d)/g1;
cout<<(l1-r1)<<endl;
}
}
return 0;
}
``````