Problem: There is an infinite set generated as follows:

  * 1 is in this set. 
  * If x is in this set, x ⋅ a and x+b both are in this set. 



For example, when a=3 and b=6, the five smallest elements of the set are:

  * 1, 
  * 3 (1 is in this set, so 1⋅ a=3 is in this set), 
  * 7 (1 is in this set, so 1+b=7 is in this set), 
  * 9 (3 is in this set, so 3⋅ a=9 is in this set), 
  * 13 (7 is in this set, so 7+b=13 is in this set). 



Given positive integers a, b, n, determine if n is in this set.

Input

The input consists of multiple test cases. The first line contains an integer t (1≤ t≤ 10^5) — the number of test cases. The description of the test cases follows.

The only line describing each test case contains three integers n, a, b (1≤ n,a,b≤ 10^9) separated by a single space.

Output

For each test case, print "Yes" if n is in this set, and "No" otherwise. You can print each letter in any case.

Example

Input


5
24 3 5
10 3 6
2345 1 4
19260817 394 485
19260817 233 264


Output


Yes
No
Yes
No
Yes

Note

In the first test case, 24 is generated as follows:

  * 1 is in this set, so 3 and 6 are in this set; 
  * 3 is in this set, so 9 and 8 are in this set; 
  * 8 is in this set, so 24 and 13 are in this set. 



Thus we can see 24 is in this set.

The five smallest elements of the set in the second test case is described in statements. We can see that 10 isn't among them.

Solution (in CPP):

Solution:
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
//#define ul unsigned long long 
#define pb push_back
typedef double dl;


ll average(vector<ll> &A, ll n)
{
   ll i,sum=0;
   for(i=0;i<A.size();i++)
   {
     sum = sum + A[i];
     
   }
   return (sum)/n;
}

int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,i,count=0,x,avg,c1,c2;
      cin>>n;
      vector <ll> A;
      for(i=0;i<n;i++)
      {
         cin>>x;
         A.pb(x);
      }
      sort(A.begin(), A.end());
      while(true)
      {
         avg= average(A , n-count);
         c1=0;
         c2=0;
         for(i=0;i<A.size();i++)
         {
             if(A[i]>avg)
             {
                A[i]=0;
                count++;
                c1++;
             }
             else
              c2++;
         }
         if(c2==A.size())
           break;
      }
      cout<<count<<endl;
   }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
//#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll i,n;
      cin>>n;
      ll A[2*n],B[2*n];
      for(i=0;i<(2*n);i++)
      {
         cin>>A[i];
      }
      sort(A, A+2*n);
     // ll a[n],b[n];
      for(i=0;i<n;i++)
      {
         B[2*i] = A[n+i]; 
         B[(2*i)+1] = A[i];
      }
      //for(i=0;i<2*n;i++)
      //{
      //   A[(2*i)+1] = b[i];
       //  A[2*i] = a[i];
      //}
      for(i=0;i<2*n;i++)
      {
         cout<<B[i]<<" ";
      }cout<<endl;
   }
}
*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
//#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
     ll n,i,count=0,n1;
     cin>>n;
     for(i=0;i<11;i++)
     {
        n1 = n - (i*111);
        //cout<<n1<<endl;
        if((n1%11 == 0) && (n1>=0))
        {
          cout<<"YES"<<endl;
          count++;
          break;
        }
     }
     if(count==0)
     {
       cout<<"NO"<<endl;
     }
  }
}*/
/*#include <bits/stdc++.h>
typedef long long int ll;
const unsigned int MOD = 1000000007;
 
using namespace std;
 
int main()
{
 
 
        ios::sync_with_stdio(false);
#ifndef NEAL_DEBUG
        cin.tie(nullptr);
#endif
 
        int N;
        cin >> N;
        priority_queue<int, vector<int>, greater<int>> pq;
        ll sum = 0;
        int potions = 0;
 
        for (int i = 0; i < N; i++)
        {
            int a;
            cin >> a;
            sum += a;
            pq.push(a);
            potions++;
 
            while (sum < 0)
            {
                sum -= pq.top();
                pq.pop();
                potions--;
            }
        }
 
        cout << potions << '\n';
    
    
 
    return 0;
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,i,a,min,diff;
      cin>>n;
      vector<ll> pos;
      vector<ll> neg;
      for(i=0;i<n;i++)
      {
         cin>>a;
         if(a<=0)
         {
            neg.pb(a);
         }
         else
         {
            pos.pb(a);
         }
      }//cout<<neg.size()<<endl;
      //if(pos.size()>0) 
         sort(pos.begin() , pos.end());
      //if(neg.size()>0)
         sort(neg.begin() , neg.end());
         //for(i=0;i<neg.size();i++)
           //// cout<<neg[i]<<" ";
            //cout<<endl; 
      if(neg.size()>1) 
         min=abs(neg[0] - neg[1]);
      if(neg.size()>1)
      {   
      for(i=0;i<neg.size()-1;i++)
      {
         //min=abs(neg[0] - neg[1]);
         diff = abs(neg[i]-neg[i+1]);
         if(min>diff)
           min=diff;
      }
      }//cout<<min<<endl;
      if(neg.size()==0)
      {
         cout<<1<<endl;
      }
      else if(neg.size()==1)
      {
         if(pos.size()>0)
           cout<<2<<endl;
         else
           cout<<1<<endl;  
      }
      else if(neg.size()>1)
      {
         if(pos.size()>0)
         {
            if(min>=pos[0])
            {
               cout<<neg.size()+1<<endl;;
            }
            else
              cout<<neg.size()<<endl;
         }
         else
           cout<<neg.size()<<endl;  
      }
   }
}*/
/*#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl "\n"
const int mod=1000000007;
mt19937_64 rng(std::chrono::steady_clock::now().time_since_epoch().count());
list<int>gr[100001];
vector<pair<int,int>>v;
 
int dp[100001][2];
void dfs(int u, int par){
  //for each neighbor of u
    for(auto x:gr[u]){
      if(x!=par){
        
        dfs(x,u);
        //here
        dp[u][0]+=max(abs(v[u-1].first-v[x-1].first)+dp[x][0],abs(v[u-1].first-v[x-1].second)+dp[x][1]);
        dp[u][1]+=max(abs(v[u-1].second-v[x-1].first)+dp[x][0],abs(v[u-1].second-v[x-1].second)+dp[x][1]);
      }
    }
}
void solve(){
    int n;
    cin>>n;
    v.clear();
    for(int i=1;i<=n;i++){
      gr[i].clear();
      dp[i][0]=dp[i][1]=0;
    }
    for(int i=1;i<=n;i++){
      int x,y;
      cin>>x>>y;
      v.push_back({x,y});
    }
    for(int i=0;i<n-1;i++){
      int x,y;
      cin>>x>>y;
      gr[x].push_back(y);
      gr[y].push_back(x);
    }
    dfs(1,0);
    cout<<max(dp[1][0],dp[1][1]);
    cout<<endl;
}
int32_t main(){
      ios_base::sync_with_stdio(0);
      cin.tie(0);
      // srand(time(0));
      int t;
      cin>>t;
        // t=1;
      while(t--){
        // cout<<"Case #"<<tt<<": ";
            solve();
            // tt++;
      }
      return 0;
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
ll add(ll arr[], ll n)
{
   ll sum=0,i;
   for(i=0;i<n;i++)
   {
      sum = sum + arr[i];
   }
   return sum;
}
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,i,summ,count=0;
      cin>>n;
      ll A[n];
      for(i=0;i<n;i++)
      {
        cin>>A[i];
      }
      summ = add(A , n);
      if(summ > n)
        {
           cout<<(summ-n)<<endl;
        }
      else if(summ == n)
       { cout<<0<<endl;}
      else
        {
          
           cout<<1<<endl;
        }
   }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,m,i,j;
      cin>>n>>m>>i>>j;
      
      cout<<1<<" "<<1<<" "<<n<<" "<<m<<endl;
   }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,i,a;
      cin>>n;
      vector <ll> vec;
      for(i=0;i<n;i++)
      {
         cin>>a;
         vec.pb(a);
      }
      sort(vec.begin(), vec.end());
      vector<ll>::iterator itr;
      ll min = INT_MAX;
      for(itr = vec.begin();itr<vec.end()-1;itr++)
      {
         if((*(itr+1) - *itr)<min)
         {
           min = (*(itr+1) - *itr);
         }
         if((*(itr+1) - *itr)==0)
         {
            
         }
      }
   }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
     ll n,i,p;
     cin>>n;
     ll A[n],B[n];
     for(i=0;i<n;i++)
     {
        cin>>A[i];
     }
     sort(A , A+n);
     ll min = 1000000009;
     //cout<<min<<endl;
     ll index=0;
     for(i=0;i<n-1;i++)
     {
        if((A[i+1] - A[i]) == 0)
        {
           index = i;
           break;
        }
        if((A[i+1] - A[i]) < min)
        {
          min = (A[i+1] - A[i]);
          index = i;
        }
        
     }
     B[0] = A[index];
     ll a=1;
     for(p=index+2;p<n;p++)
     {
       B[a] = A[p];
       a++;
     }
     for(p=0;p<index;p++)
     {
       B[a] = A[p];
       a++;
     }
     B[n-1] = A[index+1];
     for(i=0;i<n;i++)
     {
        cout<<B[i]<<" ";
     }cout<<endl;
  }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
     ll n,x,t,m;
     cin>>n>>x>>t;
     m = (t/x);
     if(x>t)
       cout<<0<<endl;
     else if(x==t)
       cout<<(n-1)<<endl;
     else
     {
        if(n>m)
        {
          ll ans = ((m)*(m+1)/2) + ((n-m-1)*m);
          cout<<ans<<endl;
        }  
        else if(n==m)
        {
          ll ans = ((m-1)*(m)/2);
          cout<<ans<<endl;
        }
        else
        {
           ll ans = ((n-1)*(n)/2);
           cout<<ans<<endl;
         
        }
     }
     
  }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
//ll addition(ll A[], ll m, ll n)
//{
   
//}
int main()
{
   ll n,q,a,b,i,ans;
   cin>>n>>q;
   string str;
   cin>>str;
   vector <pair<ll , ll>> vec;
   for(i=0;i<q;i++)
   {
     cin>>a>>b;
     vec.pb( make_pair(a,b) );
   } //cout<<vec[0].first<<" "<<vec[0].second<<endl;
   ll A[n];
   for(i=0;i<n;i++)
   {
     A[i] = ((ll)str[i] - 96);
     //cout<<A[i]<<" ";
   }//cout<<endl;
   ll B[n];
   B[0] = A[0];
   //cout<<B[0]<<" ";
   for(i=1;i<n;i++)
   {
      B[i] = A[i] + B[i-1];
      //cout<<B[i]<<" ";
   }//cout<<endl;
   for(i=0;i<q;i++)
   {
      ans = B[vec[i].second-1] - B[vec[i].first-1] + A[vec[i].first-1];
      //ans = 0;
      //for(i=vec[i].first;i<=vec[i].second;i++)
      //{
     //   ans = ans + A[i];
     // }
      cout<<ans<<endl;
   }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
   ll n,x,k,count=0,i;
   cin>>n>>k>>x;
   ll A[n];
   for(i=0;i<n;i++)
   {
      cin>>A[i];
   }
   sort(A , A+n);
   vector <ll> vec;
   for(i=0;i<n-1;i++)
   {
      if((A[i+1] - A[i]) > x)
        vec.pb(A[i+1] - A[i]);
   }
   sort(vec.begin() , vec.end());
   //for(i=0;i<vec.size();i++)
     //cout<<vec[i]<<" ";
     //cout<<endl;
   for(i=0;i<vec.size();i++)
   {
      if(k<=0)
        break;
      //((vec[i]/x)>=k)
      if((vec[i] % x) == 0)
        k = k - (vec[i]/x) +1;
          //count++;
      else
         k = k - (vec[i]/x);
      if(k>=0)
        count++;      
   }
   cout<<vec.size()-count+1<<endl;
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
bool sortbysec(const pair<int,int> &a,
              const pair<int,int> &b)
{
    return (a.second < b.second);
}
int main()
{
  ll n,a,b,i,ans=0,count=0;
  cin>>n;
  vector <pair<ll , ll>> vect;
  for(i=0;i<n;i++)
  {
    cin>>a>>b;
    vect.pb(make_pair(a , b));
    //sum = sum + vect[i].first;
  }
  sort(vect.begin(), vect.end(), sortbysec);
  
     
  
}*/
/*#include "bits/stdc++.h"
using namespace std;
 
int main()
{
    int t;
    cin >> t;
 
    while (t--)
    {
 
        int n;
        cin >> n;
        if (n % 2 == 1)
        {
            cout << "Bob\n";
            continue;
        }
        int cnt = 0;
        while (n % 2 == 0)
        {
            cnt++;
            n /= 2;
        }
 
        if (n > 1)
        {
            cout << "Alice\n";
        }
        else if (cnt % 2 == 0)
        {
            cout << "Alice\n";
        }
        else
            cout << "Bob\n";
    }
 
}*/
/*#include <iostream>
#include <bits/stdc++.h>
#define ll long long
using namespace std;
 
const int M = 1e9+7;
ll mod(ll x){
    return (x%M + M)%M;
}
 
ll mul(ll a, ll b){
    return mod((mod(a)*mod(b)));
}
 
ll add(ll a , ll b){
    return mod(mod(a)+mod(b));
}
 
string ok(string s, int k){
    int n=s.size();
    string ans="";
    for(int i=0;i<k;i++) ans+=(char)(s[(i%n)]);
    return ans;
}
 
void solve(){
    int n;
    cin>>n;
    int k;
    cin>>k;
    string s;
    cin>>s;
    vector<string>ans;
    for(int i=0;i<n;i++){
        ans.push_back(s.substr(0,i+1));
    }
    for(int i=0;i<n;i++){
        ans[i]=ok(ans[i],k);
    }
    sort(ans.begin(),ans.end());
    cout<<ans[0];
}  
int main(){
    ios::sync_with_stdio(0);
            cin.tie(0);
            cout.tie(0);
            cout<<fixed;
            cout<<setprecision(10);
    //        freopen("timber_input.txt", "r", stdin);
    //        freopen("timber_output.txt", "w", stdout);
            int t=1;
            // cin>>t;
            for(int i=1;i<=t;i++){
            //    cout<<"Case #"<<i<<": ";  
                solve();
    }
    return 0;
}*/
/*#include <iostream>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve(ll n)
{
    ll k;
    cin >> k;
 
    string str;
    cin >> str;
    int i = 1;
    for (; i < n; i++)
    {
        if (str[i] > str[0])
            break;
        else if (str[i] == str[0])
        {
            int j = 1, k = i + 1;
            while (j < i && k < n)
            {
                if (str[j] == str[k])
                {
                    j++;
                    k++;
                }
                else
                {
                    break;
                }
            }
            if (k == n)
            {
                break;
            }
            else
            {
 
                if (str[k] > str[j])
                {
                    break;
                }
                else
                {
                    i = k;
                }
            }
        }
    }
    if (i < n)
    {
        str = str.substr(0, i);
    }
    int sz = str.size();
    if (sz > 2 && str[0] == str[sz - 1] && str[sz - 2] < str[sz - 1])
    {
        str = str.substr(0, sz - 1);
    }
    while (str.size() < k)
    {
        str = str + str;
    }
    if (str.size() > k)
    {
        str = str.substr(0, k);
    }
    cout << str << endl;
}
 
int main()
{
 
    ll n;
    cin >> n;
    solve(n);
    return 0;
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,i;
      cin>>n;
      ll A[n];
      if(n%2 == 0)
      {
         for(i=1;i<n;i=i+2)
         {
            A[i] = i; 
         }
         for(i=0;i<n;i=i+2)
         {
            A[i] = i+2;
         }
      }
      else
      {
          A[0] = 3;
          A[1] = 1;
          A[2] = 2;
          for(i=3;i<n;i=i+2)
          {
             A[i] = i+2;
          }
          for(i=4;i<n;i=i+2)
          {
             A[i] = i;
          }
      } 
      for(i=0;i<n;i++)
      {
        cout<<A[i]<<" ";
      }cout<<endl;    
   
   }
}*/
/*#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ul unsigned long long 
#define pb push_back
typedef double dl;
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
     ll n,i,a,j,count=0;
     vector <pair<ll , ll>> vect;
     cin>>n;
     for(i=0;i<n;i++)
     {
        cin>>a;
        vect.pb(make_pair(a , i+1));
     }
     sort(vect.begin() , vect.end());
     //cout<<vect[0].first<<endl;
     for(i=0;i<n;i++)
     {
        for(j=i+1;j<n;j++)
        {
           ll f = (vect[i].first)*(vect[j].first);
           ll s = (vect[i].second) + (vect[j].second);
           if(f==s) count++;
           if(f>(2*n-1)) break;
        }
     }
     cout<<count<<endl;
  }
}*/
/*#include <bits/stdc++.h>
#define ll long long
#define ul unsigned long long 
typedef double dl;
#define F first
#define S second
#define pb push_back
using namespace std;
typedef double dl;
int main()
*/
/*#include <bits/stdc++.h>
typedef long long int ll;
const unsigned int MOD = 1000000007;
 
using namespace std;
 
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
 
    int t;
    cin >> t;
    for (int tt = 0; tt < t; tt++)
    {
        int n;
        cin >> n;
        ll a[n];
        for (int i = 0; i < n; i++)
        {
            cin>>a[i];
        }
        
        sort(a, a + n);
        ll sum = a[n - 1];
 
 
        ll neg[n];
        neg[0] = 0;
        for (int i = 1; i < n; i++)
        {
            neg[i] = neg[i - 1] + i * (a[i] - a[i - 1]);
            sum -= neg[i];
        }
            
        cout << sum << "\n";
    }
}*/
/*#include <bits/stdc++.h>
#define ll long long
#define ul unsigned long long 
typedef double dl;
#define F first
#define S second
#define pb push_back
using namespace std;
typedef double dl;
int main()
{
  ll t;
  cin>>t;
  while(t--)
  {
     ll n,even=0,odd=0,i;
     cin>>n;
     ll A[2*n];
     for(i=0;i<2*n;i++)
     {
        cin>>A[i];
        if(A[i] % 2 ==0)
        {
           even++;
        }
        else
        {
           odd++;
        }
     }
     if(even == odd)
       cout<<"Yes"<<endl;
     else
       cout<<"No"<<endl;
  }
}*/
/*#include <bits/stdc++.h>
#define ll long long
#define ul unsigned long long 
typedef double dl;
#define F first
#define S second
#define pb push_back
using namespace std;
typedef double dl;
int main()
{
   ll t;
   cin>>t;
   while(t--)
   {
      ll n,a,b,count=0,i=1;
      cin>>n>>a>>b;
      if(n%a == 0)
        cout<<"Yes"<<endl;
      else if(((n-b) % a == 0) || ((n-b) == 1))
        cout<<"Yes"<<endl;
      else if((n-1)%b == 0)
        cout<<"Yes"<<endl;
      else{
         while(true)
         {
            n = n-b;
            if(n%a==0)
            {
              count++;
              break;
            }
            n  = n - pow(a , i);
            if(n%b==0)
            {
              count++;
              break;
            }
         }
       }
   }
}*/
    #include <bits/stdc++.h>
    typedef long long int ll;
    const unsigned int MOD = 1000000007;
     
    using namespace std;
     
     
    int main()
    {
        ios_base::sync_with_stdio(false);
        cin.tie(NULL);
    #ifndef ONLINE_JUDGE
        freopen("input.txt", "r", stdin);
        freopen("output.txt", "w", stdout);
    #endif
     
        int t;
        cin >> t;
        for (int tt = 0; tt < t; tt++)
        {
            ll a, b, n;
            cin >> n >> a >> b;
     
            ll s = 1;
            int flag = 0;
     
            if(a==1)
            {
                if((n-1)%b == 0)
                {
                    cout << "Yes\n";
                }
                else
                {
                    cout << "No\n";
                }
                continue;
            }
            while (s <= n)
            {
                if ((n - s) % b == 0)
                {
                    flag = 1;
                    break;
                }
                s = s * a;
            }
            if (flag)
            {
                cout << "Yes\n" ;
            }
            else
            {
                cout << "No\n";
            }
        }
    #ifndef ONLINE_JUDGE
        cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
    #endif
        return 0;
    }