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 M 1000000007
#include <iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <numeric>
typedef long long int ll;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef map<int,int> maaap;
#define deffor(i,b) for(int i=0;i<b;i++)
#define defeqfor(i,b) for(int i=0;i<=b;i++)
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define eqFOR(i,a,b) for(int i=a;i<=b;i++)
#define int long long
#define prione(x) cout<<x<<'\n'
#define pritwo(x,y) cout<<x<<" "<<y<<endl
#define prinone() cout<<'\n'
#define fast_io  ios_base::sync_with_stdio(false);cin.tie(NULL);
typedef long long ll;
#define pub push_back
#define pob pop_back
#define puf push_front
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define all(x) (x).begin(),(x).end()
int nxt(){long long n; cin>>n; return n;}

void init_code(){
 fast_io;
 #ifndef ONLINE_JUDGE
 freopen("input.txt", "r", stdin);
 freopen("output.txt", "w", stdout);
 #endif // ONLINE_JUDGE
}
int gcd(int a,int b){if(b==0)return a;return gcd(b,a%b);}
int search(int array[],int x,int n){int k=0;for(int b=n/2; b>=1; b=b/2){while(k+b<n && array[k+b]<=x)k+=b;}if(array[k]==x)return true;else return false;}
int binarySearch(int arr[], int l, int r, int x)
{
    if (r >= l) {
        int mid = l + (r - l) / 2;
        if (arr[mid] == x)
            return mid;
        if (arr[mid] > x)
            return binarySearch(arr, l, mid - 1, x);
        return binarySearch(arr, mid + 1, r, x);
    }
    return -1;
}

// lower_bound return the pointer to the first array element
//whose value is at least x

//upper_bound return the pointer which has the the larger value x

// equal_range return both above pointer

int countelement(int array[],int x,int n)
{
   auto a=lower_bound(array,array+n,x);
   auto b=upper_bound(array,array+n,x);
   return b-a;
}

// tot calculate binpow of a no.
/*int binpow(long long int x, long long int y)
{

    // Initialize answer
    long long int res = 1;

    // Check till the number becomes zero
    while (y>0)
    {
        if (y&1)
             res = (res * x)%M;
        x=(x*x)%M;
        y>>=1;
    }

    return res;
}/*

//modular exponentiation

/*int power(long long int x, unsigned  int y,int p)
{
    int res = 1;
    x=x%p;
    if(x==0)
        return 0;
    while(y>0)
    {

        if(y&1)
            res=(res*x)%M;
        y=y>>1;
        x=(x*x)%p;
    }
    return res;
}*/

bool isPowerOfTwo(int n)
{
   if(n==0)
   return false;
 
   return (ceil(log2(n)) == floor(log2(n)));
}

bool subsetsum(int a[],int sum,int n)
{
   if(sum==0)
      return true;
   if(n==0)
      return false;
   if(a[n-1]<=sum)
   {
      return subsetsum(a,sum-a[n-1],n-1) || subsetsum(a,sum,n-1);
   }
   else
   {
      return subsetsum(a,sum,n-1);
   }
}



void printAllsubsets(int a[],int n,vector<int> v,int sum)
{
   if(sum==0){
      for(auto x:v)
         cout<<x<<" ";
      cout<<endl;
      return;
   }
   if(n==0)
      return ;

   printAllsubsets(a,n-1,v,sum);
   v.pub(a[n-1]);
   printAllsubsets(a,n-1,v,sum-a[n-1]);
}



void printall(int a[],int n, int sum)
{
   vector<int> v;
   printAllsubsets(a,n,v,sum);
}

int totalpaths=0;
void mazepath(int i,int j,int n,int m,string osf)
{
   if(i==n-1 and j==m-1)
   {
      totalpaths+=1;
      cout<<osf<<endl;
      return;
   }
   if(i==n || j==m)
      return;
   mazepath(i,j+1,n,m,osf+"R");
   mazepath(i+1,j,n,m,osf+"D");
   mazepath(i+1,j+1,n,m,osf+"->");
}

int highestPowerof2(int n)
{
   int p = (int)log2(n);
   return (long long )pow(2, p);
}


void solve(){
  long long  n,a,b;
  cin>>n>>a>>b;

  if(a==1)
  {
   if((n-1)%b==0)
      cout<<"Yes"<<endl;
   else{
      cout<<"NO"<<endl;
   }
  }
else{
  int s=1;
  int flag=0;
  while(s<=n)
  {
   if((n-s)%b==0)
      {
         flag=1;
         break;
      }
      s=s*a;
  }
  if(flag==1)
   cout<<"Yes"<<endl;
else
   cout<<"No"<<endl;
  }
}
   
 

     



int32_t main()
{
    init_code();
    int t=1;
    cin>>t;
    while(t--)
    {
        solve();
        

    }
    #ifndef ONLINE_JUDGE
    cout<<""<<endl;
    cout<<"Executed In: "<<((float)clock()/CLOCKS_PER_SEC)<<" secs"<<endl;
    #endif
}