#include <bits/stdc++.h>

using namespace std;

#define REP(i, n)   for(int i = 0; i < (n); i++)
#define REPS(i, n)  for(int i = 1; i <= (n); i++)
#define RREP(i, n)  for(int i = (n)-1; i >= 0; i--)
#define RREPS(i, n) for(int i = (n); i > 0; i--)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define yes cout << "yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
#define no cout << "no" << endl

using ll  = long long;
using pi  = pair<int, int>;
using pl  = pair<ll, ll>;
using vi  = vector<int>;
using vl  = vector<ll>;
using vs  = vector<string>;
using vb  = vector<bool>;
using vvi = vector<vi>;
using vvl = vector<vl>;

//const int MOD = 1e9 + 7;
const int MOD = 998244353;
const int INF = 1e9 + 7;
const ll  INFL = 1e18;
const double PI = 3.141592653589793;
const double EPS = 1e-9;

template<class T> bool chmax(T &a, const T &b) { if(a < b) { a = b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if(a > b) { a = b; return true; } return false; }

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(20);

    int N; cin >> N;
    vi x(N), y(N); REP(i, N) cin >> x[i] >> y[i];
    REP(i, N)
    {
      for(int j = i+1; j < N; j++)
      {
        for(int k = j+1; k < N; k++)
        {
          double tx = x[j]-x[i];
          double ty = y[j]-y[i];
          double sx = x[k]-x[i];
          double sy = y[k]-y[i];
          if(tx == 0 && sx != 0) continue;
          if(ty == 0 && sy!= 0) continue;
          if(sx == 0 && tx != 0) continue;
          if(sy == 0 && ty!= 0) continue;
          double a = sx / tx;
          double b = sy / ty;
          if(tx == 0 || ty == 0 || sx == 0 || sy == 0)
          {
            Yes;
            return 0;
          }
          if(abs(a-b) < EPS)
          {
            Yes;
            return 0;
          }
        }
      }
    }
    No;
}
#include <bits/stdc++.h>
// #include <atcoder/all>
// using namespace atcoder;
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) x.begin(),x.end() 
#define SZ(x) ((int)(x).size())
#define PB push_back
#define MP make_pair
#define V vector
#define P pair<int,int>
#define F first
#define S second
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
using ll = long long;
using namespace std;
const int INF =1e9+1;


// 返り値: a と b の最大公約数
// ax + by = gcd(a, b) を満たす (x, y) が格納される
long long extGCD(long long a, long long b, long long &x, long long &y) {
    if (b == 0) {
        x = 1;
        y = 0;
        return a;
    }
    long long d = extGCD(b, a%b, y, x);
    y -= a/b * x;
    return d;
}
// int main() {
//     long long x, y;
//     extGCD(111, 30, x, y);
//     cout << x << ", " << y << endl;
// }


void solve(){
    int n,s,k;cin>>n>>s>>k;
    long long x,y;
    ll g=extGCD(k,n,x,y);
    if(s%g>0){
        cout<<-1<<endl;
        return ;
    }
    s/=g;
    n/=g;
    k/=g;
    ll ans=((-s)*x)%n;
    cout<<(ans+n)%n<<endl;
    return ;
}

int main() {
    int t;cin>>t;
    rep(i,t)solve();
}