
//#include <atcoder/maxflow.hpp>
#include <memory>
#include <iostream>
#include <map>
#include <list>
#include <set>
#include <algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <functional>
#include <queue>
#include <deque>
#include <stack>
#include <limits>
#include <unordered_map>
#include <unordered_set>
#include <cmath>
#include <fstream>
#include <iterator>
#include <random>
#include <chrono>
#include <complex>
#include <thread>

#define forr(i,start,count) for (int i = (start); i < (start)+(count); ++i)
#define set_map_includes(set, elt) (set.find((elt)) != set.end())
#define readint(i) int i; cin >> i
#define readll(i) ll i; cin >> i
#define readdouble(i) double i; cin >> i
#define readstring(s) string s; cin >> s

typedef long long ll;

using namespace std;
//using namespace atcoder;

ll modd = (1000LL * 1000LL * 1000LL + 7LL);
//ll modd = 998244353;

long long gccd(long long a, long long b) { 
    if (a == 0)  {   return b;  }
    return gccd(b % a, a); 
};

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;
}

bool find_any_solution(ll a, ll b, ll c, ll &x0, ll &y0, ll &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()   {

    ios_base::sync_with_stdio(false);

    cout.precision(17);
    mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
    uniform_int_distribution<int> rand_gen(0, modd);   // rand_gen(rng) gets the rand no

    auto start = chrono::steady_clock::now();

    readint(test_cases);
//    int test_cases = 1;
    forr(t, 1, test_cases) {
        readll(n); readll(s); readll(k);
        

        ll gg = gccd(n,k);
        if (s % gg != 0) {
            cout << -1 << endl;
            continue;
        }
        ll nn = n/gg, kk = k/gg;

        ll dummy;
        ll x,y;

        find_any_solution(kk,nn,-s/gg,x,y,dummy);

        // (nn,-kk)

        if (x>0) {
            x %= nn;
        } else {
            x %= nn;
            if (x<0) {x+=nn;}
            x %= nn;
        }
        cout << x << endl;
        
    }
//        auto stop = chrono::steady_clock::now(); 
//        auto duration = chrono::duration_cast<chrono::milliseconds>(stop - start);
//        cout << "Duration: " << duration.count() << endl;
    return 0;
}


#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N = 1e6 + 50;

int k;
ll ans;
bool vis[105], is[10];

int qpow(int x, int k){
	int ans = 1;
	while(k){
		if(k & 1)  ans = 1LL * ans * x % 4;
		x = 1LL * x * x % 4,  k >>= 1;
	}
	return ans;
}
int main(){
    int a, b, c;
    scanf("%d%d%d", &a, &b, &c);
    b = qpow(b, c);
    a %= 10;
    if(!b)  b = 4;
    for(int i = 1, da = a; i < b; ++i)  a = a * da % 10;
    printf("%d", a);
	/*b %= 100;
    int db = b;
	b = 1;
    int cnt = 0;
    bool ok = 0;
    for(int i = 1; i <= c; ++i){
        b = b * db % 100;
        if(vis[b]){
            ok = 1;
            break;
        }
        vis[b] = 1;
        ++cnt;
    }
    a %= 10;
    int tt = 0, m = 1;
    for(int i = 1; i <= 10; ++i){
    	m = m * a % 10;
		if(is[m])  break;
		is[m] = 1;
		++tt;
	}
    if(!ok){
        int da = 1;
        b %= tt;
        if(!b)  b = tt;
        for(int i = 1; i <= b; ++i)  da = da * a % 10;
        printf("%d", da);
    }
    else{
        c %= cnt;
        if(c == 0)  c = cnt;
        b = 1;
        for(int i = 1; i <= c; ++i)  b = b * db % 100;
        int da = 1;
        printf("%d\n", cnt);
        b %= tt;
        if(!b)  b = tt;
        for(int i = 1; i <= b; ++i)  da = da * a % 10;
        printf("%d", da);
    }*/
}