#include<cstring>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<vector>
#include<cmath>
#include<map>
#include<queue>
#include<bitset>
#include<cstdlib>
#include<set>
#include<unordered_map>
#define l(x) (x<<1)
#define r(x) ((x<<1)|1)
#define low(x) (x&(-x))
#define IL inline
#define reg register
#define LL long long
#define N 1010
#define MOD 998244353
#define MO 1000000007
#define INF 0x3f3f3f3f
using namespace std;

LL T,n,s,k,i,j,x,Ans;

IL int Abs(int x){return (x<0)?-x:x;}
IL void Swap(int &a,int &b){a^=b^=a^=b;}
IL int Min(int a,int b){return (a<b)?a:b;}
IL int Max(int a,int b){return (a>b)?a:b;}

IL LL read(){
	LL p=0,f=1;	char	c=getchar();
	while (c<48||c>57)	{if (c=='-')	f=-1;	c=getchar();}
	while (c>=48&&c<=57)	p=(p<<1)+(p<<3)+c-48,c=getchar();
	return p*f;
}

IL LL Gcd(LL a,LL b){return (!b)?a:Gcd(b,a%b);}

IL void Ex_gcd(LL a,LL b,LL &x,LL &y){
	LL p=0,q=0;
	if (b==0) {
		x=1;	y=0;	return;
	}	else {
		Ex_gcd(b,a%b,p,q);
		x=q;	y=p-a/b*q;
	}
}

IL LL Inv(LL a,LL p){
	LL x=0,y=0;
	Ex_gcd(a,p,x,y);
	return x;
}

int main(){
	#ifdef __Marvolo
	freopen("zht.in","r",stdin);
	freopen("zht.out","w",stdout);
	#endif
	T=read();
	while (T--){
		n=read();	s=read();	k=read();
		s=n-s;
		if (!s)	{
			puts("0");
			continue;
		}
		if (s%Gcd(k,n)){
			puts("-1");
			continue;
		}
		x=Gcd(k,n);
		k/=x;	n/=x;	s/=x;
		x=Inv(k,n)%n;
		Ans=(x*s)%n;
		Ans%=n;
		while (Ans<0)	Ans+=n;
		cout<<Ans<<endl;
	}
	return 0;
}
#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < (int)(n); i++)

using namespace std;
using ll = long long;

int main(){
  int A, B, C, D;
  cin >> A >> B >> C >> D;
  ll n = 0;
  if( B >= C*D ) n = -1;
  else{
    double m = C*D - B;
    double r = A / m;
    n = ceil(r);
  }
  cout << n;
  return 0;
}