#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int t;
ll n, s, k;
ll powmod (ll a, ll b) {
  if (b == 0) return 1;
  if (b == 1) return a % n;
  ll tmp = powmod(a, b / 2);
  tmp = (tmp * tmp) % n;
  if (b & 1) return (tmp * a) % n;
  return tmp;
}
ll phi (ll x) {
  ll res = x;
  for (ll i = 2; i * i <= x; ++ i) {
    if (x % i == 0) {
      res -= res / i;
      while (x % i == 0) x /= i;
    }
  }
  if (x > 1) res -= res / x;
  return res;
}
ll modInv (ll x) {
  ll phin = phi(n);
  return powmod(x, phin - 1);
}
void solve () {
  cin >> n >> s >> k;
  ll A = k, B = (n - s) % n, M = n;
  ll g = __gcd(A, __gcd(B, M));
  A /= g; B /= g; M /= g; n /= g;
  if (__gcd(A, M) != 1) {
    cout << -1 << '\n';
    return;
  }
  ll ans = (modInv(A) * B + M) % M;
  cout << ans << "\n";
}
int main () {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL); cout.tie(NULL);
  cin >> t;
  while (t --) solve();
  return 0;
}

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <deque>
#include <stack>
#include <queue>
#include <array>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <random>
#include <chrono>
#include <utility>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <sstream>
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <functional>
#include <unordered_map>
using namespace std;

void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(bool x) {cerr << (x ? "true" : "false");}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}

template<typename T, typename V> void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i : x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";} void _print() {cerr << "]\n";}
template <typename T, typename... V>void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef HOME
#warning CHECK int:ll::INT_MAX:LLONG_MAX
#define maxn 20
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define maxn 2000006
#define gcd __gcd
#define debug(x...)
#endif

#define ff first
#define endl '\n'
#define ss second
#define inf 0x3f3f3f3f
#define MOD 1000000007
#define PI 3.14159265358979323846264338327950L
#define f(i,x,n) for(int i=x;i<=n;i++)
#define fr(i,x,n) for(int i=x;i>=n;i--)
struct _ { ios_base::Init i; _() { ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL); } } _;

int dx[] = { -1, 0, 1, 0, -1, -1, 1, 1};
int dy[] = {0, 1, 0, -1, -1, 1, 1, -1};

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

bool find_any_solution(long long a, long long b, long long c, long long &x0, long long &y0, long long &g) {
	g = gcd(abs(a), abs(b), x0, y0);
	if (c % g) return false;

	x0 *= c / g;
	y0 *= c / g;
	if (a < 0) x0 = -x0;
	if (b < 0) y0 = -y0;
	return true;
}

int main() {

	long long test; cin >> test;
	for (long long tst = 1; tst <= test; ++tst) {
		long long n, s, k; cin >> n >> s >> k;

		long long g, x0, y0;

		if (!find_any_solution(-k, n, s, x0, y0, g)) {
			cout << -1 << "\n";
			continue;
		}

		long long dec = n / g;

		if (x0 > 0) x0 -= (x0 / dec) * dec;
		else x0 += ((-x0 + dec - 1) / dec) * dec;

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

	return 0;
}