//#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
//~ while (clock()<=69*CLOCKS_PER_SEC)

#define ll long long
#define ld long double
#define pi pair<int,int>
#define pd pair<ld,ld>
#define ft first
#define sd second
#define pb push_back
#define eb emplace_back
#define FOR(i,a,b) for(int i=(a); i<=(b);i++)
#define F(i,a,b) FOR(i,(a),(b)-1)
#define REV(i,a,b) for(int i=(a); i>=(b);i--)
#define VI vector<int>
#define VPI vector<pi>
#define VPD vector<pd>
#define PI 3.14159265
#define all(x) (x).begin(), (x).end()
#define sz(a) (int)((a).size())
#define int long long

template<class TH> void _dbg(const char *sdbg, TH h){cerr<<sdbg<<"="<<h<<"\n";}
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
    while(*sdbg!=',')cerr<<*sdbg++;cerr<<"="<<h<<","; _dbg(sdbg+1, a...);
}
#define debug(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const long long K = 1000;
const long long KK = K*K;
const long long MIL = KK*K;
const long long INF = MIL*MIL;
const long long MOD = 1e9 + 7;
const long long N = 1e5 + 10, M=310;
const int base = 1 << 18;

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

void solve() {
    int n, s, k;
    int res = 0;
    cin >> n >> s  >> k;
    if (__gcd(n, k) != 1) {
        int tmp = __gcd(n, k);
        if (s % tmp) {
            cout << "-1\n";
            return;
        }
    }
    int tmp = (n - s) / k;
    res += tmp;
    s += tmp *  k;
    s %= n;
    if (s == 0) {
        cout << res << "\n";
        return;
    }
    int x = 1, y = 1;
    gcd(n, k, x, y);
    int nww = ((n * k) / __gcd(n, k));
    while( y < 0 ) {
        x -= nww / n;
        y += nww / k;
    }
    res += (n - s) / __gcd(n, k) * y;
    s = 0;

    int cofaj = nww / k;
    res %= cofaj;

    cout << res << "\n";
}

int32_t main() {
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);

    ios_base::sync_with_stdio(0);
    cin.tie(0);
//    cout.tie(0);
    cerr.tie(0);
    cout << setprecision(9) << fixed;
    cerr << setprecision(20) << fixed;
    int test = 1, f;
    cin >> test;
    F(_test, 0, test) {
        //cout<<"Case #"<<_test + 1<<": ";
        solve();
//		if(_test == 1)
//            return 0;
    }
}
/*

 */
#define _USE_MATH_DEFINES

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

#define	INF   1010101010LL
#define INFLL 1010101010101010101LL

using namespace std;

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

long long extgcd(long long a, long long b, long long &x, long long &y)
{
	long long d = a;
	if (b != 0) {
		d = extgcd(b, a % b, y, x);
		y -= a / b * x;
	} else {
		x = 1;
		y = 0;
	}

	return d;
}

int main()
{
	int t;
	cin >> t;
	for (int i = 0; i < t; i++) {
		long long n, s, k;
		cin >> n >> s >> k;
		long long x, y;
		long long d = extgcd(k, n, x, y);
		if (s % d != 0) {
			cout << -1 << endl;
			continue;
		}

		n /= d;
		s /= d;
		k /= d;
		long long ans = ((x * -s) % n + n) % n;
		cout << ans << endl;
	}

	return 0;
}