#include<bits/stdc++.h>
#include<iostream>
#include<string>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<cstring>
#include<iomanip>
#include<cstdlib>
#include<ctime>
#include<set>
#include<map>
#include<utility>
#include<queue>
#include<vector>
#include<stack>
#include<sstream>
#include<algorithm>
#define forn(i,a,b)for(int i=(a),_b=(b);i<=_b;i++)
#define fore(i,b,a)for(int i=(b),_a=(a);i>=_a;i--)
#define rep(i,n)for(int i=0,_n=n;i<n;i++)
#define ll long long
#define pii pair<int,int>
#define vi vector<int>
#define vpii vector<pii>
#define m_p make_pair
#define re return
#define pb push_back
#define si set<int>
#define ld long double
#define X first
#define Y second
#define st string
#define ull unsigned long long
#define mod 1000000007
#define INF 1000000007
#define x1 XZVJDFADSPFOE
#define y1 GASDIJSLDAEJF
#define x2 DFDAJKVOHKWIW
#define y2 PSFSAODSXVNMQ
#define LLINF 0x3f3f3f3f3f3f3f3fLL
#define foreach(i,c) for(__typeof(c.begin())i=(c.begin());i!=(c).end();i++)
using namespace std;
inline void read(int &x)
{
	short negative=1;
    x=0;
    char c=getchar();
    while(c<'0' || c>'9')
    {
		if(c=='-')
			negative=-1;
		c=getchar();
	}
    while(c>='0' && c<='9')
        x=(x<<3)+(x<<1)+(c^48),c=getchar();
    x*=negative;
}
ll quickpower(ll n,ll k){
	ll ans=1;
	while(k){
		if(k%2){
			ans*=n;
			ans%=mod;
		}
		n*=n;
		n%=mod;
		k/=2;
	}
	return ans;
}
string int_to_string(int n)
{
	string s="";
	while(n)
	{
		int now=n%10;
		s+=now+'0';
		n/=10;
	}
	reverse(s.begin(),s.end());
	return s;
}
int string_to_int(string s)
{
	int n=0;
	rep(i,s.size())
	{
		n*=10;
		n+=s[i]-'0';
	}
	return n;
}
int n;
int x[1010],y[1010];
int main()
{
	ios::sync_with_stdio(0);
	cin>>n;
	rep(i,n)cin>>x[i]>>y[i];
	int ans=0;
	rep(i,n)
	{
		rep(j,i)
		{
			if(abs(y[j]-y[i])<=abs(x[j]-x[i]))ans++;
		}
	}
	cout<<ans<<endl;
	return 0;
}

#include <bits/stdc++.h>

using namespace std;

void fastio() {
	cin.tie(nullptr);
	cin.sync_with_stdio(false);
}

using LL = long long;
using LD = long double;

const LL MOD = 1e9+7;
const LL INF = LLONG_MAX;
const LL N = 101;

#define F first
#define S second
#define vt vector

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

int main() {
	fastio();

	LL T;
	cin >> T;

	while (T--) {
		LL n, s, k;
		cin >> n >> s >> k;

		if (s % __gcd(k, n) != 0) {
			cout << "-1\n";
			continue;
		}
		LL xg, yg;

		LL gc = __gcd(k, n);
		LL k1 = k/gc, n1 = n/gc, s1 = s/gc;

		gcd(k, n, xg, yg);

		LL x0 = -xg * s1;
		LL y0 = yg * s1;

		//cerr << x0 << " " << y0 << "??\n";
		
		if (x0 < 0) {
			// adjust y and x
			LL mx = max(-x0/n1, -y0/k1);
			x0 += mx*n1;
			y0 += mx*k1;

			if (x0 < 0 || y0 < 0) {
				x0 += n1;
				y0 += k1;
			}
		}
		else {
			x0 %= n1;
		}

		cout << x0 << "\n";
	}
}
