#include <bits/stdc++.h>

using namespace std;

#define pb push_back
#define mp make_pair
#define ff first
#define ss second

typedef long long int ll;
typedef vector< pair<int, int> > vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long int> vll;
typedef pair<int, int> pii;

const ll INF = 1e18;
const int inf = 1e9;
const int MOD = 1e9 + 7;
const int nax = 1000000 + 10;

ll p, q, x, y;
bool is(ll nt)
{
    ll z = p * y + p * nt - q * x;
    z /= q;
    if(z >= 0 && z <= nt)
        return true;
    return false;
}
int main()
{
    ios::sync_with_stdio(0);

    int t; cin >> t;
    while(t--)
    {
        cin >> x >> y >> p >> q;
        ll l = 0;
        ll r = 1e11;
        ll ans = -1;

        while(r - l > 1)
        {
            ll mid = (l + r) / 2;
            if(is(mid) == true)
            {
                ans = mid;
                r = mid;
            }
            else
                l = mid;
        }
        ll mainans;
        if(ans == -1)
        {
            cout << -1 << endl;
            continue;
        }
        if(ans > 0 && is(ans - 1))
            mainans = ans - 1;
        else
            mainans = ans;
    ll temp = y + mainans;
    ll xnot;
    if(temp % q != 0)
        xnot = temp / q + 1;
    else
        xnot = temp / q;
    ll diff = q * xnot - temp;

    cout << mainans + diff << endl;

    }

    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0&&x!=0))
       {
           cout<<-1<<endl;
           continue;
       }
       if(p==0)
       {
           cout<<0<<endl;
           continue;
       }
       if(p%q==0&&y%x==0&&p/q&&y/x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int t;
   cin>>t;
   while(t--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if(p==q)
       {
           cout<<-1<<endl;
           return 0;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0&&x!=0))
       {
           cout<<-1<<endl;
           continue;
       }
       if(p==0)
       {
           cout<<0<<end;
           continue;
       }
       if(p%q==0&&y%x==0&&p/q&&y/x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0&&x!=0))
       {
           cout<<-1<<endl;
           continue;
       }
       if(p==q&&y==x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0&&x!=0))
       {
           cout<<-1<<endl;
           continue;
       }
       if(p%q&&y%x&&p/q&&y/x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        ll x,y,p,q,n;
        cin>>x>>y>>p>>q;
        if(p==q)
        {
            cout<<-1<<endl;
            return 0;
        }
        else if(q-p==1)
        {
            n=0;
        }
        else
        {
            ll ans=ceil((1.0*x)/(1.0*(p)));
            n=ans*(q-p)-(y-x);
        }
        ll m=p*((y-x)+n);
        m/=(q-p);
        cout<<m+n-x<<endl;
    }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0))
       {
           cout<<-1<<endl;
           continue;
       }
       if(p==q&&y==x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if(p==q||p==0)
       {
           cout<<-1<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0&&x!=0))
       {
           cout<<-1<<endl;
           continue;
       }
       if(p%q==0&&y%x==0&&p/q&&y/x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if((p==q&&y!=x)||(p==0&&x!=0))
       {
           cout<<-1<<endl;
           continue;
       }
       ll yy=__gcd(x,y);
       x/=yy;
       y/=yy;
       ll ww=__gcd(p,q);
       p/=ww;
       q/=ww;
       if(p==q&&y==x)
       {
           cout<<0<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
   int tt;
   cin>>tt;
   while(tt--)
   {
       ll x,y,p,q;
       cin>>x>>y>>p>>q;
       if(p==q)
       {
           cout<<-1<<endl;
           continue;
       }
       ll ans=ceil((1.0*x)/(1.0*(p)));
//       cout<<"ans "<<ans<<endl;
       ll n=ans*(q-p)-(y-x);
       if(n<=0)
       {
           ll demo=(q-p)/(__gcd(p,(q-p)));
           n=ceil((1.0*(y-x)/(1.0*demo)))*demo -(y-x);
       }
//       cout<<n<<endl;
       ll m=p*((y-x)+n);
       m/=(q-p);
       cout<<m+n-x<<endl;
   }
}


#include <cmath>
#include <cstdio>

typedef long long ll;
typedef long double ld;

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    ll x, y, p, q; scanf("%lld %lld %lld %lld\n", &x, &y, &p, &q);

    ll res = 0;
    if(p == 0) {
      res = x == 0 ? 0 : -1;
    } else if(q == 1) {
      res = x == y ? 0 : -1;
    } else {
      if(x == 0) { x += 1; y += 1; res = 1; }
      bool ltePQ = x * q / (ld) p >= y;

      ll x1 = x + (ltePQ ? 0 : (ll) ceil((y * p - q * x) / (ld) (q - p)));
      x1 = p * (ll) ceil(x1 / (ld) p);
      ll y1 = x1 / p * q;

      res += y1 - y;
    }
    if(res == 499999992000000030)
    	printf("%lld,%lld,%lld,%lld\n",x,y,p,q);
    printf("%lld\n", res);
  }
  return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define INT long long
#define oo 987654321
#define MOD 1000000007

using namespace std;

bool poss(long double m, long double o, long double x, long double y, long double p, long double q)
{
    x = p*y/q;
    return o <= x and x <= o+m;
}

void solve(void)
{
    INT x, y, p, q;
    cin>>x>>y>>p>>q;
    INT lb = 0, ub = 4000000000000000000ll;
    while( lb < ub )
    {
        INT m = (lb+ub-1)/2;
        if( poss(m, x, x, y+m, p, q) ) ub = m;
        else lb = m+1;
    }
    if( ub >= 4000000000000000000ll ){ cout<<-1<<endl; return; }
    if( (p*(y+ub))%q )
        ub += (q -((y+ub)%q));
    cout<<ub<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while( t-- ) solve();
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
      while(t--)
      {
          __int64 x,y,p,q;
          scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
          if(x*q==y*p)
          {
              printf("0\n");
              continue;
          }
           if(x!=0&&p==0)
            printf("-1\n");
          else if(p==q&&x!=y)
            printf("-1\n");
          else
          {
              __int64 l=1,r=10000000000,p1,q1,k1,k2;
              __int64 mm=9223372036854775800;
              while(l<=r)
              {
                  __int64 mid=(l+r)/2;
                  p1=p*mid;
                  q1=q*mid;
                  if(p1<x)
                  {
                      l=mid+1;
                      continue;
                  }
                  k1=p1-x;
                  if(y+k1>q1)
                  {
                      l=mid+1;
                      continue;
                  }
                  mm=min(mm,q1-y);
                  r=mid-1;
              }
              if(mm!=1000000005)
                printf("%I64d\n",mm);
              else
                printf("-1\n");
          }
      }
    }
    return 0;
}


#include<bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<bitset>
using namespace std;         //

#define ll long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
#define sqr(a) (a)*(a)
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
ll qp(ll a,ll b,ll mod){
	ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t;
}
struct DOT{int x;int y;};
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;

int T;
int x,y,p,q;

int chk(ll m){
	if(1ll*q*(m+x)>=1ll*p*(y+m))return 1;
	return 0;
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d",&x,&y,&p,&q);
		if(p==q && x!=y){puts("-1");continue;}
		if(p==0&&x!=0){
			puts("-1");continue;
		}else if(p==0 && x==0){
			puts("0");continue;
		}
		if(1ll*p*y==1ll*q*x && 1ll*p*y!=0){puts("0");continue;}

		ll seda=1ll*ceil((1.0*y*p-1.0*q*x)/(q-p));
		seda=max(seda,0ll);
		ll sedxa=seda+x;
		if(sedxa%p!=0)sedxa+=p-(sedxa)%p;
                if(1ll*(sedxa)/p*q-y == 333333329000000008)
                       cout << x<<"+"<<y<<"+"<<p<<"+"<<q<< endl;
		printf("%lld\n",1ll*(sedxa)/p*q-y);
	}
}


#include <cmath>
#include <cstdio>

typedef long long ll;
typedef long double ld;

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    ll x, y, p, q; scanf("%lld %lld %lld %lld\n", &x, &y, &p, &q);

    ll res = 0;
    if(p == 0) {
      res = x == 0 ? 0 : -1;
    } else if(q == 1) {
      res = x == y ? 0 : -1;
    } else {
      if(x == 0) { x += 1; y += 1; res = 1; }
      bool ltePQ = x * q / (ld) p >= y;

      ll x1 = x + (ltePQ ? 0 : (ll) ceil((y * p - q * x) / (ld) (q - p)));
      x1 = p * (ll) ceil(x1 / (ld) p);
      ll y1 = x1 / p * q;

      res += y1 - y;
    }
    if(res == 499999992000000030)
    	cout << x<<"+"<<y<<"+"<<p<<"+"<<q<<endl;
    printf("%lld\n", res);
  }
  return 0;
}


#include <cmath>
#include <cstdio>

typedef long long ll;
typedef long double ld;

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    ll x, y, p, q; scanf("%lld %lld %lld %lld\n", &x, &y, &p, &q);

    ll res = 0;
    if(p == 0) {
      res = x == 0 ? 0 : -1;
    } else if(q == 1) {
      res = x == y ? 0 : -1;
    } else {
      if(x == 0) { x += 1; y += 1; res = 1; }
      bool ltePQ = x * q / (ld) p >= y;

      ll x1 = x + (ltePQ ? 0 : (ll) ceil((y * p - q * x) / (ld) (q - p)));
      x1 = p * (ll) ceil(x1 / (ld) p);
      ll y1 = x1 / p * q;

      res += y1 - y;
    }
    printf("%lld\n", res);
  }
  return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
      while(t--)
      {
          __int64 x,y,p,q;
          scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
          if(x*q==y*p)
          {
              printf("0\n");
              continue;
          }
           if(x!=0&&p==0)
            printf("-1\n");
          else if(p==q&&x!=y)
            printf("-1\n");
          else
          {
              __int64 l=1,r=10000000000,p1,q1,k1,k2;
              __int64 mm=9223372036854775800;
              while(l<=r)
              {
                  __int64 mid=(l+r)/2;
                  p1=p*mid;
                  q1=q*mid;
                  if(p1<x)
                  {
                      l=mid+1;
                      continue;
                  }
                  k1=p1-x;
                  if(y+k1>q1)
                  {
                      l=mid+1;
                      continue;
                  }
                  mm=min(mm,q1-y);
                  r=mid-1;
              }
              if(mm!=1000000005)
                printf("%I64d\n",mm);
              else
              {
            	  printf("%I64d %I64d",p,q);
                printf("-1\n");
              }
          }
      }
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define INT long long
#define oo 987654321
#define MOD 1000000007

using namespace std;

bool poss(long double m, long double o, long double x, long double y, long double p, long double q)
{
    x = p*y/q;
    return o <= x and x <= o+m;
}

void solve(void)
{
    INT x, y, p, q;
    cin>>x>>y>>p>>q;
    INT lb = 0, ub = 4000000000000000000ll;
    while( lb < ub )
    {
        INT m = (lb+ub-1)/2;
        if( poss(m, x, x, y+m, p, q) ) ub = m;
        else lb = m+1;
    }
    if( ub >= 4000000000000000000ll ){ cout<<-1<<endl; return; }
    if( (p*(y+ub))%q )
        ub += (q -((y+ub)%q));
    if(ub == 12999998937)
    	printf("%lld,%lld,%lld,%lld\n",x,y,p,q);
    cout<<ub<<endl;
}

int main()
{
    ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while( t-- ) solve();
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
      while(t--)
      {
          __int64 x,y,p,q;
          scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
          if(x*q==y*p)
          {
              printf("0\n");
              continue;
          }
           if(x!=0&&p==0)
            printf("-1\n");
          else if(p==q&&x!=y)
            printf("-1\n");
          else
          {
              __int64 l=1,r=10000000000,p1,q1,k1,k2;
              __int64 mm=9223372036854775800;
              while(l<=r)
              {
                  __int64 mid=(l+r)/2;
                  p1=p*mid;
                  q1=q*mid;
                  if(p1<x)
                  {
                      l=mid+1;
                      continue;
                  }
                  k1=p1-x;
                  if(y+k1>q1)
                  {
                      l=mid+1;
                      continue;
                  }
                  mm=min(mm,q1-y);
                  r=mid-1;
              }
              if(mm!=1000000005)
                printf("%I64d\n",mm);
              else
              {
            	  printf("%I64d %I64d %I64d",y,p,q);
                printf("-1\n");
              }
          }
      }
    }
    return 0;
}


#include <cmath>
#include <cstdio>

typedef long long ll;
typedef long double ld;

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    ll x, y, p, q; scanf("%lld %lld %lld %lld\n", &x, &y, &p, &q);

    ll res = 0;
    if(p == 0) {
      res = x == 0 ? 0 : -1;
    } else if(q == 1) {
      res = x == y ? 0 : -1;
    } else {
      if(x == 0) { x += 1; y += 1; res = 1; }
      ld y0 = x * q / (ld) p;

//      cerr << (y * p - q * x) / (ld) (q - p) << endl;

      ll x1 = x + (y0 >= y ? 0 : (ll) ceil((y * p - q * x) / (ld) (q - p)));
      x1 = p * (ll) ceil(x1 / (ld) p);
      ll y1 = x1 / p * q;

//      fprintf(stderr, "y0=%Lf, x1=%lld, y1=%lld\n", y0, x1, y1);
      res += y1 - y;
    }
    printf("%lld\n", res);
  }
  return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
      while(t--)
      {
          __int64 x,y,p,q;
          scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
          if(x*q==y*p)
          {
              printf("0\n");
              continue;
          }
           if(x!=0&&p==0)
            printf("-1\n");
          else if(p==q&&x!=y)
            printf("-1\n");
          else
          {
              __int64 l=1,r=10000000000,p1,q1,k1,k2;
              __int64 mm=9223372036854775800;
              while(l<=r)
              {
                  __int64 mid=(l+r)/2;
                  p1=p*mid;
                  q1=q*mid;
                  if(p1<x)
                  {
                      l=mid+1;
                      continue;
                  }
                  k1=p1-x;
                  if(y+k1>q1)
                  {
                      l=mid+1;
                      continue;
                  }
                  mm=min(mm,q1-y);
                  r=mid-1;
              }
              if(mm!=1000000005)
                printf("%I64d\n",mm);
              else
              {
            	  printf("%I64d",q);
                printf("-1\n");
              }
          }
      }
    }
    return 0;
}


#include<bits/stdc++.h>
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<algorithm>
#include<queue>
#include<string.h>
#include<iostream>
#include<math.h>
#include<set>
#include<map>
#include<vector>
#include<iomanip>
#include<bitset>
using namespace std;         //

#define ll long long
#define pb push_back
#define FOR(a) for(int i=1;i<=a;i++)
#define sqr(a) (a)*(a)
#define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y))
ll qp(ll a,ll b,ll mod){
	ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t;
}
struct DOT{int x;int y;};
const int dx[4]={0,0,-1,1};
const int dy[4]={1,-1,0,0};
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;

int T;
int x,y,p,q;

int chk(ll m){
	if(1ll*q*(m+x)>=1ll*p*(y+m))return 1;
	return 0;
}

int main(){
	scanf("%d",&T);
	while(T--){
		scanf("%d%d%d%d",&x,&y,&p,&q);
		if(p==q && x!=y){puts("-1");continue;}
		if(p==0&&x!=0){
			puts("-1");continue;
		}else if(p==0 && x==0){
			puts("0");continue;
		}
		if(1ll*p*y==1ll*q*x && 1ll*p*y!=0){puts("0");continue;}

		ll seda=1ll*ceil((1.0*y*p-1.0*q*x)/(q-p));
		seda=max(seda,0ll);
		ll sedxa=seda+x;
		if(sedxa%p!=0)sedxa+=p-(sedxa)%p;
		printf("%lld\n",1ll*(sedxa)/p*q-y);
	}
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#define INT long long
#define oo 987654321
#define MOD 1000000007

using namespace std;

bool poss(long double m, long double o, long double x, long double y, long double p, long double q)
{
    x = p*y/q;
    return o <= x and x <= o+m;
}

void solve(void)
{
    INT x, y, p, q;
    cin>>x>>y>>p>>q;
    INT lb = 0, ub = 4000000000000000000ll;
    while( lb < ub )
    {
        INT m = (lb+ub-1)/2;
        if( poss(m, x, x, y+m, p, q) ) ub = m;
        else lb = m+1;
    }
    if( ub >= 4000000000000000000ll ){ cout<<-1<<endl; return; }
    if( (p*(y+ub))%q )
        ub += (q -((y+ub)%q));
    if(ub == 12999998937)
    	printf("%lld,%lld,%lld,%lld\n",x,y,p,q);
    cout<<ub<<endl;
}

int main()
{

    int t;
    cin>>t;
    while( t-- ) solve();
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
long long x, y, p, q;
bool chk(long long limit){
    if(limit * q < y)
        return 0;
    if(limit * p < x)
        return 0;
    long long cur = (limit * q) - y;
    if(p * limit > x + cur)
        return 0;
    return 1;
}
int main(){
    int t;
    cin >> t;
    while(t--){
        cin >> x >> y >> p >> q;
        long long lo = 1, hi = 1e9;
        while(lo < hi){
            long long md = lo + (hi - lo)/2;
            if(chk(md))
                hi = md;
            else
                lo = md + 1;
        }
        if(!chk(lo)){
            puts("-1");
            continue;
        }
        cout << (lo * q) - (y) << '\n';
    }
    return 0;
}


#include <cmath>
#include <cstdio>

typedef long long ll;
typedef long double ld;

int main() {
  int t; scanf("%d\n", &t);
  for(int tc = 1; tc <= t; tc++) {
    ll x, y, p, q; scanf("%lld %lld %lld %lld\n", &x, &y, &p, &q);

    ll res = 0;
    if(p == 0) {
      res = x == 0 ? 0 : -1;
    } else if(q == 1) {
      res = x == y ? 0 : -1;
    } else {
      if(x == 0) { x += 1; y += 1; res = 1; }
      bool ltePQ = x * q / (ld) p >= y;

      ll x1 = x + (ltePQ ? 0 : (ll) ceil((y * p - q * x) / (ld) (q - p)));
      x1 = p * (ll) ceil(x1 / (ld) p);
      ll y1 = x1 / p * q;

      res += y1 - y;
    }
    if(res == 499999992000000030)
    	printf("%lld,%lld,%lld,%lld",x,y,p,q);
    printf("%lld\n", res);
  }
  return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
      while(t--)
      {
          __int64 x,y,p,q;
          scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
          if(x*q==y*p)
          {
              printf("0\n");
              continue;
          }
           if(x!=0&&p==0)
            printf("-1\n");
          else if(p==q&&x!=y)
            printf("-1\n");
          else
          {
              __int64 l=1,r=10000000000,p1,q1,k1,k2;
              __int64 mm=9223372036854775800;
              while(l<=r)
              {
                  __int64 mid=(l+r)/2;
                  p1=p*mid;
                  q1=q*mid;
                  if(p1<x)
                  {
                      l=mid+1;
                      continue;
                  }
                  k1=p1-x;
                  if(y+k1>q1)
                  {
                      l=mid+1;
                      continue;
                  }
                  mm=min(mm,q1-y);
                  r=mid-1;
              }
              if(mm!=1000000005)
                printf("%I64d\n",mm);
              else
              {
            	cout << x << " " << y << " " << p << " " << q << endl;
                printf("-1\n");
              }
          }
      }
    }
    return 0;
}


#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<vector>
#include<algorithm>
using namespace std;
int main()
{
    int t;
    while(~scanf("%d",&t))
    {
      while(t--)
      {
          __int64 x,y,p,q;
          scanf("%I64d %I64d %I64d %I64d",&x,&y,&p,&q);
          if(x*q==y*p)
          {
              printf("0\n");
              continue;
          }
           if(x!=0&&p==0)
            printf("-1\n");
          else if(p==q&&x!=y)
            printf("-1\n");
          else
          {
              __int64 l=1,r=10000000000,p1,q1,k1,k2;
              __int64 mm=9223372036854775800;
              while(l<=r)
              {
                  __int64 mid=(l+r)/2;
                  p1=p*mid;
                  q1=q*mid;
                  if(p1<x)
                  {
                      l=mid+1;
                      continue;
                  }
                  k1=p1-x;
                  if(y+k1>q1)
                  {
                      l=mid+1;
                      continue;
                  }
                  mm=min(mm,q1-y);
                  r=mid-1;
              }
              if(mm!=1000000005)
                printf("%I64d\n",mm);
              else
              {
            	  printf("%I64d %I64d %I64d %I64d",x,y,p,q);
                printf("-1\n");
              }
          }
      }
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
            if (p == 0) {
      cout << (x == 0 ? 0 : -1) << endl;
      continue;
    }
        if (p == q) {
      cout << (x == y ? 0 : -1) << endl;
      continue;
    }
        int lo=1,hi=1e9,ans=1e10;
        double b,a=double(x)/double(p);

       // if(q!=p)
            b=double(y-x)/double(q-p);
        /*else
        {
            cout<<"-1"<<endl;
            continue;
        }*/
        double maxi=max(a,b);
        int maxii=ceil(maxi);
        ans=maxii;
        /*for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;*/
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        //}
        if(ans==1e10)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e9;
        double b,a=double(x)/double(p);

        if(q!=p)
            b=double(y-x)/double(q-p);
        else
        {
            cout<<"-1"<<endl;
            continue;
        }
        double maxi=max(a,b);
        int maxii=ceil(maxi);
        ans=maxii;
        /*for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;*/
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        //}
        if(ans==1e9)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e9;
        while(lo<hi)
        {
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        }
        if(ans==1e9)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e9;
        double b,a=double(x)/double(p);

        if(q!=p)
            b=double(y-x)/double(q-p);
        else
        {
            cout<<"-1"<<endl;
            continue;
        }
        double maxi=max(a,b);
        int maxii=ceil(maxi);
        ans=maxii;
        /*for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;*/
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        //}
        if(ans>=1e9)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e10;
        while(lo<=hi)
        {
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        }
        if(ans==1e10)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e9;
        while(hi-lo>1)
        {
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid;
            }
            else
                lo=mid;
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        }
        if(ans==1e9)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
            if (p == 0) {
      cout << (x == 0 ? 0 : -1) << endl;
      continue;
    }
        if (p == q) {
      cout << (x == y ? 0 : -1) << endl;
      continue;
    }
        int lo=1,hi=1e9,ans=1e10;
        double b,a=double(x)/double(p);

       // if(q!=p)
            b=double(y-x)/double(q-p);
        /*else
        {
            cout<<"-1"<<endl;
            continue;
        }*/
        double maxi=max(a,b);
        int maxii=ceil(maxi);
        ans=maxii;
        /*for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;*/
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        //}
        if(ans==1e9)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e9;
        double b,a=double(x)/double(p);

        if(q!=p)
            b=double(y-x)/double(q-p);
        else
        {
            cout<<"-1"<<endl;
            continue;
        }
        double maxi=max(a,b);
        int maxii=ceil(maxi);
        ans=maxii;
        /*for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;*/
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        //}
        if(q*ans-y>=1e9)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e10;
        double b,a=double(x)/double(p);

        if(q!=p)
            b=double(y-x)/double(q-p);
        else
        {
            cout<<"-1"<<endl;
            continue;
        }
        double maxi=max(a,b);
        int maxii=ceil(maxi);
        ans=maxii;
        /*for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;*/
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        //}
        if(ans==1e10)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define speed std::ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define int long long

using namespace std;
int lcm(int a, int b)
{
    return (a*b)/(__gcd(a,b));
}
 main()
{
    speed
   // freopen("input.txt","r",stdin);
    int t,x,y,p,q;
    cin>>t;
    while(t--)
    {
        cin>>x>>y>>p>>q;
        int lo=1,hi=1e9,ans=1e10;
        for(int i=0; i<30; i++)
        {
            if(hi<lo)
            break;
            int mid=lo+(hi-lo)/2;
            if(x<=(p*mid)&&(y-x)<=(q*mid-p*mid))
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else
                lo=mid+1;
            /*int den=y+mid;
            int minn=x,maxx=x+mid;
            int lc=lcm(den,q);
            int fac=lc/den;
            minn*=fac;
            maxx*=fac;
            int fac2=lc/q;
            int p1=p*fac2;
            if(p1>=minn&&p1<=maxx)
            {
                ans=min(ans,mid);
                hi=mid-1;
            }
            else if(p1>maxx)
            {
                lo=mid+1;
            }
            else if(p1<minn)
            {
                hi=mid-1;
            }*/
        }
        if(ans==1e10)
            cout<<"-1"<<endl;
        else
            cout<<q*ans-y<<endl;
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n, a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==b){
			if(p==q)
				cout << 0 << endl;
			else
				cout << -1 << endl;
		}else{
			double n = max(ceil((double)q/b),ceil((double)(q-p)/(b-a)));
			n = max(n,ceil((double)p/a));
			cout << b*n - q << endl;
		}
	}

return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n;
double a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==b){
			if(p==q)
				cout << 0 << endl;
			else
				cout << -1 << endl;
		}else{
			double n = max(ceil(q/b),ceil((q-p)/(b-a)));
			n = max(n,ceil(p/a));
			cout << b*n - q << endl;
		}
	}

return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n, a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==b){
			if(p==q)
				cout << 0 << endl;
			else
				cout << -1 << endl;
		}else{
			long long n = max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a)));
			long long ans = b*n - q;
			if(ans<=1e10)
				cout << ans << endl;
			else
				cout << -1 << endl;
		}
	}

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a,b,p,q;
int main(){
	cin>>n;
	while(n--){
		cin>>p>>q>>a>>b;
		if(!a)
			!p?puts("0"):puts("-1");
		else
			if(a==b)
				p==q?puts("0"):puts("-1");
			else
				cout<<b*(ll)max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a))) - q<<'\n';
	}
}


#include <bits/stdc++.h>
using namespace std;
int n,a,b,p,q;
int main(){
	cin>>n;
	while(n--){
		cin>>p>>q>>a>>b;
		if(!a)
			!p?puts("0"):puts("-1");
		else
			if(a==b)
				p==q?puts("0"):puts("-1");
			else
				cout<<b*(int)max(max(ceil(1.0*q/b),ceil(1.0*p/a)),ceil(1.0*(q-p)/(b-a))) - q<<'\n';
	}
}


#include <bits/stdc++.h>

using namespace std;

long long n, a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==b){
			if(p==q)
				cout << 0 << endl;
			else
				cout << -1 << endl;
		}else{
			long long n = max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a)));
			long long ans = b*n - q;
			if(ans<=1e9)
				cout << ans << endl;
			else
				cout << -1 << endl;
		}
	}

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, a, b, p, q;
int main(){
	cin >> n;
	while(n--){
		cin >> p >> q >> a >> b;
		if(a==0)
			p==0?cout<<0<<'\n':cout<<-1<<'\n';
		else{
			if(a==b)
				p==q?cout<<0<<'\n':cout<<-1<<'\n';
			else{
				cout<<(ll)b*max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a))) - q<<'\n';
			}
		}
	}
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n,a,b,p,q;
int main(){
	cin>>n;
	while(n--){
		cin>>p>>q>>a>>b;
		if(!a)
			!p?puts("-1"):puts("0");
		else
			if(a==b)
				p==q?puts("0"):puts("-1");
			else
				cout<<b*(ll)max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a))) - q<<'\n';
	}
}


#include <bits/stdc++.h>
#define l long long
using namespace std;
l n,a,b,p,q;
int main(){
	cin>>n;
	while(n--){
		cin>>p>>q>>a>>b;
		if(!a)
			!p?puts("0"):puts("-1");
		else
			if(a==b)
				p==q?puts("0"):puts("-1");
			else
				cout<<b*(l)max(max(ceil(1.0*q/b),ceil(1.0*p/a)),ceil(1.0*(q-p)/(b-a))) - q<<'\n';
	}
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, a, b, p, q;
int main(){
	cin >> n;
	while(n--){
		cin >> p >> q >> a >> b;
		if(a==0)
			if(p==0)
				cout<<0<<'\n';
			else
				cout<<-1<<'\n';
		else{
			if(a==b)
				if(p==q)
					cout<<0<<'\n';
				else
					cout<<-1<<'\n';
			else{
				ll n = max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a)));
				cout<<b*n - q<<'\n';
			}
		}
	}

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, a, b, p, q;
int main(){
	cin >> n;
	while(n--){
		cin >> p >> q >> a >> b;
		if(a==0)
			p==0?cout<<0<<'\n':cout<<-1<<'\n';
		else{
			if(a==b)
				p==q?cout<<0<<'\n':cout<<-1<<'\n';
			else{
				cout<<b*(ll)max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a))) - q<<'\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

long long n, a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==b){
			if(p==q)
				cout << 0 << endl;
			else
				cout << -1 << endl;
			if(a==0)
				if(p==0)
					cout<<0<<endl;
				else
					cout<<-1<<endl;
		}else{
			long long n = max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a)));
			long long ans = b*n - q;
			cout << ans << endl;
		}
	}

return 0;
}


#include <bits/stdc++.h>

using namespace std;

long long n, a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==0){
			if(p==0)
				cout<<0<<endl;
			else
				cout<<-1<<endl;
		}else{
			if(a==b){
				if(p==q)
					cout << 0 << endl;
				else
					cout << -1 << endl;
			}else{
				long long n = max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a)));
				long long ans = b*n - q;
				cout << ans << endl;
			}
		}
	}

return 0;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll n, a, b, p, q;
int main(){
	cin >> n;
	while(n--){
		cin >> p >> q >> a >> b;
		if(a==0)
			p==0?cout<<0<<'\n':cout<<-1<<'\n';
		else{
			if(a==b)
				p==q?cout<<0<<'\n':cout<<-1<<'\n';
			else{
				cout<<b*max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a))) - q<<'\n';
			}
		}
	}
}


#include <bits/stdc++.h>

using namespace std;

long long n, a, b, p, q;

int main(){
	cin >> n;
	for(int i=1;i<=n;i++){
		cin >> p >> q >> a >> b;
		if(a==b){
			if(p==q)
				cout << 0 << endl;
			else
				cout << -1 << endl;
		}else{
			long long n = max(max(ceil((double)q/b),ceil((double)p/a)),ceil((double)(q-p)/(b-a)));
			cout << b*n - q << endl;
		}
	}

return 0;
}


#include <bits/stdc++.h>
#define l long long
using namespace std;
l n,a,b,p,q;
int main(){
cin>>n;
while(n--){
cin>>p>>q>>a>>b;
if(!a) !p?puts("0"):puts("-1");else if(a==b) p==q?puts("0"):puts("-1");else
cout<<b*(l)max(max(ceil(1.0*q/b),ceil(1.0*p/a)),ceil(1.0*(q-p)/(b-a))) - q<<'\n';}
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];
LL x,y,q,p,a,b;

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

LL check(LL num){
	LL u,v,w;
	u=p*num-x;
	v=q*num-y;
	if(v<0||u<0)return 0;
	return v>=u;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,r,ca=0,u,v,w;

	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
//		if(p==0){
//			if(x==0)cout<<"0"<<endl;
//			else cout<<"-1"<<endl;
//			continue;
//		}
//		if(p==q){
//			if(x==y)cout<<"0"<<endl;
//			else cout<<"-1"<<endl;
//			continue;
//		}
//		if(x%p==0&&y%q==0&&x/p==y/q){
//			cout<<"0"<<endl;
//			continue;
//		}
		u=v=ans=0;
		l=0;
		r=2e9+7;
		LL mid;
		while(l+1<r){
			mid=(l+r)/2;
			if(!check(mid))l=mid+1;
			else r=mid-1;
//			cout<<l<<" "<<r<<endl;
		}
//		cout<<l<<" "<<r<<endl;
		u=q*l-y;
		v=p*l-x;
		if(l==2e9+7)ans=-1;
		else ans=u;
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		u=v=ans=0;
//		u=p-x%p;
//		ans+=u;y+=u;x+=u;

		while(1){
			if(y*p<x*q){
				w=x*q+p-1;
				w=w/p-y;
				v+=w,ans+=w,y+=w;
			}
			if(y*p==x*q) break;
			w=p-x%p;
			x+=w;u+=w;y+=w;ans+=w;
		}
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		u=v=ans=0;
//		u=p-x%p;
//		ans+=u;y+=u;x+=u;

		while(1){
			if(y*p<x*q){
				w=x*q+p-1;
				w=w/p-y;
				v+=w,ans+=w,y+=w;
			}
			if(y*p==x*q) break;
			if(!ca){
				w=p-x%p;
				x+=w;u+=w;y+=w;ans+=w;
			}
			else {
				w=y*p+q-1;
				w=w/q-x;
				x+=w;y+=w;
				u+=w;ans+=w;
			}
			ca++;
			if(ca==2e5){
				ans=-1;break;
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		ans=-1;
		ans=max(ans,(y-x+q-p-1)/(q-p));
		ans=max(ans,(x+p-1)/p);
		ans=max(ans,(y+q-1)/q);
		//if(ans!=-1)ans=q*ans-y;
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		/*
		//By py.
		ans=0;
		ans=max(ans,(p*y-q*x+q-p-1)/(q-p));
		ans=max(ans,(x*q-p*y+p-1)/p);
		ans=(y+ans+q-1)/q*q-y;
		cout<<ans<<endl;
		*/

		ans=0;
		ans=max(ans,(x+p-1)/p);
		ans=max(ans,(x*y+x*q-p-1)/(x*q-p));
		ans=ans*q-y;
		cout<<ans<<endl;

	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}

		ans=0;
		ans=max(ans,(p*y-q*x+q-p-1)/(q-p));
		ans=max(ans,(x*q-p*y+p-1)/p);
		ans=(y+ans+q-1)/q*q-y;
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}

		ans=-1;
		ans=max(ans,(y-x+q-p-1)/(q-p));

//		ans=max(ans,(x+p-1)/p);
//		ans=max(ans,(y+q-1)/q);

//		if(ans!=-1) //It is guaranteed that the answer is always existed.
			ans=q*ans-y;
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		ans=-1;
		ans=max(ans,(y-x+q-p-1)/(q-p));
		ans=max(ans,(x+p-1)/p);
		ans=max(ans,(y+q-1)/q);
//		if(ans!=-1)
			ans=q*ans-y;
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
                        continue;
                }
                ans=-1;
                ans=max(ans,(y-x+q-p-1)/(q-p));
                ans=max(ans,(x+p-1)/p);
                ans=max(ans,(y+q-1)/q);
                if(ans!=-1)ans=q*ans-y;
                cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		u=v=ans=0;
//		u=p-x%p;
//		ans+=u;y+=u;x+=u;

		while(1){
			if(y*p<x*q){
				w=x*q+p-1;
				w=w/p-y;
				v+=w,ans+=w,y+=w;
			}
			if(y*p==x*q) break;
			w=p-x%p;
			x+=w;u+=w;y+=w;ans+=w;
			ca++;
			if(ca==1e5){
				ans=-1;break;
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		ans=-1;
		//ans=max(ans,(y-x+q-p-1)/(q-p));
		ans=max(ans,(x+p-1)/p);
		ans=max(ans,(y+q-1)/q);
		if(ans!=-1)ans=q*ans-y;
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		u=v=ans=0;
//		u=p-x%p;
//		ans+=u;y+=u;x+=u;

		while(1){
			if(y*p<x*q){
				w=x*q+p-1;
				w=w/p-y;
				v+=w,ans+=w,y+=w;
			}
			if(y*p==x*q) break;
			if(!ca){
				w=p-x%p;
				x+=w;u+=w;y+=w;ans+=w;
			}
			else {
				w=y*p+q-1;
				w=w/q-x;
				x+=w;y+=w;
				u+=w;ans+=w;
			}
			ca++;
		}
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		u=v=ans=0;
//		u=p-x%p;
//		ans+=u;y+=u;x+=u;

		while(1){
			while(y*p<x*q)v++,ans++,y++;
			if(y*p==x*q) break;
			w=p-x%p;
			x+=w;u+=w;y+=w;ans+=w;
		}
		cout<<ans<<endl;
	}

	return 0;
}


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

typedef long long LL;

const LL MAXN = 1e6+7;
const LL inf=0x3f3f3f3f;
const LL MOD = 1e9+7;
LL t,n,m,ans,tot;
LL f[MAXN];

LL exgcd(LL a,LL b,LL &x,LL &y){
	if(a==0&&b==0)return -1;
	if(b==0){
		x=1;
		y=0;
		return a;
	}
	LL d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}

int main()
{
	ios_base::sync_with_stdio(false);cin.tie(0);
	LL i,j,k,l,ca=0,u,v,w;

	LL x,y,q,p,a,b;
	cin>>t;
	while(t--){
		ca=0;
		cin>>x>>y>>p>>q;
		if(p==0){
			if(x==0)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(p==q){
			if(x==y)cout<<"0"<<endl;
			else cout<<"-1"<<endl;
			continue;
		}
		if(x%p==0&&y%q==0&&x/p==y/q){
			cout<<"0"<<endl;
			continue;
		}
		u=v=ans=0;
//		u=p-x%p;
//		ans+=u;y+=u;x+=u;

		while(1){
			if(y*p<x*q){
				w=x*q+p-1;
				w=w/p-y;
				v+=w,ans+=w,y+=w;
			}
			if(y*p==x*q) break;
			if(!ca){
				w=p-x%p;
				x+=w;u+=w;y+=w;ans+=w;
			}
			else {
				w=y*p+q-1;
				w=w/q-x;
				x+=w;y+=w;
				u+=w;ans+=w;
			}
			ca++;
			if(ca==1e5){
				ans=-1;break;
			}
		}
		cout<<ans<<endl;
	}

	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define ll long long

inline void in(ll &now)
{
	char Cget=getchar();now=0;
	while(Cget>'9'||Cget<'0') Cget=getchar();
	while(Cget>='0'&&Cget<='9')
	{
		now=now*10+Cget-'0';
		Cget=getchar();
	}
}

ll gcd(ll a,ll b)
{
	return b?gcd(b,a%b):a;
}

int main()
{
	ll t,x,y,p,q;
	in(t);
	for(;t--;)
	{
		in(x),in(y),in(p),in(q);
		p/=gcd(p,q),q/=gcd(p,q);
		if(q==p)
		{
			if(x!=y)
			{
				printf("-1\n");
				continue;
			}
		}
		if(p==0)
		{
			if(x!=0)
			{
				printf("-1\n");
				continue;
			}
		}
		ll pos=(q-y%q)%q;
		double ki=(double)p/(double)q,li,ri;
		while(1)
		{
			li=(double)x/(double)(y+pos);
			ri=(double)(x+pos)/(double)(y+pos);
			if(ki>=li&&ki<=ri) break;
			else pos+=q;
		}
		printf("%lld\n",pos);
//		cout<<pos;
//		putchar('\n');
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
ll get_gcd(ll a,ll b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t;
	ll x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		cin>>x>>y>>p>>q;
		if(!p)
		{
			if(x) puts("0");
			else puts("-1");
			return 0;
		}
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			return 0;
		}
		gcd=get_gcd(p,q);
		p/=gcd; q/=gcd;
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		cout<<n*q-y<<endl;
	}
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
ll get_gcd(ll a,ll b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t;
	ll x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		cin>>x>>y>>p>>q;
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			continue;
		}
		if(!p)
		{
			if(!x) puts("0");
			else puts("-1");
			continue;
		}
		/*gcd=get_gcd(p,q);
		p/=gcd; q/=gcd;*/
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		cout<<n*q-y<<endl;
	}
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define ll long long

inline void in(ll &now)
{
	char Cget=getchar();now=0;
	while(Cget>'9'||Cget<'0') Cget=getchar();
	while(Cget>='0'&&Cget<='9')
	{
		now=now*10+Cget-'0';
		Cget=getchar();
	}
}

ll gcd(ll a,ll b)
{
	return b?gcd(b,a%b):a;
}

int main()
{
	ll t,x,y,p,q;
	in(t);
	for(;t--;)
	{
		in(x),in(y),in(p),in(q);
		p/=gcd(p,q),q/=gcd(p,q);
		if(q==p)
		{
			if(x!=y)
			{
				printf("-1\n");
				continue;
			}
		}
		if(p==0)
		{
			if(x!=0)
			{
				printf("-1\n");
				continue;
			}
		}
		ll pos=(q-y%q)%q;
		double ki=(double)p/(double)q,li,ri;
		int l=0,r=10000000,ans;
		while(l<=r)
		{
			int mid=l+r>>1;
			li=(double)x/(double)(y+pos+(mid*q));
			ri=(double)(x+pos+mid*q)/(double)(y+pos+mid*q);
			if(ki>=li&&ki<=ri) ans=mid,r=mid-1;
			else l=mid+1;
		}
		printf("%lld\n",pos+ans*q);
//		cout<<pos;
//		putchar('\n');
	}
	return 0;
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

#define ll long long

inline void in(ll &now)
{
	char Cget=getchar();now=0;
	while(Cget>'9'||Cget<'0') Cget=getchar();
	while(Cget>='0'&&Cget<='9')
	{
		now=now*10+Cget-'0';
		Cget=getchar();
	}
}

ll gcd(ll a,ll b)
{
	return b?gcd(b,a%b):a;
}

int main()
{
	ll t,x,y,p,q;
	in(t);
	for(;t--;)
	{
		in(x),in(y),in(p),in(q);
		p/=gcd(p,q),q/=gcd(p,q);
		if(q==p)
		{
			if(x!=y)
			{
				printf("-1\n");
				continue;
			}
		}
		if(p==0)
		{
			if(x!=0)
			{
				printf("-1\n");
				continue;
			}
		}
		ll pos=(q-y%q)%q;
		double ki=(double)p/(double)q,li,ri;
		ll l=0,r=1000000000,ans;
		while(l<=r)
		{
			int mid=l+r>>1;
			li=(double)x/(double)(y+pos+(mid*q));
			ri=(double)(x+pos+mid*q)/(double)(y+pos+mid*q);
			if(ki>=li&&ki<=ri) ans=mid,r=mid-1;
			else l=mid+1;
		}
		printf("%lld\n",pos+ans*q);
//		cout<<pos+ans*q;
//		putchar('\n');
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
long long x, y, p, q;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        cin>>x>>y>>p>>q;
        if(p==q){
            printf("%d\n", x==y?0:-1);  continue;
        }
        if(p==0){
            printf("%d\n", x==0?0:-1);  continue;
        }
        long long GCD = __gcd(p, q);
        p /= GCD,   q /= GCD;
        long long n = max(ceil(x*1.0/p), ceil((y-x)*1.0/(q-p)));
        printf("%I64d\n", n*q-y);
    }
}


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int get_gcd(int a,int b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t,x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d",&x,&y,&p,&q);
		if(!p)
		{
			if(x) puts("0");
			else puts("-1");
			return 0;
		}
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			return 0;
		}
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		printf("%lld\n",1ll*n*q-y);
	}
}


#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

inline void in(int &now)
{
	char Cget=getchar();now=0;
	while(Cget>'9'||Cget<'0') Cget=getchar();
	while(Cget>='0'&&Cget<='9')
	{
		now=now*10+Cget-'0';
		Cget=getchar();
	}
}

int gcd(int a,int b)
{
	return b?gcd(b,a%b):a;
}

int main()
{
	int t,x,y,p,q;
	in(t);
	for(;t--;)
	{
		in(x),in(y),in(p),in(q);
		p/=gcd(p,q),q/=gcd(p,q);
		if(q==p)
		{
			if(x!=y)
			{
				printf("-1\n");
				continue;
			}
		}
		if(p==0)
		{
			if(x!=0)
			{
				printf("-1\n");
				continue;
			}
		}
		int pos=(q-y%q)%q;
		double ki=(double)p/(double)q,li,ri;
		while(1)
		{
			li=(double)x/(double)(y+pos);
			ri=(double)(x+pos)/(double)(y+pos);
			if(ki>=li&&ki<=ri) break;
			else pos+=q;
		}
		printf("%d\n",pos);
	}
	return 0;
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
ll get_gcd(ll a,ll b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t;
	ll x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		cin>>x>>y>>p>>q;
		if(!p)
		{
			if(x) puts("0");
			else puts("-1");
			continue;
		}
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			continue;
		}
		gcd=get_gcd(p,q);
		p/=gcd; q/=gcd;
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		cout<<n*q-y<<endl;
	}
}


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int get_gcd(int a,int b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t,x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d",&x,&y,&p,&q);
		if(!p)
		{
			if(x) puts("0");
			else puts("-1");
			continue;
		}
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			continue;
		}
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		printf("%lld\n",1ll*n*q-y);
	}
}


#include<cstdio>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
#define ll long long
ll get_gcd(ll a,ll b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t;
	ll x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		cin>>x>>y>>p>>q;
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			continue;
		}
		if(!p)
		{
			if(x) puts("0");
			else puts("-1");
			continue;
		}
		gcd=get_gcd(p,q);
		p/=gcd; q/=gcd;
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		cout<<n*q-y<<endl;
	}
}


#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int get_gcd(int a,int b)
{
	return !b ? a:get_gcd(b,a%b);
}
int main()
{
	int t,x,y,p,q,gcd,n;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d%d%d",&x,&y,&p,&q);
		if(!p)
		{
			if(x) puts("0");
			else puts("-1");
			return 0;
		}
		if(p==q)
		{
			if(x==y) puts("0");
			else puts("-1");
			return 0;
		}
		gcd=get_gcd(p,q);
		p/=gcd; q/=gcd;
		n=max(ceil(x*1.0/p),ceil((y-x)*1.0/(q-p)));
		printf("%lld\n",1ll*n*q-y);
	}
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/

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

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
    	long long x, y, p, q;
    	scanf("%lld %lld %lld %lld",&x,&y,&p,&q);
    	if(q - p == 0) cout << -1 << endl;
    	else {
    		long long m = ((y - x + q - p - 1) / (q - p));
    		long long mult = max(m, max((x+p-1)/p, (y+q-1)/q));
    		cout << mult * q - y << endl;
    	}
    }
	return 0;
}


/*input
329 19913 19900
*/

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

int main()
{
    int pos, x, y;
    scanf("%d %d %d", &pos, &x, &y);
    int ans = 0;
    int tx = x;
    while(1)
    {
    	int ara[525];
    	memset(ara, 0, sizeof ara);
    	int s = x;
    	int i = (s / 50) % 475;
	    int cnt = 25;
	    while(cnt--)
	    {
	    	i = (i * 96 + 42) % 475;
	    	ara[i + 26] = 1;
	    	// cout << i + 26 << " ";
	    }
	    // cout << endl;
	    if(x>=y && ara[pos]==1) break;
	    ans ++;
	    if(ans>10007) break;
	    x += 50;
    }
    int ret = ans;
    ans = 0;
    int f = 0;
    x = tx;
    while(1)
    {
    	int ara[525];
    	memset(ara, 0, sizeof ara);
    	int s = x;
    	int i = (s / 50) % 475;
	    int cnt = 25;
	    while(cnt--)
	    {
	    	i = (i * 96 + 42) % 475;
	    	ara[i + 26] = 1;
	    	// cout << i + 26 << " ";
	    }
	    // cout << endl;
	    if(x>=y && ara[pos]==1) {
	    	ans = 0;
	    	break;
	    }
	    ans ++;

	    x -= 50;
	    if(x<y || ans>10006) {f = 1; break;}
    }
    if(f) ans = ret;
    else ans = min(ans, ret);
    if(ans&1) printf("%d\n", ans/2+1);
    else printf("%d\n", ans/2);
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
1 5 0 6
*/

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second

typedef pair<long long, long long> pii;

pii extendedEuclid(long long a, long long b) { // returns x, y | ax + by = gcd(a,b)
  if(b == 0) return pii(1ll, 0ll);
  else {
    pii d = extendedEuclid(b, a % b);
    return pii(d.y, d.x - d.y * (a / b));
  }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
    	long long m, n, p, q;
    	scanf("%lld %lld %lld %lld",&m,&n,&p,&q);
    	long long c = m*q-n*p;
    	int ans = 0;
    	long long i;
    	if(p==0){
    		while(1);
    		printf("%d\n", p==m?(n<=q? q - n : -1):-1);
    		continue;
    	}
    	// for(i = 0; i<100005; i++)
    	// 	if(p*i>=c && (p*i - c) % q == 0 && (p*i - c) / q <=i){
    	// 		ans = i;
    	// 		break;
    	// 	}
    	long long g = __gcd(p, q);
    	pii x = extendedEuclid(p/g, -q/g);
    	// cout << p/g << " * " << -q/g << endl;
    	// cout << x.first << " " << x.second << endl;
    	x.first *= c, x.second*=c;
    	if(x.y<0){
    		long long mult = p/g;
    		long long xx = ((-x.y + mult -1)/mult);
    		x.x += xx * (q/g);
    		x.y += xx * (p/g);
    	}
    	if(x.y>0){
    		long long mult = p/g;
    		long long xx = ((x.y)/mult);
    		x.x -= xx * (q/g);
    		x.y -= xx * (p/g);
    	}
    	if(x.x<0)
    	{
    		x.x = -x.x;
    	}
    	ans = x.x;
    	if(p==q && n!=m) ans = -1;
    	printf("%d\n", ans);
    }
	return 0;
}


/*input
1
1 5 0 6
*/

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second

typedef pair<long long, long long> pii;

pii extendedEuclid(long long a, long long b) { // returns x, y | ax + by = gcd(a,b)
  if(b == 0) return pii(1ll, 0ll);
  else {
    pii d = extendedEuclid(b, a % b);
    return pii(d.y, d.x - d.y * (a / b));
  }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
    	long long m, n, p, q;
    	scanf("%lld %lld %lld %lld",&m,&n,&p,&q);
    	long long c = m*q-n*p;
    	int ans = 0;
    	long long i;
    	if(p==0){
    		printf("%d\n", p==m?0:-1);
    		continue;
    	}
    	// for(i = 0; i<100005; i++)
    	// 	if(p*i>=c && (p*i - c) % q == 0 && (p*i - c) / q <=i){
    	// 		ans = i;
    	// 		break;
    	// 	}
    	long long g = __gcd(p, q);
    	pii x = extendedEuclid(p/g, -q/g);
    	// cout << p/g << " * " << -q/g << endl;
    	// cout << x.first << " " << x.second << endl;
    	x.first *= c, x.second*=c;
    	if(x.y<0){
    		long long mult = p/g;
    		long long xx = ((-x.y + mult -1)/mult);
    		x.x += xx * (q/g);
    		x.y += xx * (p/g);
    	}
    	if(x.y>0){
    		long long mult = p/g;
    		long long xx = ((x.y)/mult);
    		x.x -= xx * (q/g);
    		x.y -= xx * (p/g);
    	}
    	if(x.x<0)
    	{
    		x.x = -x.x;
    	}
    	ans = x.x;
    	if(p==q && n!=m) ans = -1;
    	printf("%d\n", ans);
    }
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
1 5 0 6
*/

#include<bits/stdc++.h>
using namespace std;
#define x first
#define y second

typedef pair<long long, long long> pii;

pii extendedEuclid(long long a, long long b) { // returns x, y | ax + by = gcd(a,b)
  if(b == 0) return pii(1ll, 0ll);
  else {
    pii d = extendedEuclid(b, a % b);
    return pii(d.y, d.x - d.y * (a / b));
  }
}

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
    	long long m, n, p, q;
    	scanf("%lld %lld %lld %lld",&m,&n,&p,&q);
    	long long c = m*q-n*p;
    	int ans = 0;
    	long long i;
    	if(p==0){
    		printf("%d\n", p==m?(n<=q? q - n : -1):-1);
    		continue;
    	}
    	// for(i = 0; i<100005; i++)
    	// 	if(p*i>=c && (p*i - c) % q == 0 && (p*i - c) / q <=i){
    	// 		ans = i;
    	// 		break;
    	// 	}
    	long long g = __gcd(p, q);
    	pii x = extendedEuclid(p/g, -q/g);
    	// cout << p/g << " * " << -q/g << endl;
    	// cout << x.first << " " << x.second << endl;
    	x.first *= c, x.second*=c;
    	if(x.y<0){
    		long long mult = p/g;
    		long long xx = ((-x.y + mult -1)/mult);
    		x.x += xx * (q/g);
    		x.y += xx * (p/g);
    	}
    	if(x.y>0){
    		long long mult = p/g;
    		long long xx = ((x.y)/mult);
    		x.x -= xx * (q/g);
    		x.y -= xx * (p/g);
    	}
    	if(x.x<0)
    	{
    		x.x = -x.x;
    	}
    	ans = x.x;
    	if(p==q && n!=m) ans = -1;
    	printf("%d\n", ans);
    }
	return 0;
}


/*input
4
3 10 1 2
7 14 3 8
20 70 2 7
5 6 1 1
*/

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

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
    	long long x, y, p, q;
    	scanf("%lld %lld %lld %lld",&x,&y,&p,&q);
    	if(q - p == 0 || p==0 || q==0) cout << -1 << endl;
    	else {
    		long long m = ((y - x + q - p - 1) / (q - p));
    		long long mult = max(m, max((x+p-1)/p, (y+q-1)/q));
    		cout << mult * q - y << endl;
    	}
    }
	return 0;
}


/*input
4
0 5 0 6
7 14 3 8
20 70 2 7
5 6 1 1
*/

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

int main()
{
    int t;
    scanf("%d", &t);
    while(t--)
    {
    	long long x, y, p, q;
    	scanf("%lld %lld %lld %lld",&x,&y,&p,&q);
    	if(p==0 && x==0) cout << 0 << endl;
    	else if(q - p == 0 || p==0 || q==0) cout << -1 << endl;
    	else {
    		long long m = ((y - x + q - p - 1) / (q - p));
    		long long mult = max(m, max((x+p-1)/p, (y+q-1)/q));
    		cout << mult * q - y << endl;
    	}
    }
	return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int n;
	cin >> n;
	int *a= new int[n];
	for(int i=0; i<n; i++) cin >> a[i];

	int *left_range=new int[n];
	int *right_range=new int[n];

	int it=n-1;
	while(it>=0)
	{
		int it_temp=it;
		while(it_temp-1>=0 && a[it_temp]>=a[it_temp-1]) it_temp--;

		for(int i=it_temp; i<=it; i++) left_range[i]=i-it_temp+1;
		it=it_temp-1;
	}

	it=0;
	while(it<=n-1)
	{
		int it_temp=it;
		while(it_temp+1<=n-1 && a[it_temp]>=a[it_temp+1]) it_temp++;

		for(int i=it; i<=it_temp; i++) right_range[i]=it_temp-i+1;
		it=it_temp+1;
	}

	int _max=0;
	for(int i=0; i<n; i++)
	{
		if(_max<right_range[i]+left_range[i]-1) _max=right_range[i]+left_range[i]-1;
	}

	cout << _max;
	delete[] left_range, right_range, a;
	return 0;
}


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

typedef long long ll;
#define sz(v) (int)v.size()

int main() {
	int n;
	cin >> n;
	vector<int> v(n + 2);
	for (int i = 1; i <= n; i++)
		cin >> v[i];
	int mx = 0;
	for (int i = 1; i <= n; i++) {
		int l = i, r = i, cl = 0, cr = 0;
		while (--l >= 1 && v[l] <= v[l + 1])
			cl++;
		while (++r <= n && v[r] <= v[r - 1])
			cr++;
		mx = max(mx, cl + cr + 1);
	}
	cout << mx << endl;
}


#include<bits/stdc++.h>

using namespace std;

#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)
#define fff(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)
#define ford(i, n, x) for (int i = (int)(n), _x = (int)(x); i >= _x; i--)
#define fmp(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)
#define len(x) (int((x).size()))
#define pb push_back
#define fx(x) fixed<<setprecision(x)
#define mkp make_pair
#define all(X) (X).begin(),(X).end()
#define bug     cerr << ""
#define see(x) cerr << #x << "= " << x << endl
#define clr(a, b) memset(a, b, sizeof(a))

#define fi first
#define se second

typedef long long ll;
typedef pair<int, int> pii;

const int NN = 1e5 + 20;
const double pi = acos(-1.) ;
const int mod = 1000003;

template <class T> inline void smin(T &a, T b){ if(b<a) a = b;}
template <class T> inline void smax(T &a, T b){ if(a<b) a = b;}
///////////////////////////////////////////////////////////////

int a[1<<11];

int main() {
    ios_base::sync_with_stdio(false); cin.tie(0);
    int n; cin >> n;
    fff(i, 1, n+1) cin >> a[i];
    int maxi=1;
    fff(i, 1, n+1) {
        int mid = i;
        int lower, upper; lower = upper = mid;
        while(lower-1 >= 1 && a[lower] >= a[lower-1]) {
            lower--;
        }
        while(upper+1 <= n && a[upper] >= a[upper+1]) {
            upper ++;
        }
        smax(maxi, upper - lower+1);
    }
    printf("%d\n",maxi);
    return 0;
}


//4306556	 Aug 18, 2013 7:48:28 AM	fuwutu	 66B - Petya and Countryside	 GNU C++0x	Accepted	30 ms	0 KB
#include <cstdio>

int main()
{
    int n, height[1000], left[1000] = {0}, right[1000] = {0};
    scanf("%d", &n);
    for (int i = 0; i < n; ++i)
    {
        scanf("%d", &height[i]);
    }

    for (int i = 1; i < n; ++i)
    {
        left[i] = (height[i] >= height[i-1] ? left[i-1] + 1 : 0);
    }
    for (int i = n - 2; i >= 0; --i)
    {
        right[i] = (height[i] >= height[i+1] ? right[i+1] + 1 : 0);
    }

    int maximal(0);
    for (int i = 0; i < n; ++i)
    {
        if (left[i] + right[i] + 1 > maximal)
        {
            maximal = left[i] + right[i] + 1;
        }
    }
    printf("%d\n", maximal);
    return 0;
}


#include <iostream>

int main() {
  int n;
  std::cin >> n;

  int a[1000];
  for (int i = 0; i < n; ++i) std::cin >> a[i];

  int ans = 0;
  for (int i = 0; i < n; ++i) {
    int cnt = 1;
    for (int j = i - 1; j >= 0 && a[j] <= a[j + 1]; --j) ++cnt;
    for (int j = i + 1; j < n && a[j] <= a[j - 1]; ++j) ++cnt;

    if (cnt > ans) ans = cnt;
  }

  std::cout << ans;
}


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

typedef long long ll;
#define sz(v) (int)v.size()

int main() {
    int n;
    cin >> n;
    vector<int> v(n + 2);
    for (int i = 1; i <= n; i++)
        cin >> v[i];
    int mx = 0;
    for (int i = 1; i <= n; i++) {
        int l = i, r = i, cl = 0, cr = 0;
        while (--l >= 1 && v[l] <= v[l + 1])
            cl++;
        while (++r <= n && v[r] <= v[r - 1])
            cr++;
        mx = max(mx, cl + cr + 1);
    }
    cout << mx << endl;
}


#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <string>
#include <string.h>
#include <ctype.h>
using namespace std;
int main()
{

    int cas[1010];
    int n,m;
    while(cin>>m>>n&&m+n)
    {
        bool jug=true;
        memset(cas,0,sizeof(cas));
        string s;
        for(int i=0;i<m;i++)
        {
            cin>>s;
            int line=s.find('S')-s.find('G');
            if(line<0) jug=false;
            else cas[line]=1;
        }
        if(jug) {
            int max=0;
            for(int i=0;i<n;i++)
            max+=cas[i];
            cout<<max<<endl;
        }
        else cout<<"-1"<<endl;
    }
    return 0;
}


#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <bitset>
#include <utility>
#include <set>
#include <math.h>
using namespace std;

#define clr(x) memset((x), 0, sizeof(x))
#define all(x) (x).begin(), (x).end()
#define pb push_back

#define sz size()
#define RP(i, st, en) for(int i=(st); i<=(int)(en); i++)
#define RPD(i, st, en) for(int i=(st); i>=(int)(en); i--)
#define REP(i, n) for(int i=0; i<(int)(n); i++)
#define REPD(i, n) for(int i=(n)-1; i>=0; i--)

vector< int > help;
int main()
{
    int an,n,m,k,l,cc=0;
    cin>>m;
    int gr[m],ans[m],lp[m];
    REP(q,m)cin>>ans[q];
    REP(i,m)
    {an=1;
        k=i;
        while(k>=1){
            if(ans[k]>=ans[k-1]){
                    an++;
            --k;
            }
            else break;}
    l=i;
    while(l<(m-1)){
        if(ans[l]>=ans[l+1]){an++;++l;}
        else break;
    }
    gr[i]=an;

    }
    n=1;
     //REP(i,m)cout<<gr[i]<<endl;
    REP(i,m)n=max(gr[i],n);
    cout<<n;

}


#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <cctype>
#include <cstdio>
#include <iomanip>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <climits>
#include <complex>
#include <numeric>
#include <valarray>
#include <iostream>
#include <memory.h>
#include <algorithm>
using namespace std;
#define all(a) a.begin(),a.end()
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i(0),_n(n);i<_n;++i)
#define repi(i,a,b) for(int i(a),_b(b);i<_b;++i)
template<class T> int len(const T&c){return (int)c.size();}
typedef long long ll;
#define repe(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
typedef vector<int> vi;
typedef vector<string> vs;
#define mem(x,a) memset(x,a,sizeof(x))
template<class T> void cmin(T &a,T b){if(b<a) a=b;}
template<class T> void cmax(T &a,T b){if(b>a) a=b;}

int main(){
    int n;
    cin>>n;
    vi v(n);
    rep(i,n){
        cin>>v[i];
    }

    int ans=0,l,r,cur;

    rep(i,n){

        cur=1;
        l=i-1;
        r=i+1;

        while(r<n&&v[r]<=v[r-1]){
            cur++;
            r++;

        }
        while(l>=0&&v[l]<=v[l+1]){
            cur++;
            l--;
        }

        ans=max(ans,cur);

    }

    cout<<ans;

}


# include <iostream>
# include <string>
# include <cstring>
# include <cstdlib>
# include <cstdio>
# include <ctime>
# include <clocale>
# include <fstream>
# include <sstream>
# include <memory>
# include <cmath>
# include <deque>
# include <list>
# include <set>
# include <map>
# include <stack>
# include <queue>
# include <iomanip>
# include <algorithm>
# include <iterator>

# define mp make_pair
# define fi first
# define se second

using namespace std;

typedef long long  ll;

ll n,a[11111],res=0;
ll maxx=-1;
bool u[1111];

ll rec(ll v)
{
    u[v]=true;
    ++res;
    if (v-1>=0&&a[v-1]<=a[v]&&u[v-1]==0)
        rec(v-1);
    if (v+1<n&&a[v+1]<=a[v]&&u[v+1]==0)
        rec(v+1);
    return res;
}

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

    cin>>n;
    for(ll i=0;i<n;i++)
        cin>>a[i];
    for(ll i=0;i<n;i++)
    {
        res=0;
        memset(u,0,sizeof(u));
        maxx=max(maxx,rec(i));
    }
    cout<<maxx;
    return 0;
}


#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main()
{
    int n;
    cin >> n;
    vector <int> vec(n+5);
    for (int i = 0; i < n ; i ++)
        cin >> vec[i];
    int max1 = 0;
    for (int i = 0; i < n ; i ++)
    {
        int k1 = 0;
        int t1 = vec[i];
        for (int j = i + 1; j < n; j++)
            if (t1 >= vec[j])
            {
                    k1++;
                    t1 = vec[j];
            }
            else break;
        int k2 = 0;
        t1 = vec[i];
        for (int j = i-1; j >= 0; j--)
            if (t1 >= vec[j])
            {
                    k2++;
                    t1 = vec[j];
            }
            else break;
        if (max1 < (k1 + k2 + 1)) max1 = k1 + k2 + 1;

    }
    cout << max1;
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
	int n;
	cin>>n;
	int *A = new int[n];
	for(int i=0; i<n; i++)
	{
		cin>>A[i];
	}
	int result=0;
	int maxR=-999999;
	int t;
	int t1;
	for(int i=0; i<n; i++)
	{
		t=i;
		t1=i;
		for(int j=i; j<n; j--)
		{
			if(j==0)
			{
				break;
			}
			if(A[t1]<A[j-1])
			{
				break;
			}
			result++;
			t1--;
		}
		for(int i1=i; i1<n; i1++)
		{
			if(i1==n-1)
			{
				break;
			}
			if(A[i1+1]>A[t])
			{
				break;
			}
			result++;
			t++;
		}
		if(maxR<result)
		{
			maxR=result;
		}
		result=0;
	}
	cout<<maxR+1<<endl;
	return 0;
}


#include <cstdio>
#include <cstring>
#include <utility>
#include <algorithm>
using namespace std;

int main(){
    int n,a[1002],l[1002],r[1002];
    scanf("%d",&n);
    a[0]=a[n+1]=10000000;
    int ans=0;
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    for(int i=1;i<=n;i++) if(a[i]>=a[i-1]) l[i]=l[i-1]+1; else l[i]=0;
    for(int i=n;i>=1;i--) if(a[i]>=a[i+1]) r[i]=r[i+1]+1; else r[i]=0;
    for(int i=1;i<=n;i++) ans=max(ans,l[i]+r[i]+1);
    printf("%d\n",ans);
}


#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;

#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define ford(i, n) for (int i = int(n); i--; )
#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define all(a) (a).begin(), (a).end()

template<class T>
ostream& operator<< (ostream& fout, const vector<T>& vec) {
    for (size_t i = 0; i < vec.size(); ++i) {
        fout << vec[i] << ' ';
    }
    return fout;
}

template <class T>
istream& operator>> (istream& fin, vector<T>& vec) {
    for (size_t i = 0; i < vec.size(); ++i) {
        cin >> vec[i];
    }
    return fin;
}

int main() {
    int n;
    cin >> n;
    vi in(n);
    cin >> in;
    int mx = 1;
    for (int i = 0; i < n; ++i) {
        int cur = 1;
        for (int j = i + 1; j < n; ++j) {
            if (in[j] <= in[j-1]) {
                cur++;
            } else {
                break;
            }
        }
        for (int j = i - 1; j >= 0; --j) {
            if (in[j] <= in[j+1]) {
                cur++;
            } else {
                break;
            }
        }
        mx = max(cur, mx);
        cur = 1;
    }
    cout << mx << endl;
    return 0;
}


#include <stdio.h>
int main() {
    int n, N[1000], max=0, watered=0;
    scanf("%d\n", &n);
    for(int i=0; i<n; i++) {
        scanf("%d ", &N[i]);
        if(max < N[i]) max = N[i];
    }
    for(int i=0; i<n; i++) {
        if(N[i] == max) {
            int twatered = 0;
            for(int k=i; k<n-1; k++)
                if(N[k] >= N[k+1])
                    ++twatered;
                else break;
            for(int k=i; k>0; k--)
                if(N[k-1] <= N[k]) ++twatered;
                else break;
            ++twatered;//count i
            if(twatered > watered) watered = twatered;
        }
    }
    printf("%d", watered);
    return 0;
}


#include <stdio.h>
int main() {
    int n, N[1000], max=0, watered=0;
    scanf("%d\n", &n);
    for(int i=0; i<n; i++) {
        scanf("%d ", &N[i]);
        if(max < N[i]) max = N[i];
    }
    for(int i=0; i<n; i++) {
            int twatered = 0;
            for(int k=i; k<n-1; k++)
                if(N[k] >= N[k+1])
                    ++twatered;
                else break;
            for(int k=i; k>0; k--)
                if(N[k-1] <= N[k]) ++twatered;
                else break;
            ++twatered;//count i
            if(twatered > watered) watered = twatered;
    }
    printf("%d", watered);
    return 0;
}


#include <stdio.h>
int main() {
    int n, N[1000], max=0, watered=0;
    scanf("%d\n", &n);
    for(int i=0; i<n; i++) {
        scanf("%d ", &N[i]);
        if(max < N[i]) max = N[i];
    }
    for(int i=0; i<n; i++) {
            int twatered = 0;
            for(int k=i; k<n-1 && N[k] >= N[k+1]; k++)
                ++twatered;
            for(int k=i; k>0 && N[k-1] <= N[k]; k--)
                ++twatered;
            ++twatered;//count i
            if(twatered > watered) watered = twatered;
    }
    printf("%d", watered);
    return 0;
}


#include <stdio.h>
int main() {
    int n, N[1000], max=0, watered=0;
    scanf("%d\n", &n);
    for(int i=0; i<n; i++) {
        scanf("%d ", &N[i]);
        if(max < N[i]) max = N[i];
    }
    for(int i=0; i<n; i++) {
        if(N[i] == max) {
            int twatered = 0;
            for(int k=i; ++k<n;)
                if(N[k-1] >= N[k]) ++twatered;
            for(int k=i; --k>0;)
                if(N[k-1] <= N[k]) ++twatered;
            ++twatered;//count n
            if(twatered > watered) watered = twatered;
        }
    }
    printf("%d", watered);
    return 0;
}


#include <cstdio>

int main(){

    int n(0); scanf("%d", &n);
    int *heights = new int[n];
    int *scores = new int[n];
    for(int k = 0; k < n; k++){scanf("%d", heights + k); scores[k] = 0;}

    int below(0);
    for(int k = 1; k < n; k++){
        if(heights[k - 1] <= heights[k]){scores[k] += ++below;}
        else{below = 0;}
    }

    below = 0;
    for(int k = n - 2; k >= 0; k--){
        if(heights[k] >= heights[k + 1]){scores[k] += ++below;}
        else{below = 0;}
    }

    int maxScore(0);
    for(int k = 0; k < n; k++){if(scores[k] > maxScore){maxScore = scores[k];}}
    printf("%d\n", maxScore + 1);
    return 0;
}


#include <iostream>
#define f(i,a,b) for(int i=a;i<b;i++)
int A[1005],L[1005],R[1005],N,Best=1;
using namespace std;
int main()
{
    cin>>N;
    f(i,0,N)cin>>A[i];
    f(i,1,N)L[i]=A[i]>=A[i-1]?L[i-1]:i;
    A[N]=1005;
    for(int i=N-1;i>=0;i--)R[i]=A[i]>=A[i+1]?R[i+1]:i,Best=max(Best,R[i]-L[i]+1);
    cout<<Best;
}


#include <functional>
#include <algorithm>
#include <iostream>
#include <numeric>
#include <stdio.h>
#include <string>
#include <vector>
#include <cctype>
#include <math.h>
#include <time.h>
#include <queue>
#include <stack>
#include <set>
#include <map>

using namespace std;

#define all(p) (p).begin(), (p).end()
#define fi first
#define se second

typedef long long ll;

const int N = 111111;
const int INF = int(1e9), mod = INF + 7;

int n, a[N];

int main() {

    scanf("%d", &n);

    for (int i = 1; i <= n; ++i)
        scanf("%d", a + i);

    a[0] = a[n + 1] = INF;

    int mx = 0;

    for (int i = 1; i <= n; ++i) {

        int cnt = 0, it = i;

        while(it > 0 && a[it] >= a[it - 1])
            it--, cnt++;

        it = i;

        while(it <= n && a[it] >= a[it + 1])
            it++, cnt++;

        mx = max(mx, cnt + 1);
    }

    printf("%d\n", mx);

    return 0;
}


//#define PROB "test"         /* Comment to use standard I/O everywhere    */
#define STDIO                 /* Uncomment to use standard I/O on local PC */

//{{{ Preamble
#if !defined(LOCAL_PC) && !defined(NDEBUG)
#  define NDEBUG
#endif
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <forward_list>
#include <unordered_map>
#include <unordered_set>
#ifdef LOCAL_PC
#  include <debug_output.hpp>
#else
#  define LOG(...)  static_cast<void>(0)
#endif
using namespace std; typedef long long ll; typedef unsigned long long ull;
const double pi=3.14159265358979323846; const double E=2.7182818284590452354;
static struct _{ ios_base::Init i; _(){cin.sync_with_stdio(0);cin.tie(0);} } _;
inline ostream& endl(ostream& os) { return os << '\n'; }
#if defined(PROB) && (!defined(LOCAL_PC) || !defined(STDIO))
static struct __{ __(){cin.rdbuf(a.open(PROB".in",ios::in));cout.rdbuf(b.open(
PROB".out",ios::out));} ~__(){cin.rdbuf(0);cout.rdbuf(0);} filebuf a,b; } __;
#endif
#define pb          push_back
#define sz(v)       static_cast<int>((v).size())
#define X           first
#define Y           second
//}}}

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

    vector<int> arr(n);
    for (int i = 0; i < n; i++)
        cin >> arr[i];

    vector<int> l(n), r(n);

    l[0] = 0;
    for (int i = 1; i < n; i++)
    {
        if (arr[i-1] <= arr[i])
            l[i] = l[i-1] + 1;
        else
            l[i] = 0;
    }

    r[n-1] = 0;
    for (int i = n-2; i >= 0; i--)
    {
        if (arr[i+1] <= arr[i])
            r[i] = r[i+1] + 1;
        else
            r[i] = 0;
    }

    int res = 0;
    for (int i = 0; i < n; i++)
    {
        res = max(res, l[i] + 1 + r[i]);
    }

    cout << res << endl;
}


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

int main() {
    int n, res = 0;
    cin >> n;
    vector<int>list(n);
    for(int i = 0;i < n;i++)
        cin >> list[i];
    for(int i = 0;i < n;i++)
    {
        int cnt = 0, j = i - 1;
        while(j >= 0 && list[j] == list[i])cnt++, j--;
        while(j >= 0 && list[j] <= list[j + 1])cnt++, j--;
        j = i + 1;
        while(j < n && list[j] == list[i])cnt++, j++;
        while(j < n && list[j] <= list[j - 1])cnt++, j++;
        res = max(cnt, res);
    }
    cout << res + 1;
    return 0;
}


#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <complex>
using namespace std;
typedef pair<int, int> pi;
typedef long long int li;
typedef vector<int> vi;
typedef double ld;
void solve();
#define mp make_pair
#define pb push_back

int main(){
#ifdef DEBUG
    freopen("input", "r", stdin);
    //freopen("output","w",stdout);
#endif
    solve();
    return 0;
}
void solve(){
    int n,x[1111];
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>x[i];
    }
    int mx=0;
    for(int i=0;i<n;++i){
        int l=i,r=i;
        while(l && x[l-1]<=x[l])
            --l;
        while(r<n-1 && x[r+1]>=x[r])
            ++r;
        mx=max(mx,r-l+1);
    }
    cout<<mx;
}


#include <stdio.h>
#include <iostream>
#include <set>
#include <map>
#include <string>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
#include <sstream>
#include <complex>
using namespace std;
typedef pair<int, int> pi;
typedef long long int li;
typedef vector<int> vi;
typedef double ld;
void solve();
#define mp make_pair
#define pb push_back

int main(){
#ifdef DEBUG
    freopen("input", "r", stdin);
    //freopen("output","w",stdout);
#endif
    solve();
    return 0;
}
void solve(){
    int n,x[1111];
    cin>>n;
    for(int i=0;i<n;++i){
        cin>>x[i];
    }
    int mx=0;
    for(int i=0;i<n;++i){
        int l=i,r=i;
        while(l && x[l-1]<=x[l])
            --l;
        while(r<n-1 && x[r+1]<=x[r])
            ++r;
        //cout<<l<<' '<<r<<endl;
        mx=max(mx,r-l+1);
    }
    cout<<mx;
}


// Designed by darkknight10
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;
#define max(A,B) (A)>(B)?(A):(B)
#define min(A,B) (A)<(B)?(A):(B)
#define fori(i,n) for(i=0;i<n;i++)
#define ford(i,n) for(i=n-1;i>=0;i--)
#define wh(t) while(t--)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d\n",a)
#define sc64(a) scanf("%I64d",&a)
#define pr64(a) printf("%I64d\n",a)
#define scs(a) scanf("%s",a)
#define prs(a) printf("%s\n",a)
#define ll long long int
#define MOD 1000000007
#define pb push_back
int a[1010],d[1010];
int main()
{
int n,i;
sc(n);
fori(i,n)
sc(a[i]);
if(n==1)
{

    pr(1);
}
else
{

for(i=0;i<n-1;i++)
{
d[i]=a[i+1]-a[i];
//cout<<d[i]<<endl;
}
int max=0,count=0;
for(int i=0;i<n-1;i++)
{
if(d[i]>=0)
count=count+1;
else
{
if(count>=max)
max=count+1;
count=0;
}
}
if(count>max)
    max=count;
cout<<max+1<<endl;
}
return 0;
}


// Designed by darkknight10
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;
#define max(A,B) (A)>(B)?(A):(B)
#define min(A,B) (A)<(B)?(A):(B)
#define fori(i,n) for(i=0;i<n;i++)
#define ford(i,n) for(i=n-1;i>=0;i--)
#define wh(t) while(t--)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d\n",a)
#define sc64(a) scanf("%I64d",&a)
#define pr64(a) printf("%I64d\n",a)
#define scs(a) scanf("%s",a)
#define prs(a) printf("%s\n",a)
#define ll long long int
#define MOD 1000000007
#define pb push_back
int a[1010],d[1010],e[1010];
int main()
{
int n,i;
sc(n);
fori(i,n)
sc(a[i]);
if(n==1)
{

    pr(1);
}
else
{
for(i=0;i<n-1;i++)
{
d[i]=a[i+1]-a[i];
//cout<<d[i]<<endl;
}
for(int i=n-1;i>0;i--)
{

    e[i]=a[i-1]-a[i];
}
int max1=0,count=0,max2=0;
for(int i=0;i<n-1;i++)
{
if(d[i]>=0)
count=count+1;
else
{
if(count>=max1)
max1=count+1;
count=0;
}
}
if(count>max1)
    max1=count;
count=0;
for(int i=n-1;i>0;i--)
{
if(e[i]>=0)
count=count+1;
else
{
if(count>=max2)
max2=count+1;
count=0;
}
}
if(count>max2)
max2=count;
int max;
if(max1>max2)
    max=max1;
else
    max=max2;
cout<<max+1<<endl;
}
return 0;
}


#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <memory.h>
#include <cassert>

using namespace std;
#define max(A,B) (A)>(B)?(A):(B)
#define min(A,B) (A)<(B)?(A):(B)
#define fori(i,n) for(i=0;i<n;i++)
#define ford(i,n) for(i=n-1;i>=0;i--)
#define wh(t) while(t--)
#define sc(a) scanf("%d",&a)
#define pr(a) printf("%d\n",a)
#define sc64(a) scanf("%I64d",&a)
#define pr64(a) printf("%I64d\n",a)
#define scs(a) scanf("%s",a)
#define prs(a) printf("%s\n",a)
#define ll long long int
#define MOD 1000000007
#define pb push_back
int l[1100],r[1100];
int main()
{
int n,a[1100],count1,max1;
sc(n);
for(int i=0;i<n;i++)
sc(a[i]);
if(n==1)
{
printf("1");
return 0;
}
else
{
    for(int i=1;i<n;i++)
    {
    l[i]=(a[i]>=a[i-1]?l[i-1]+1:0);
        }
        for(int i=n-2;i>=0;i--)
        {

            r[i]=(a[i]>=a[i+1]?r[i+1]+1:0);
        }
        max1=0;
        for(int i=0;i<n;i++)
        {
            if(l[i]+r[i]+1>=max1)
                max1=l[i]+r[i]+1;
        }
}
printf("%d",max1);
return 0;
}