#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp> 
#include <ext/pb_ds/detail/standard_policies.hpp>
using namespace __gnu_pbds;
using namespace std;
#define getbit(n, i) (((n) & (1LL << (i))) != 0) 
#define setbit0(n, i) ((n) & (~(1LL << (i)))) 
#define setbit1(n, i) ((n) | (1LL << (i))) 
#define togglebit(n, i) ((n) ^ (1LL << (i))) 
#define lastone(n) ((n) & (-(n))) 
char gap = 32;
template<typename T> 
ostream& operator<<(ostream &os, const vector<T> &v) { 
    os << '{'; 
    for (const auto &x : v) os << gap << x;
        return os << '}'; 
}
template<typename A, typename B> 
ostream& operator<<(ostream &os, const pair<A, B> &p) {
        return os << '(' << p.first << gap << p.second << ')';
}

template <class T>
void read(T &x) {
    char c;
    for (c = getchar(); c < '0' || c > '9'; c = getchar())
        ;
    for (x = 0; c <= '9' && c >= '0'; c = getchar()) x = x * 10 + (c & 15);
}
#define ll long long 
#define lll __int128_t
#define pb push_back
#define mp make_pair
typedef pair < int,int> ii;
typedef vector<ii> vii;
typedef vector<ll>vl;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef tree<
int,
null_type,
less<int>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll hashPrime = 1610612741;
#define mod 1000000007
long gcdExtended(long a, long b, long *x, long *y) 
    { 
        // Base Case 
        if (a == 0) 
        { 
            *x = 0, *y = 1; 
            return b; 
        } 

        long x1, y1; // To store results of recursive call 
        long gcd = gcdExtended(b%a, a, &x1, &y1); 


        *x = y1 - (b/a) * x1; 
        *y = x1; 

        return gcd; 
    } 
long modInverse(long a, long m) 
    { 
        long x, y; 
        long g = gcdExtended(a, m, &x, &y); 

            long res = (x%m + m) % m; 
        return res; 
    } 
void solve(){
    ll i,j,k,l,n,m,s;
    cin>>n>>s>>k;
    ll kola = n-s;
    ll GCD=__gcd(__gcd(kola,k),n);
    kola/=GCD;
    k/=GCD;
    n/=GCD;
    if(__gcd(k,n)!=1){
        cout<<"-1\n";
        return;
    }
    ll ans = (kola*modInverse(k,n))%n;
    cout<<ans<<"\n";
}
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    ll t;
    cin>>t;
    while(t--){
        solve();
    }
    return 0;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <cmath>
#include <string>
#include <sstream>
#include <set>
#include <functional>
#include <map>
#include <unordered_map>
#include <queue>
#include <iomanip>
#include <stack>
using namespace std;
typedef long long ll;
#define repi(i,a,b) for(ll i=a;i<b;i++)
#define rep(i,a) repi(i,0,a)
#define all(u) u.begin(),u.end()
using Interval = pair<ll,ll>;
using Graph = vector<vector<ll> >;
#define fi first
#define se second

struct Edge{
    ll to;
    ll w;
    Edge(ll to, ll w):to(to),w(w){}
};

using wGraph = vector<vector<Edge> >;


template<class T> void chmax(T& a, T b){
    if (a < b){
        a=b;
    }
}

template<class T> void chmin(T& a, T b){
    if (a > b){
        a=b;
    }
}

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

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

const ll INF =  998244353;

string N;

int main(){
    cin >> N;
    ll add = 0;
    
    while(true){
        bool ok = true;
        rep(i,N.size()/2){
            if (N[i] != N[N.size()-1-i]){
                ok = false;
            }
        }
        if (ok){
            cout << "Yes" << endl;
            return 0;
        }
        else{
            N = "0"+N;
            add++;
        }
        if (add >= 15) break;
    }
    cout << "No" << endl;
    return 0;
}