#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define PI 3.14159265
template<class T> bool ckmin(T& a, const T& b) { return b < a ? a = b, 1 : 0; }
template<class T> bool ckmax(T& a, const T& b) { return a < b ? a = b, 1 : 0; }
//mod Inverse
long long modInverse(long long a, long long m)   //m and a should be coprime.
{
    long long m0 = m;
    long long y = 0, x = 1;
    if (m == 1)
      return -1;
    while (a > 1)
    {
        long long q = a / m;
        long long t = m;
        m = a % m, a = t;
        t = y;
        y = x - q * y;
        x = t;
    }
    if (x < 0)
       x += m0;
    return x;
}
// HERE a = numerator, b = denominator, m = mod;
long long modDivide(long long a, long long b, int m)
{
    a = a % m;
    long long inv = modInverse(b, m);
    if (inv == -1)
       return -1;
    else    {
        long long ans = (inv * a) % m;
        return ans;
    }
}
int main()  {
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);
    ll t;
    cin >> t;
    while(t--)  {
        ll n, s, k;
        cin >> n >> s >> k;
        ll g = __gcd(k, n);
        if(s%g != 0)    {
            cout << "-1" << "\n";
        }
        else    {
            n /= g, k /= g, s /= g;
            ll a = n-s;
            ll b = k;
            cout << modDivide(a, b, n) << "\n";
        }
    }
    
}
    

#define _USE_MATH_DEFINES

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <complex>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#include <queue>

#define	INF   1010101010LL
#define INFLL 1010101010101010101LL

using namespace std;

const int mod = 1000000007;
//const int mod = 998244353;

int main()
{
	int n;
	cin >> n;
	vector<int> a(n), p(n), x(n);
	for (int i = 0; i < n; i++) {
		cin >> a[i] >> p[i] >> x[i];
	}

	int mn = INF;
	for (int i = 0; i < n; i++) {
		if (x[i] - a[i] > 0) {
			mn = min(mn, p[i]);
		}
	}
	cout << (mn == INF ? -1 : mn) << endl;

	return 0;
}