#include <bits/stdc++.h>
using namespace std;

#define int long long

namespace IO {
char buf_[1 << 21], *p1_ = buf_, *p2_ = buf_;
#define ch()                                                                 \
  (p1_ == p2_ &&                                                             \
           (p2_ = (p1_ = buf_) + fread(buf_, 1, 1 << 21, stdin), p1_ == p2_) \
       ? EOF                                                                 \
       : *p1_++)
inline int in() {
  int s = 0, f = 1;
  char x = ch();
  for (; x < '0' || x > '9'; x = ch())
    if (x == '-') f = -1;
  for (; x >= '0' && x <= '9'; x = ch()) s = (s * 10) + (x & 15);
  return f == 1 ? s : -s;
}
char buf__[1 << 21];
int p1__ = -1, p2__ = (1 << 21) - 1;
inline void flush() {
  fwrite(buf__, 1, p1__ + 1, stdout);
  p1__ = -1;
}
inline void pc(char x) {
  if (p1__ == p2__) flush();
  buf__[++p1__] = x;
}
inline void out(int x) {
  char k[30];
  int pos = 0;
  if (!x) return pc('0');
  if (x < 0) pc('-'), x = -x;
  while (x) k[++pos] = (x % 10) | 48, x /= 10;
  for (int i = pos; i; i--) pc(k[i]);
}
inline void out(string x) {
  int k = x.size();
  for (int i = 0; i < k; i++) pc(x[i]);
}
}  // namespace IO
using namespace IO;

inline int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }

inline int work(int p, int q, int b) {
  q /= gcd(p, q);
  while (q != 1 && b != 1) {
    q /= gcd(b, q);
    b = gcd(b, q);
    b *= b;
  }
  return (q == 1);
}

signed main() {
  int Ts = in();
  while (Ts--) {
    int p = in(), q = in(), b = in();
    if (!p)
      out("Infinite\n");
    else
      out(work(p, q, b) ? "Finite\n" : "Infinite\n");
  }
  flush();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

namespace IO {
char buf_[1 << 21], *p1_ = buf_, *p2_ = buf_;
#define ch()                                                                 \
  (p1_ == p2_ &&                                                             \
           (p2_ = (p1_ = buf_) + fread(buf_, 1, 1 << 21, stdin), p1_ == p2_) \
       ? EOF                                                                 \
       : *p1_++)
inline int in() {
  int s = 0, f = 1;
  char x = ch();
  for (; x < '0' || x > '9'; x = ch())
    if (x == '-') f = -1;
  for (; x >= '0' && x <= '9'; x = ch()) s = (s * 10) + (x & 15);
  return f == 1 ? s : -s;
}
char buf__[1 << 21];
int p1__ = -1, p2__ = (1 << 21) - 1;
inline void flush() {
  fwrite(buf__, 1, p1__ + 1, stdout);
  p1__ = -1;
}
inline void pc(char x) {
  if (p1__ == p2__) flush();
  buf__[++p1__] = x;
}
inline void out(int x) {
  char k[30];
  int pos = 0;
  if (!x) return pc('0');
  if (x < 0) pc('-'), x = -x;
  while (x) k[++pos] = (x % 10) | 48, x /= 10;
  for (int i = pos; i; i--) pc(k[i]);
}
inline void out(string x) {
  int k = x.size();
  for (int i = 0; i < k; i++) pc(x[i]);
}
}  // namespace IO
using namespace IO;

inline int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }

inline int work(int p, int q, int b) {
  q /= gcd(p, q);
  while (q != 1 && b != 1) {
    q /= gcd(b, q);
    b = gcd(b, q);
    b *= b;
  }
  return (q == 1);
}

signed main() {
  int Ts = in();
  while (Ts--) {
    int p = in(), q = in(), b = in();
    if (!p)
      out("Finite\n");
    else
      out(work(p, q, b) ? "Finite\n" : "Infinite\n");
  }
  flush();
  return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define int long long

namespace IO {
char buf_[1 << 21], *p1_ = buf_, *p2_ = buf_;
#define ch()                                                                 \
  (p1_ == p2_ &&                                                             \
           (p2_ = (p1_ = buf_) + fread(buf_, 1, 1 << 21, stdin), p1_ == p2_) \
       ? EOF                                                                 \
       : *p1_++)
inline int in() {
  int s = 0, f = 1;
  char x = ch();
  for (; x < '0' || x > '9'; x = ch())
    if (x == '-') f = -1;
  for (; x >= '0' && x <= '9'; x = ch()) s = (s * 10) + (x & 15);
  return f == 1 ? s : -s;
}
char buf__[1 << 21];
int p1__ = -1, p2__ = (1 << 21) - 1;
inline void flush() {
  fwrite(buf__, 1, p1__ + 1, stdout);
  p1__ = -1;
}
inline void pc(char x) {
  if (p1__ == p2__) flush();
  buf__[++p1__] = x;
}
inline void out(int x) {
  char k[30];
  int pos = 0;
  if (!x) return pc('0');
  if (x < 0) pc('-'), x = -x;
  while (x) k[++pos] = (x % 10) | 48, x /= 10;
  for (int i = pos; i; i--) pc(k[i]);
}
inline void out(string x) {
  int k = x.size();
  for (int i = 0; i < k; i++) pc(x[i]);
}
}  // namespace IO
using namespace IO;

inline int gcd(int x, int y) { return !y ? x : gcd(y, x % y); }

inline int work(int p, int q, int b) {
  q /= gcd(p, q);
  while (q != 1 && b != 1) {
    q /= gcd(b, q);
    b = gcd(b, q);
    b *= b;
  }
  return (q == 1);
}

signed main() {
  int Ts = in();
  while (Ts--) {
    int p = in(), q = in(), b = in();
    if (!p)
      out("No\n");
    else
      out(work(p, q, b) ? "Infinite\n" : "Finite\n");
  }
  flush();
  return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=10000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        q=q/gcd(q,b);
        bool flag = true;
        for (int i=1;i<=prime[0];i++)
        {
            if (q%prime[i]==0)
            {
                if (b%prime[i]!=0)
                {
                    flag = false;
                    break;
                }
                while(q%prime[i]==0)
                    q=q/prime[i];
                if (q==1)
                    break;
            }
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=10000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        q=q/gcd(q,b);
        bool flag = true;
        for (int i=1;i<=prime[0];i++)
        {
            if (q==1)
                break;
            if (q%prime[i]==0)
            {
                if (b%prime[i]!=0)
                {
                    flag = false;
                    break;
                }
                while(q%prime[i]==0)
                    q=q/prime[i];
                if (q==1)
                    break;
            }
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}
//1000000000000000000
//1000000000000000000
//1000000000000000000


#include<bits/stdc++.h>
using namespace std;
const int MAXN=100000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    //getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        k=gcd(q,b);
        bool flag = true;
        while(k!=1)
        {
            q=q/k;
            k=gcd(q,b);
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}
//1000000000000000000
//1000000000000000000
//1000000000000000000


#include<bits/stdc++.h>
using namespace std;
const int MAXN=1000000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        q=q/gcd(q,b);
        bool flag = true;
        for (int i=1;i<=prime[0];i++)
        {
            if (q%prime[i]==0)
            {
                if (b%prime[i]!=0)
                {
                    flag = false;
                    break;
                }
                while(q%prime[i]==0)
                    q=q/prime[i];
                if (q==1)
                    break;
            }
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
const int MAXN=100000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    //getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        k=gcd(q,b);
        bool flag = true;
        while(k!=1)
        {
            while (q%k==0)
                q=q/k;
            k=gcd(q,b);
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}
//1000000000000000000
//1000000000000000000
//1000000000000000000


#include<bits/stdc++.h>
using namespace std;
const int MAXN=100000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        q=q/gcd(q,b);
        bool flag = true;
        for (int i=1;i<=prime[0];i++)
        {
            if (q==1)
                break;
            if (q%prime[i]==0)
            {
                if (b%prime[i]!=0)
                {
                    flag = false;
                    break;
                }
                while(q%prime[i]==0)
                    q=q/prime[i];
                if (q==1)
                    break;
            }
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}
//1000000000000000000
//1000000000000000000
//1000000000000000000


#include<bits/stdc++.h>
using namespace std;
const int MAXN=100000;
int prime[MAXN+1];
void getPrime()
{
    memset(prime,0,sizeof(prime));
    for(int i=2; i<=MAXN; i++)
    {
        if(!prime[i])prime[++prime[0]]=i;
        for(int j=1; j<=prime[0]&&prime[j]<=MAXN/i; j++)
        {
            prime[prime[j]*i]=1;
            if(i%prime[j]==0) break;
        }
    }
}
long long gcd(long long a,long long b)
{
    return b == 0 ? a : gcd(b, a % b);
}
int main()
{
    long long p,q,b;
    int n;
    getPrime();
    //printf("%d\n",prime[0]);
    scanf("%d",&n);
    while(n--)
    {
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        long long k = gcd(p,q);
        q=q/k;
        q=q/gcd(q,b);
        bool flag = true;
        for (int i=1;i<=prime[0];i++)
        {
            if (q%prime[i]==0)
            {
                if (b%prime[i]!=0)
                {
                    flag = false;
                    break;
                }
                while(q%prime[i]==0)
                    q=q/prime[i];
                if (q==1)
                    break;
            }
        }
        if (b%q!=0)
            flag = false;
        if (flag)
            printf("Finite\n");
        else
            printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}

int main(){
//    freopen("in.txt","r",stdin);
//    freopen("ou.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        ll g=gcd(p,q);
        p/=g;
        q/=g;
        while(q%b==0) q/=b;
        while(true){
            g=gcd(b,q);
            if(g==1) break;
            q/=g;
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        ll g=gcd(p,q);
        p/=g;
        q/=g;
        while(true){
            g=gcd(b,q);
            if(g==1) break;
            q/=g;
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

inline ll gcd(ll a,ll b){
    while(b^=a^=b^=a%=b);
    return a;
}

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
//    freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        p=read();
        q=read();
        b=read();
        ll g=gcd(p,q);
        q/=g;
        g=gcd(b,q);
        while(g>1){
            q/=g;
            g=gcd(g,q);
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

inline ll gcd(ll a,ll b){
    while(b^=a^=b^=a%=b);
    return a;
}

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
//    freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        p=read();
        q=read();
        b=read();
        ll g=gcd(p,q);
        q/=g;
        g=gcd(b,q);
        while(g>1){
            while(q%g==0) q/=g;
            g=gcd(b,q);
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

ll gcd(ll a,ll b){
    return b==0?a:gcd(b,a%b);
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        ll g=gcd(p,q);
        p/=g;
        q/=g;
        while(q%b==0) q/=b;
        g=gcd(b,q);
        p/=g;
        q/=g;
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

inline ll gcd(ll a,ll b){
    while(b^=a^=b^=a%=b) return a;
}

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
//    freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        p=read();
        q=read();
        b=read();
        ll g=gcd(p,q);
        p/=g;
        q/=g;
        g=gcd(b,q);
        while(g>1){
            q/=g;
            g=gcd(b,q);
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

inline ll gcd(ll a,ll b){
    if(a==0)return b;
    if(b==0)return a;
    if(!(a&1)&&!(b&1))return 2*gcd(a>>1,b>>1);
    else if(!(a&1))return gcd(a>>1,b);
    else if(!(b&1))return gcd(a,b>>1);
    else return gcd(abs(a-b),min(a,b));
}

int main(){
//    freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        scanf("%I64d%I64d%I64d",&p,&q,&b);
        ll g=gcd(p,q);
        p/=g;
        q/=g;
        while(true){
            g=gcd(b,q);
            if(g==1) break;
            q/=g;
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <iostream>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>
#include <set>
#include <stack>

using namespace std;

const int maxn = 1e5+10;
typedef long long ll;
const ll inf = 1LL<<60;

inline ll gcd(ll a,ll b){
    if(a==0)return b;
    if(b==0)return a;
    if(!(a&1)&&!(b&1))return 2*gcd(a>>1,b>>1);
    else if(!(a&1))return gcd(a>>1,b);
    else if(!(b&1))return gcd(a,b>>1);
    else return gcd(abs(a-b),min(a,b));
}

ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

int main(){
//    freopen("in.txt","r",stdin);
    int T;
    scanf("%d",&T);
    while(T--){
        ll p,q,b;
        p=read();
        q=read();
        b=read();
        ll g=gcd(p,q);
        p/=g;
        q/=g;
        while(true){
            g=gcd(b,q);
            if(g==1) break;
            q/=g;
        }
        if(q==1) printf("Finite\n");
        else printf("Infinite\n");
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        GCD = __gcd(dem, base);
        while(GCD > 1) {
            if(dem % GCD != 0) break;
            while(dem % GCD == 0) dem /= GCD;
        }
        if(dem > 1) {
            cout << "Infinite\n";
        } else cout << "Finite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if(dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num -= ((ll)((long double)num/(long double)dem))*dem;
        base = ((ll)(((long double)base*(long double)num)/dem))*dem;
        for(int i = 0; i < 7; ++i) {
            base -= ((ll)((base*(long double)base)/dem))*dem;
        }
        if(base%dem == 0) {
            cout << "Finite\n";
            continue;
        }
        cout << "Infinite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num -= dem*floor(num/dem);
        base *= num;
        base -= dem*floor(base/dem);
        for(int i = 0; i < 7; ++i) {
            base *= base;
            base -= dem*floor(base/dem);
            if((ll)base == 0) break;
        }
        if((ll)base == dem*(ll)floor(base/dem)) {
            cout << "Finite\n";
            continue;
        }
        cout << "Infinite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    long double num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd((ll)num, (ll)dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num -= ((ll)(num/dem))*dem;
        base *= num;
        base -= ((ll)(base/dem))*dem;
        for(int i = 0; i < 7; ++i) {
            base *= base;
            base -= ((ll)(base/dem))*dem;
            if((ll)base == 0) break;
        }
        if((ll)base == ((ll)(base/dem))*dem) {
            cout << "Finite\n";
            continue;
        }
        cout << "Infinite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        while((GCD = __gcd(dem, base)) > 1) {
            dem /= GCD;
        }
        if(dem > 1) {
            cout << "Infinite\n";
        } else cout << "Finite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if(dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num %= dem;
        unordered_set<ll> seen;
        bool finite = true;
        while(num > 0) {
            num %= dem;
            if(num == 0) {
                break;
            }
            if(seen.count(num)) {
                finite = false;
                break;
            }
            seen.insert(num);
            num *= base;
        }
        cout << (finite?"Finite\n":"Infinite\n");

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if(dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num %= dem;
        unordered_set<int> seen;
        bool finite = true;
        while(num > 0) {
            num %= dem;
            if(num == 0) {
                break;
            }
            if(seen.count(num)) {
                finite = false;
                break;
            }
            seen.insert(num);
            num *= base;
        }
        cout << (finite?"Finite\n":"Infinite\n");

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if(__gcd(dem, base) > 1 || dem == 1) {
            cout << "Finite\n";
        } else {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        GCD = __gcd(dem, base);
        while(GCD > 1) {
            while(dem % GCD == 0) dem /= GCD;
            GCD = __gcd(GCD, dem);
        }
        if(dem > 1) {
            cout << "Infinite\n";
        } else cout << "Finite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    long double num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd((ll)num, (ll)dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num -= ((ll)(num/dem))*dem;
        base *= num;
        base -= ((ll)(base/dem))*dem;
        for(int i = 0; i < 7; ++i) {
            base *= base;
            base -= ((ll)(base/dem))*dem;
        }
        if((ll)base == ((ll)(base/dem))*dem) {
            cout << "Finite\n";
            continue;
        }
        cout << "Infinite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        ll GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if((ll)dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num -= dem*floor(num/dem);
        base *= num;
        base -= dem*floor(base/dem);
        for(int i = 0; i < 6; ++i) {
            base *= base;
            base -= dem*floor(base/dem);
            if((ll)base == 0) break;
        }
        if((ll)base == dem*(ll)floor(base/dem)) {
            cout << "Finite\n";
            continue;
        }
        cout << "Infinite\n";
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int n;
    cin >> n;
    ll num, dem, base;
    while(n--) {
        cin >> num >> dem >> base;
        if(num == 0) {
            cout << "Finite\n";
            continue;
        }
        int GCD = __gcd(num, dem);
        num /= GCD;
        dem /= GCD;
        if(dem == 1) {
            cout << "Finite\n";
            continue;
        }
        num %= dem;
        base *= num;
        base %= dem;
        for(int i = 0; i < 7; ++i) {
            base *= base;
            base %= dem;
        }
        if(base%dem == 0) {
            cout << "Finite\n";
            continue;
        }
        cout << "Infinite\n";
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001];
long long p[100001],q[100001],base[100001];
long long gcd(long long x,long long y)
{
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main(void)
{
    int n;
    long long gg,t,hh,fuck;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {
        if(p[i]==0)ans[i]=1;
        else
        {
            if(p[i]<q[i])t=p[i];
            else t=q[i];
            for(int ii=t;ii>0;ii--)
            gg=gcd(p[i],q[i]);//gg是p[i]和q[i]的最大公约数
            hh=q[i]/gg;
            fuck=base[i];

            while(gcd(hh,fuck)>1)
            {
                hh=hh/gcd(hh,fuck);
            }
            if(hh==1)ans[i]=1;
            else ans[i]=0;

        }
    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001],yin1[50000];
long long p[100001],q[100001],base[100001];
int main(void)
{
    int n,hh,dd=1,fuck=1,s1=0,s=0;
    long long gg,t;
    bool is=0;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {
        if(p[i]==0)ans[i]=1;
        else{
        if(p[i]<q[i])t=p[i];
        else t=q[i];
        for(int ii=t;ii>0;ii--)
        {
            if(p[i]%ii==0 && q[i]%ii==0)
            {
                gg=ii;
                break;
            }
        }//gg是p[i]和q[i]的最大公约数
        hh=q[i]/gg;
        fuck=base[i];

        if(hh==1)ans[i]=1;
        else
        {
            if(hh%2==0)
        {
            s1=1;
            yin1[0]=2;
        }

        for(int ii=2;ii<=hh;ii++)
        {
            if(hh%ii==0)
            {
                hh=hh/ii;
                ii--;
            }
            else if(hh%(ii+1)==0 && ii+1<=hh)
            {
                yin1[s1]=ii+1;
                s1++;
            }
        }//因式分解

        for(int ii=0;ii<s1;ii++)
        {
            dd=dd*yin1[ii];
        }
        if(fuck%dd==0)ans[i]=1;
        else ans[i]=0;

        }
        s1=0;

        s=0;
        is=0;
        dd=1;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001],yin1[50000],yin2[50000];
long long p[100001],q[100001],base[100001];
int main(void)
{
    int n,hh,fuck=1,s1=0,s2=0,s=0;
    long long gg,t;
    bool is=0;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {
        if(p[i]<q[i])t=p[i];
        else t=q[i];
        for(int ii=t;ii>0;ii--)
        {
            if(p[i]%ii==0 && q[i]%ii==0)
            {
                gg=ii;
                break;
            }
        }//gg是p[i]和q[i]的最大公约数
        hh=q[i]/gg;
        fuck=base[i];

        if(hh==1)ans[i]=1;
        else
        {
            if(hh%2==0)
        {
            s1=1;
            yin1[0]=2;
        }

        for(int ii=2;ii<=hh;ii++)
        {
            if(hh%ii==0)
            {
                hh=hh/ii;
                ii--;
            }
            else if(hh%(ii+1)==0 && ii+1<=hh)
            {
                yin1[s1]=ii+1;
                s1++;
            }
        }//因式分解
        if(fuck%2==0)
        {
            s2=1;
            yin2[0]=2;
        }

        for(int ii=2;ii<=fuck;ii++)
        {
            if(fuck%ii==0)
            {
                fuck=fuck/ii;
                ii--;
            }
            else if(fuck%(ii+1)==0 && ii+1<=fuck)
            {
                yin2[s2]=ii+1;
                s2++;
            }
        }
        for(int ii=0;ii<s1;ii++)
        {
            for(int j=s;j<s2;j++)
            {
                if(yin1[ii]==yin2[j])
                {
                    s=j+1;
                    is=1;
                    break;
                }
            }
            if(is==0)
            {
                ans[i]=0;//表示无限长
                break;
            }
            if(ii==s1-1 && is==1)
            {
                ans[i]=1;//有限长
            }
            is=0;
        }
        }
        s1=0;
        s2=0;
        s=0;
        is=0;

    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001];
long long p[100001],q[100001],base[100001];
long long gcd(long long x,long long y)
{
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main(void)
{
    int n;
    long long gg,t,hh,fuck,you;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%lld%lld%lld",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {

            gg=gcd(p[i],q[i]);//gg是p[i]和q[i]的最大公约数
            hh=q[i]/gg;
            fuck=base[i];

            you=2;
            while(you>1)
            {
                you=gcd(hh,fuck);
                hh=hh/you;
                fuck=you;
            }
            if(hh==1)ans[i]=1;
            else ans[i]=0;

    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001];
long long p[100001],q[100001],base[100001];
long long gcd(long long x,long long y)
{
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main(void)
{
    int n;
    long long gg,t,hh,fuck;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%lld%lld%lld",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {

            gg=gcd(p[i],q[i]);//gg是p[i]和q[i]的最大公约数
            hh=q[i]/gg;
            fuck=base[i];

            while(gcd(hh,fuck)>1)
            {
                hh=hh/gcd(hh,fuck);
            }
            if(hh==1)ans[i]=1;
            else ans[i]=0;

    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001],yin1[50000];
long long p[100001],q[100001],base[100001];
long long gcd(long long x,long long y)
{
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main(void)
{
    int n,hh,dd=1,fuck=1,s1=0,s=0;
    long long gg,t;
    bool is=0;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {
        if(p[i]==0)ans[i]=1;
        else{
        if(p[i]<q[i])t=p[i];
        else t=q[i];
        for(int ii=t;ii>0;ii--)
        gg=gcd(p[i],q[i]);//gg是p[i]和q[i]的最大公约数
        hh=q[i]/gg;
        fuck=base[i];

        if(hh==1)ans[i]=1;
        else
        {
            if(hh%2==0)
        {
            s1=1;
            yin1[0]=2;
        }

        for(int ii=2;ii<=hh;ii++)
        {
            if(hh%ii==0)
            {
                hh=hh/ii;
                ii--;
            }
            else if(hh%(ii+1)==0 && ii+1<=hh)
            {
                yin1[s1]=ii+1;
                s1++;
            }
        }//因式分解

        for(int ii=0;ii<s1;ii++)
        {
            dd=dd*yin1[ii];
        }
        if(fuck%dd==0)ans[i]=1;
        else ans[i]=0;

        }
        s1=0;

        s=0;
        is=0;
        dd=1;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001],yin1[50000],yin2[50000];
long long p[100001],q[100001],base[100001];
int main(void)
{
    int n,hh,fuck=1,s1=0,s2=0,s=0;
    long long gg,t;
    bool is=0;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {
        if(p[i]==0)ans[i]=1;
        else{
        if(p[i]<q[i])t=p[i];
        else t=q[i];
        for(int ii=t;ii>0;ii--)
        {
            if(p[i]%ii==0 && q[i]%ii==0)
            {
                gg=ii;
                break;
            }
        }//gg是p[i]和q[i]的最大公约数
        hh=q[i]/gg;
        fuck=base[i];

        if(hh==1)ans[i]=1;
        else
        {
            if(hh%2==0)
        {
            s1=1;
            yin1[0]=2;
        }

        for(int ii=2;ii<=hh;ii++)
        {
            if(hh%ii==0)
            {
                hh=hh/ii;
                ii--;
            }
            else if(hh%(ii+1)==0 && ii+1<=hh)
            {
                yin1[s1]=ii+1;
                s1++;
            }
        }//因式分解
        if(fuck%2==0)
        {
            s2=1;
            yin2[0]=2;
        }

        for(int ii=2;ii<=fuck;ii++)
        {
            if(fuck%ii==0)
            {
                fuck=fuck/ii;
                ii--;
            }
            else if(fuck%(ii+1)==0 && ii+1<=fuck)
            {
                yin2[s2]=ii+1;
                s2++;
            }
        }
        for(int ii=0;ii<s1;ii++)
        {
            for(int j=s;j<s2;j++)
            {
                if(yin1[ii]==yin2[j])
                {
                    s=j+1;
                    is=1;
                    break;
                }
            }
            if(is==0)
            {
                ans[i]=0;//表示无限长
                break;
            }
            if(ii==s1-1 && is==1)
            {
                ans[i]=1;//有限长
            }
            is=0;
        }
        }
        s1=0;
        s2=0;
        s=0;
        is=0;
        }
    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

int ans[100001];
long long p[100001],q[100001],base[100001];
long long gcd(long long x,long long y)
{
    if(y==0)return x;
    else return gcd(y,x%y);
}
int main(void)
{
    int n;
    long long gg,t,hh,fuck;

    scanf("%d",&n);
    for(int i=0;i<n;i++)
        scanf("%d%d%d",&p[i],&q[i],&base[i]);
    for(int i=0;i<n;i++)
    {
            gg=gcd(p[i],q[i]);//gg是p[i]和q[i]的最大公约数
            hh=q[i]/gg;
            fuck=base[i];

            while(gcd(hh,fuck)>1)
            {
                hh=hh/gcd(hh,fuck);
            }
            if(hh==1)ans[i]=1;
            else ans[i]=0;

    }
    for(int i=0;i<n;i++)
    {
        if(ans[i]==0)printf("Infinite\n");
        else printf("Finite\n");
    }

}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define gcd __gcd
int main()
{
    ll T,p,q,b;
    scanf("%lld",&T);
    while(T--){
        scanf("%lld%lld%lld",&p,&q,&b);
        ll t=gcd(p,q);
        q/=t;
        t=b;
        while(q!=1){
            t=gcd(q,t);
            q/=t;
            if(t==1)break;
        }
        if(q==1){
            printf("Finite\n");
        }else{
            printf("Infinite\n");
        }
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b ){ return b==0? a:gcd(b,a%b); }
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		LL p,q,b;
		scanf("%lld%lld%lld",&p,&q,&b);
		LL g = gcd(p,q);
		q /= g;
		while( q != 1 )
		{
			g = gcd(b,q);
			q /= g;
			if( g == 1 )
				break;
		}
		printf("%s\n",q==1?"Finite":"Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define gcd __gcd
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		LL p,q,b;
		scanf("%lld%lld%lld",&p,&q,&b);
		LL g = gcd(p,q);
		q /= g;
		while( q != 1 )
		{
			g = gcd(b,q);
			q /= g;
			if( g == 1 )
				break;
		}
		printf("%s\n",q==1?"Finite":"Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b ){ return b==0? a:gcd(b,a%b); }
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		LL p,q,b;
		scanf("%lld%lld%lld",&p,&q,&b);
		LL g = gcd(p,q);
		p /= g;	q /= g;
//		printf("%lld %lld\n",p,q);
		bool f = 1;
		while( q != 1 )
		{
			g = gcd(b,q);
			if( g == 1 && q != 1 )
			{
				f = 0;
				break;
			}
			if( q % g == 0 && q != 1 )
				q /= g;
		}
		printf("%s\n",f==1?"Finite":"Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
LL gcd(LL a, LL b ){ return b==0? a:gcd(b,a%b); }
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		LL p,q,b;
		scanf("%lld%lld%lld",&p,&q,&b);
		LL g = gcd(p,q);
		p /= g;	q /= g;
		bool f = 1;
		while( q != 1 )
		{
			g = gcd(b,g);
			if( g == 1 && q != 1 )
			{
				f = 0;
				break;
			}
			if( q % g == 0 && q != 1 )
				q /= g;
		}
		printf("%s\n",f==1?"Finite":"Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define gcd __gcd
int main()
{
	int T;
	scanf("%d",&T);
	while(T--)
	{
		LL p,q,b;
		scanf("%lld%lld%lld",&p,&q,&b);
		LL g = gcd(p,q);
		q /= g;
		g = b;
		while( q != 1 )
		{
			g = gcd(g,q);
			q /= g;
			if( g == 1 )
				break;
		}
		printf("%s\n",q==1?"Finite":"Infinite");
	}
	return 0;
}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    if (  b == 1 ) {
        if ( a == 1 ) return 1;
        else return 0;
    }
    ll hcf = gcd(a,b);

    while ( a % hcf == 0 and hcf != 1) {
        a /= hcf;
        cout << a << " " << hcf << endl;
    }

     b = gcd (a,b);
    return check ( a, b);
}
int main(){
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    if (  b == 1 ) {
        if ( a == 1 ) return 1;
        else return 0;
    }
    ll hcf = gcd(a,b);

    while ( a % hcf == 0 and hcf != 1) {
        a /= hcf;
       // cout << a << " " << hcf << endl;
    }

     b = gcd (a,b);
    return check ( a, b);
}
int main(){
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    if ( a == 1 )
        return 1;
    ll hcf = gcd(a,b);
    if ( hcf == 1 )
        return 0;
    while ( a % hcf == 0 )
        a /= hcf ;
    return check ( a, b);
}
int main(){
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    while ( b != 1 ) {
        while ( a % b == 0 )
            a /= b;
        b = gcd (a,b);
    }
    if (a == 1 ) return 1;
    return 0;
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    if ( a == 1 )
        return 1;
    ll hcf = gcd(a,b);
    if ( hcf == 1 )
        return 0;
    while ( a % hcf == 0 )
        a /= hcf ;
     b /= hcf ;
    return check ( a, b);
}
int main(){
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    if ( a == 1 )
        return 1;
    ll hcf = gcd(a,b);
    if ( hcf == 1 )
        return 0;
    while ( a % hcf == 0 )
        a /= hcf ;
    return check ( a, b);
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int n; cin >> n;
    while (n--) {
        ll p, q, b;
        cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
        ll hcf = gcd(p,q);
        p/=hcf;
        q /= hcf;
        int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
        else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    while ( b != 1 ) {
        while ( a % b == 0 )
            a /= b;
        b = gcd (a,b);
    }
    if (a == 1 ) return 1;
    return 0;
}
int main(){
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


//
// Created by Anand Kotriwal on 15/05/18.
//

#include <iostream>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <algorithm>

using namespace std;

typedef long long ll;

typedef pair<int, int > pi;

typedef pair<pi, pi > pii;

int ar[100];

vector <pii> v;
#define mkp make_pair

ll gcd (ll a, ll b) {
    if ( b == 0 )
        return a;
    else
        return gcd(b, a%b);
}

int check ( ll a, ll b ) {
    if ( a == 1 or b == 1 )
        return 1;
    ll hcf = gcd(a,b);
    if ( hcf == 1 )
        return 0;
    while ( a % hcf == 0 )
        a /= hcf ;
     b /= hcf ;
    return check ( a, b);
}
int main(){
    int n; cin >> n;
    while (n--) {
    ll p, q, b;
    cin >> p >> q >> b;
        if ( p == 0 ) {
            cout <<"Finite\n";
            continue;
        }
    ll hcf = gcd(p,q);
    p/=hcf;
    q /= hcf;
    int flg = check(q,b);
        if (!flg) cout <<"Infinite\n";
    else cout <<"Finite\n";}

}


#include<bits/stdc++.h>
using namespace std;

long long gcd(long long a,long long b){
	if(b==0) return a;
	return gcd(b,a%b);
}
int main(){
	int k;
	cin>>k;
	while (k--){
	long long p,q,b;
	cin>>p>>q>>b;
	long long asd=gcd(p,q);
	long long minim=q/asd;
	while(minim!=1){
		long long minim2=minim/gcd(minim,b);
		if(minim2==minim) break;
		minim=minim2;
	}
	if(minim==1){
		cout<<"Finite\n";
	}
	else{
		cout<<"Infinite\n";
	}
}
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){

	ll n;
	cin>>n;
	for (int i = 0; i < n; ++i)
		{
			ll p,q,b;
			cin>>p>>q>>b;

			ll aux = __gcd(p,q);
			p/=aux;
			q/=aux;

			ll resto = p%q;
			if(resto == 0){
				cout<<"Finite\n";
				continue;
			}
			else{
				if(q == 1)
					cout<<"Finite\n";
				else{
					ll aux1 = __gcd(q,b);
					while(aux1 > 1){
						//cout<<"hola";
						q/=aux1;
						aux1 = __gcd(q,b);
					}
					if(q>1)
						cout<<"Infinite\n";
					else
						cout<<"Finite\n";
				}
			}

		}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){
	ll n;
	cin>>n;
	for (int i = 0; i < n; ++i)
		{
			ll p,q,b;
			cin>>p>>q>>b;
			ll aux = __gcd(p,q);
			p/=aux;
			q/=aux;
			ll resto = p%q;
			if(resto == 0){
				cout<<"Finite\n";
				continue;
			}
			else{
				if(q == 1){
					cout<<"Finite\n";
				}
				else{
					if(__gcd(q,b) == 1) cout<<"Infinite\n";
					else cout<<"Finite\n";

				}
			}
		}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd (ll a,ll b){ return (b)?gcd(b,a%b):a; }
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	ll n,p,q,b;
	cin>>n;
	while (n--){
		cin>>p>>q>>b;
		if (p==0) cout<<"Finite\n";
		else {
			ll gc = gcd(p,q);
			q/=gc;
			while ( (gc = gcd(q,b))!=1 ){
				while ( q%gc == 0 ) q/=gc;
			}
			if (q == 1) cout<<"Finite\n";
			else cout<<"Infinite\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvvi> vvvvi;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    while (n--) {
        ll p, q, b;
        cin >> p >> q >> b;
        ll gc = __gcd(p, q);
        p /= gc, q /= gc;
        bool flag = true;
        while (q != 1) {
            ll gc2 = __gcd(q, b);
            if (__gcd(q, b) == 1) {
                flag = false;
                break;
            }
            q /= gc2;
        }
        if (!flag) cout << "Infinite\n";
        else cout << "Finite\n";
    }
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

string solve(ll p, ll q, ll b) {
	/*p %= q;
	set<ll> vis;
	vis.insert(p);
	while(p) {
		while(p < q) p = (p * b);
		p %= q;
		if(vis.count(p)) {
			return "Infinite";
		} else {
			vis.insert(p);
		}
	}
	return "Finite"; */
	q = q/__gcd(p, q);
	while(q != 1LL) {
		ll g = __gcd(b, q);
		if(g == 1) {
			return "Infinite";
		}
		q /= g;
	}

	return "Finite";
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;
	for (int i = 0; i < n; ++i)
	{
		ll p, q, b; cin >> p >> q >> b;
		cout << solve(p, q, b) << '\n';
	}
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long

int main(){
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	ll n;
	cin>>n;
	for (int i = 0; i < n; ++i)
		{
			ll p,q,b;
			cin>>p>>q>>b;

			ll aux = __gcd(p,q);
			p/=aux;
			q/=aux;

			ll resto = p%q;
			if(resto == 0){
				cout<<"Finite\n";
				continue;
			}
			else{
				if(q == 1)
					cout<<"Finite\n";
				else{
					ll aux1 = __gcd(q,b);
					while(aux1 > 1){
						//cout<<"hola";
						while(q%aux1 == 0){
							q/=aux1;
						}
						aux1 = __gcd(q,b);
					}
					if(q>1)
						cout<<"Infinite\n";
					else
						cout<<"Finite\n";
				}
			}

		}

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd (ll a,ll b){ return (b)?gcd(b,a%b):a; }
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	ll n,p,q,b;
	cin>>n;
	while (n--){
		cin>>p>>q>>b;
		if (p==0) cout<<"Finite\n";
		else {
			ll gc = gcd(p,q);
			q/=gc;
			while (q%b == 0 && q>=b) q/=b;
			gc = gcd(q,b);
			if (gc==q) cout<<"Finite\n";
			else cout<<"Infinite\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvvi> vvvvi;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    while (n--) {
        ll p, q, b;
        cin >> p >> q >> b;
        ll gc = __gcd(p, q);
        p /= gc, q /= gc;
        bool flag = true;
        while (q != 1) {
            ll gc2 = __gcd(q, b);
            if (gc2 == 1) {
                flag = false;
                break;
            }
            q /= gc2;
        }
        if (!flag) cout << "Infinite\n";
        else cout << "Finite\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long gcd(long long a,long long b){
	if(b==0) return a;
	return gcd(b,a%b);
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int k;
	cin>>k;
	while (k--){
	long long p,q,b;
	cin>>p>>q>>b;
	long long asd=gcd(p,q);
	long long minim=q/asd;
	while(minim!=1){
		long long minim2=minim/gcd(minim,b);
		if(minim2==minim) break;
		minim=minim2;
	}
	if(minim==1){
		cout<<"Finite\n";
	}
	else{
		cout<<"Infinite\n";
	}
}
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd (ll a,ll b){ return (b)?gcd(b,a%b):a; }
int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);
	ll n,p,q,b;
	cin>>n;
	while (n--){
		cin>>p>>q>>b;
		if (p==0) cout<<"Finite\n";
		else {
			ll gc = gcd(p,q);
			q/=gc;
			if (q>=b){
				gc = gcd(q,b);
				if (gc!=1){
					while (q%gc == 0) q/=gc;
				}
			}
			gc = gcd(q,b);
			if (gc==q) cout<<"Finite\n";
			else cout<<"Infinite\n";
		}
	}
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvvi> vvvvi;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    while (n--) {
        ll p, q, b;
        cin >> p >> q >> b;
        ll gc = __gcd(p, q);
        p /= gc, q /= gc;
        bool flag = true;
        while (q != 1) {
            ll gc2 = __gcd(q, b);
            if (gc2 == 1) {
                flag = false;
                break;
            }
            while (q%gc2 == 0) q /= gc2;
        }
        if (!flag) cout << "Infinite\n";
        else cout << "Finite\n";
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

long long gcd(long long a,long long b){
	if(b==0) return a;
	return gcd(b,a%b);
}
int main(){
	ios::sync_with_stdio(0); cin.tie(0);
	int k;
	cin>>k;
	while (k--){
		long long p,q,b;
		cin>>p>>q>>b;
		long long asd=gcd(p,q);
		long long minim=q/asd;
		long long prueba=gcd(minim,b);
		while(minim!=1){
			long long minim2=minim/prueba;
			if(minim2==minim) break;
			minim=minim2;
			prueba=gcd(minim,prueba);
		}
		if(minim==1){
			cout<<"Finite\n";
		}
		else{
			cout<<"Infinite\n";
		}
	}
}


#include<bits/stdc++.h>

using namespace std;

typedef long long ll;

string solve(ll p, ll q, ll b) {
	/*p %= q;
	set<ll> vis;
	vis.insert(p);
	while(p) {
		while(p < q) p = (p * b);
		p %= q;
		if(vis.count(p)) {
			return "Infinite";
		} else {
			vis.insert(p);
		}
	}
	return "Finite"; */
	q /= __gcd(p, q);
	while(b != 1LL and q != 1LL) {
		ll g = __gcd(b, q);
		if(g == 1) {
			return "Infinite";
		}
		b /= g;
		q /= g;
	}

	return "Finite";
}

int main(int argc, char const *argv[])
{
	ios::sync_with_stdio(0);
	cin.tie(0);

	int n; cin >> n;
	for (int i = 0; i < n; ++i)
	{
		ll p, q, b; cin >> p >> q >> b;
		cout << solve(p, q, b) << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>
#define F first
#define S second
using namespace std;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<vvi> vvvi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<vii> vvii;
typedef vector<vvvi> vvvvi;

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    int n;
    cin >> n;
    while (n--) {
        ll p, q, b;
        cin >> p >> q >> b;
        ll gc = __gcd(p, q);
        p /= gc, q /= gc;
        bool flag = true;
        while (q != 1 && b != 1) {
            ll gc2 = __gcd(q, b);
            if (gc2 == 1) {
                flag = false;
                break;
            }
            while (q%gc2 == 0) q /= gc2;
            while (b%gc2 == 0) b /= gc2;
        }
        if (!flag) cout << "Infinite\n";
        else cout << "Finite\n";
    }
    return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int T;
LL p,q,b;
int main(){
	cin>>T;
	while (T--){
		scanf("%lld%lld%lld",&p,&q,&b);
		p%=q;
		LL t=__gcd(p,q);
		p/=t,q/=t;
		b=__gcd(b,q);
		while (b>1){
			while (q%b==0) q/=b;
			b=__gcd(q,b);
		}
		if (q==1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int T;
LL p,q,b;
int main(){
	cin>>T;
	while (T--){
		cin>>p>>q>>b;
		p%=q;
		LL t=__gcd(p,q);
		p/=t,q/=t;
		while (q>1){
			t=__gcd(b,q);
			q/=t;
			if (t==1) break;
		}
		if (q==1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int T,p,q,b;
int main(){
	cin>>T;
	while (T--){
		cin>>p>>q>>b;
		p%=q;
		int t=__gcd(p,q);
		p/=t,q/=t;
		while (q>1){
			t=__gcd(b,q);
			q/=t;
			if (t==1) break;
		}
		if (q==1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int T;
LL p,q,b;
int main(){
	cin>>T;
	while (T--){
		cin>>p>>q>>b;
		p%=q;
		LL t=__gcd(p,q);
		p/=t,q/=t;
		while (q>1){
			t=__gcd(b,q);
			if (t==1) break;
			while (q%t==0) q/=t;
		}
		if (q==1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<cstdio>
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<ctime>
#include<string>
#include<cmath>
#include<algorithm>
#include<complex>
#include<vector>
#include<set>
#include<map>
#include<queue>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define LL long long
#define FOR(i,a,b) for (int i=a;i<=b;++i)
#define FORD(i,a,b) for (int i=a;i>=b;--i)
using namespace std;
void getint(int &v){
    char ch,fu=0;
    for(ch='*'; (ch<'0'||ch>'9')&&ch!='-'; ch=getchar());
    if(ch=='-') fu=1, ch=getchar();
    for(v=0; ch>='0'&&ch<='9'; ch=getchar()) v=v*10+ch-'0';
    if(fu) v=-v;
}
int T;
LL p,q,b;
int main(){
	cin>>T;
	while (T--){
		cin>>p>>q>>b;
		p%=q;
		LL t=__gcd(p,q);
		p/=t,q/=t;
		b=__gcd(b,q);
		while (b>1){
			while (q%b==0) q/=b;
			b=__gcd(q,b);
		}
		if (q==1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define frab(i, a, b) for (int i = a; i < b; ++i)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef long double ld;

const ll INF = 2e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-14;
const ld PI = acos(-1);
const ll M = 1e3 + 10;
const int N = 1e6 + 10;

void solve() {
    ios_base::sync_with_stdio(false);
    ll n, p, q, b;
    cin >> n;
    while (n--) {
        cin >> p >> q >> b;
        p %= q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        if (p == 0) {
            cout << "Finite\n";
            continue;
        }
        ll g = __gcd(b, q);
        while (q % g == 0 && g != 1) {
            q /= g;
            g = __gcd(b, q);
        }
        if (q != 1)
            cout << "Infinite\n";
        else
            cout << "Finite\n";
    }
}

int main() {
    //freopen("a.in", "r", stdin);
    solve();

}


#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define frab(i, a, b) for (int i = a; i < b; ++i)
#define pb push_back
#define mp make_pair

using namespace std;
using apa::lint;

typedef long long ll;
typedef long double ld;

const ll INF = 2e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-14;
const ld PI = acos(-1);
const ll M = 1e3 + 10;
const int N = 1e6 + 10;

void solve() {
    ios_base::sync_with_stdio(false);
    ll n, p, q, b;
    cin >> n;
    while (n--) {
        cin >> p >> q >> b;
        p %= q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        if (p == 0 || __gcd(b, q) != 1)
            cout << "Finite\n";
        else
            cout << "Infinite\n";
    }
}

int main() {
    //freopen("a.in", "r", stdin);
    solve();

}


#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define frab(i, a, b) for (int i = a; i < b; ++i)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef long double ld;

const ll INF = 2e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-14;
const ld PI = acos(-1);
const ll M = 1e3 + 10;
const int N = 1e6 + 10;

void solve() {
    ios_base::sync_with_stdio(false);
    ll n, p, q, b;
    cin >> n;
    while (n--) {
        cin >> p >> q >> b;
        p %= q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        if (p == 0 || __gcd(b, q) != 1)
            cout << "Finite\n";
        else
            cout << "Infinite\n";
    }
}

int main() {
    //freopen("a.in", "r", stdin);
    solve();

}


#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define frab(i, a, b) for (int i = a; i < b; ++i)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef long double ld;

const ll INF = 2e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-14;
const ld PI = acos(-1);
const ll M = 1e3 + 10;
const int N = 1e6 + 10;

void solve() {
    ios_base::sync_with_stdio(false);
    ll n, p, q, b;
    cin >> n;
    while (n--) {
        cin >> p >> q >> b;
        p %= q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        if (p == 0) {
            cout << "Finite\n";
            continue;
        }
        ll g = __gcd(b, q);
        while (q % g == 0 && g != 1)
            q /= g;
        if (q != 1)
            cout << "Infinite\n";
        else
            cout << "Finite\n";
    }
}

int main() {
    //freopen("a.in", "r", stdin);
    solve();

}


#include <bits/stdc++.h>
#include "lint.h"
#include "lint.cpp"

#define fr(i, n) for (int i = 0; i < n; ++i)
#define frab(i, a, b) for (int i = a; i < b; ++i)
#define pb push_back
#define mp make_pair

using namespace std;
using apa::lint;

typedef long long ll;
typedef long double ld;

const ll INF = 2e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-14;
const ld PI = acos(-1);
const ll M = 1e3 + 10;
const int N = 1e6 + 10;

void solve() {
    ios_base::sync_with_stdio(false);
    ll n, p, q, b;
    cin >> n;
    while (n--) {
        cin >> p >> q >> b;
        p %= q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        if (p == 0 || __gcd(b, q) != 1)
            cout << "Finite\n";
        else
            cout << "Infinite\n";
    }
}

int main() {
    //freopen("a.in", "r", stdin);
    solve();

}


#include <bits/stdc++.h>
#define fr(i, n) for (int i = 0; i < n; ++i)
#define frab(i, a, b) for (int i = a; i < b; ++i)
#define pb push_back
#define mp make_pair

using namespace std;

typedef long long ll;
typedef long double ld;

const ll INF = 2e9 + 10;
const ll MOD = 1e9 + 7;
const ld EPS = 1e-14;
const ld PI = acos(-1);
const ll M = 1e3 + 10;
const int N = 1e6 + 10;

void solve() {
    ios_base::sync_with_stdio(false);
    ll n, p, q, b;
    cin >> n;
    while (n--) {
        cin >> p >> q >> b;
        p %= q;
        ll gcd = __gcd(p, q);
        p /= gcd;
        q /= gcd;
        if (p == 0) {
            cout << "Finite\n";
            continue;
        }
        ll g = __gcd(b, q);
        while (q % g == 0 && g > 1) {
            while (q % g == 0) {
                q /= g;
            }
            g = __gcd(b, q);
        }
        if (q != 1)
            cout << "Infinite\n";
        else
            cout << "Finite\n";
    }
}

int main() {
    //freopen("a.in", "r", stdin);
    solve();

}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll p,q,b;
vector<ll>v;
bool f(ll n,ll b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		int G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll p,q,b;
vector<ll>v;
bool f(ll n,ll b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
		b=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		ll G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int p,q,b;
vector<ll>v;
bool f(int n,int b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
		while(b%g==0)b/=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		int G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll p,q,b;
vector<ll>v;
bool f(ll n,ll b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
		b=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		int G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int p,q,b;
vector<ll>v;
bool f(int n,int b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		int G=gcd(p,q);
		p/=G;
		q/=G;
		//枚举q的所有因子，如果这个因子不能整除b则为无限小数
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
int p,q,b;
vector<ll>v;
bool f(int n,int b)
{
	v.clear();
	int n2,nn;
    n2=n;
	nn=(int)sqrt(n);
    for(int i=2;i<=nn;i++)
    {
        while (n2%i == 0)
        {
            //cout << i<<' ';
            v.pb(i);
            while(n2%i==0)
            {
            	n2/=i;
			}
        }
    }
    if(n2!=1)v.pb(n2);
    for(int i=0;i<v.size();i++)
    {
    	int vv=v[i];
		if(b%vv!=0)
		{
			return false;
		}
	}
	return true;
}
int main()
{
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		int G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll p,q,b;
vector<ll>v;
bool f(ll n,ll b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
		b=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	std::ios::sync_with_stdio(false);
std::cin.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		int G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(),x.end()
#define mem0(s) memset(s,0,sizeof(s))
#define meminf(s) memset(s,0x3f,sizeof(s))
#define mp_insert(a,b,c) mp.insert(map<pair<int,int>,int>::value_type(make_pair(a,b),c))
double pi=acos(-1);
typedef long long ll;
typedef unsigned long long ull;
inline ll lowbit(ll x){return x&(-x);}
inline ll min(ll a,ll b){return a<b?a:b;}
inline ll max(ll a,ll b){return a>b?a:b;}
const int INF=0x3f3f3f3f,max_n=100005,mod=19260817;
inline ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
inline ll pow(ll a,ll b,ll mod){ll res=1;a%=mod;assert(b>=0);for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll p,q,b;
vector<ll>v;
bool f(ll n,ll b)
{
	while(1)
	{
		ll g=gcd(n,b);
		if(n==1||b==1||g==1)break;
		while(n%g==0)n/=g;
		b=g;
	}
	if(n!=1&&b==1)
	{
		return false;
	}
	else if(n!=1&&b!=1)
	{
		return false;
	}
	else return true;
}
int main()
{
	//std::ios::sync_with_stdio(false);
	//std::cin.tie(0);
	int n;cin>>n;
	for(int i=1;i<=n;i++)
	{
		cin>>p>>q>>b;
		ll G=gcd(p,q);
		p/=G;
		q/=G;
		//Ã¶¾ÙqµÄËùÓÐÒò×Ó£¬Èç¹ûÕâ¸öÒò×Ó²»ÄÜÕû³ýbÔòÎªÎÞÏÞÐ¡Êý
		if(f(q,b))
		{
			cout<<"Finite"<<endl;
		}
		else cout<<"Infinite"<<endl;
	}
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Azat Ismagilov
 */

/*-.``                                          ``                                                   .
 .``                               ```````..-:oss+/-``                                              .
 .`                        `-.-://::::/+/+osyyhhyyysss+/:-.`                                        .
 `                      .::++:/+++syyyyyyyyyyyyydmmddhdmhhho-.`                                     -
                    ``./o+/++oooooooyhmmNddmmmdddhhmmdmmNmmmhhs:-..`                                -
                   `.-/++/oshddmmmmhhdmmNmmmNMNNmmmhyddmNNNNNNmmdhhso+/-.`                          :
                  `./+soooshNNMMNMNNNNNNmMMNNNMMNNmmdhddmmNNNNmNNddmmmmdhs+-`                       :
                `.:/+osyddhhdmNMMMMNMNNMNMMMMMMMMMMNmdmmmmmNMNNNNdddNNmNNNmdy/`                     -
 ``           `-osssydmNNNNNNNNmNMMMMMNMMMMMMMMMMMMMNmmNNNmmNNMNmmydNNNNNNmNNNy:`                   `
            `-+syoyhmNNNMMMMMMMMMMMMMNNNNNNNMMMMMMMMMMNNmmmmmmmNdmhsyNNmMNmmmNdho-`                 `
 `         -oddyoohyyyhdmNMMMMMMMMNmmdmmmmmddmmmmmmNNmmmdddddmmmmNdshdmhMNNdhdmmmho-`
 `        :hmmysosoooooshdmmNMMMNmdysshhddddhysysyshyyhhyyyyhhhdmmyhmym+hmhyyydhmNmh+.
 `       .ohmyyhhhhyysssssyhdmmmhyo+++osydhssoooo++oo+/+++/++++ososhmhss+hy++sshydmmds-
 `       :shhdddddddmmddddddhhyso/--::/+oyyso+o++//::-----:---:://oyhhsoosos+osydhdddh+.
 `      `/yddmdmmmmNNmNNNNdyo+/:-.````...-:///+/+/:--............--:+syhhdshshhdddhddhy/`
 `       :ydmdmNNNNNNMNNdyo/--..````````````..---:-......````````...-:+yhmmddhmmmmddmddy-
 `      `/ydhmNNNNNMMNmy+:...`````````````````....-....``.`````````...-/ohmmmmmNNmmmmmmd+`
        `:oydNNMNNNNmho:-..`````````````````````.....`......```````....-/oyhmmmNNNNdddddy.
        `:oddNNNNNNds/-..`````````````````````````````..........```.....:/syhmmmNNNNmmhhy:
        `-ymmNNNNdho:...``````````````````````````````````..............-:+yhmmmmNmNNmmmh+`
        `:dmNNMNdhs:-...`````````````````````````````````````...........-:+shdmmmNNmmmmmmo`
        `/dNNMMNdh+-.....``````````````````````````````````````````.....--/oyhmNmmNNNNNNNh-
        .omNMNNNdy/-......```````````````````````````````````````````....--:+sdmmNNNNMNNNm+`
        .sNNNNmdds/-......`````````````````````````````````````````````...---:+hmmNNNMNMMNh:`
        `oNMMNNNdy/:-.....````````````````````````````````....--:::----.....---/ymNNNNMMMNho/.
         .dNMMMNmy+:-.....````````````````````````....--:/+osyhhhyyyssoo+:------/hNNNNNNNdso+/-`
         .oNMMMNmds/--...`````````````............-:/oyddmmmmmddhhysoooooo+/:-:/+ymmmmNNdo/++/:-`
 `        -yNMMNNdy/--..``````....--:://::---....-:oyhdmddhyyssssssssoooo+++shoddhhmmNNmy+shhyo:`
 `        `+mNNNNms/--..`..-:/osyyhhdddhyso/-....-:ohhyssyyyyysssssyyhhyssoosdo+/:/ymmNdss+ohhy/.
           -dNNNNNs:--..:/+syyhddmmmdddddhyo:--://sssyyyhhhyhddmmmhhhhhhyso+/o-..-:+hmdy++//+dh+.
           `sNNNNNo---:+syyyyssooosyyhdddhhhyo/::::sosyhhhso+sssyssssso++/:--/...--:odhoso++:yho:.
            :dNNNN+--:+oso++osssyyhdmmhddhyys:`````:sssso+++++++++++++/:--...:`...--/yyohhys++yoo+`
 ``         `:hmNNo/:///++osyhhhdmdmddssso+so:`````./ys+::::://////::--...``..`...--:oy/yhhs++ooo:`
 ``         ./syhdhs-::/+oshhhdhssso+++++/+yo-.````..:o:--............`````````...--:/s:oyys+++/.
            `.-:odNd:-::/+ooooo++//++++/-.-+:..`````...//:-.........```````````...--:/+/sss+//-`
            `:+//dmo+:-------:://///:-..``./-..`````````.://:--....```````````....--::+++///:.
            .oysoyd++/--............````..-:..``````````.:/o+:-....`````````......--::/+//:-`
            .sy+:/y+:/:..................-::-.```` ``````../o/:--...............---::://::-`
            .:s/:/oo::--........``````..-:/+-..```````.-----o/::::----------------:::::/+:.
            `://-:/o/--...`.```````....--://.--:----::/+osooo/--::///::::::::::::::::::/o:`
             -//---//:-.....````.......-:::--:oyysoosssyhhyyo/-..--://///:::::::::::::://.
              `-...-::--.............--::-...-oyhhyyhhhssooo+/-..---::///::::::::::::::-.
                  ``.-:--...........-:::-..``.-/////osso++//:---..-:::::::-::::/::::::-``
 `                  ``.----........--::-..`.``...---::::////:-----:/++/::----:::::::::.`
 `                     `..------..--::-.....``..-::////oososssssyyyyyo+/:.--::::::::::.`
 .                       `.-----------.----..-://+++++ossyyyysssso+//:::---://////::::.`
 -                        `-:::---------/+/+ssssssssssyss+/////:--....---::/++///////:.`
 .                        `-::::--...--/ossssoo+//:---::::/++/:::--------:/++++//////:-`
 .`                        .-:::------:/+/--..-::/+/////+++oo++////::::::://++++//////:.
 -`                         .-::::::--:::-.....-://+ooosssyssoooo++//::::://+++///////:`
 .`                         `.:////////::-..--:::/+oosyyyyyyssoo+/::----://+++///////:-`
 ``                          `:++++/++//:----:://++oossssoo++//::--.---:///+++///+//::-`
 -`          ````             .:+ooo+++/::-------::::::-----.--....--::////+///+++//::--
 `.`    ``    ````````        `./++oooo++/:--.........`........---::://////++++++///::::-
 :-.`   ``    ``` `            `-:/oooooo++/:--.........-----::////++++++++++++++///::::o.
 ++-``   `     ``               .:/o+osoooo++//:------::////++ooooooo+++++ooo+++////::::/+       `  `
 `/:```         `   ```         `:os/+ossoooo++++///+++oosoosssssooooo+ooooooo++////:::::+-``` `
 `...               .`.          -sh+//++osssoooooooooosssssssssooooooooooooo+++/////:::::/s--:-   `
                   `:`           `:oo/:://+osssssoosooooossssssssssooooooooooo++/////::::-::::o.    .
                   ``              `::::::://+ossssssyssyyyyyyyyyssooooooooooo++/////::::----:o/.`..+
                                    .-:::::::://++osssyyyyyyyyyyyssooooooooooo++/////::::--------+ydy
                                    .---------::://++oossssyyysssssoooooossooo+++///::::------.....:+
                                    `----------:::::://++oooossssoooooooooooo+++///::::-----.......``
                                    .--------------::::///++oooooooooooooo++++/////:::----.......````
                        ```         `-----------------::::////++++++++++++++/////::::----......``````
                     ```.-:::--`   `.----.-.....-------::::://////////////////:::::----.......```````
                  `.-:+osyyyys: ````...............------::::::::::::::::::::::::----.......`````````
    `...`````.--:/osysyyhyyyyo```````..................-------::::--::--::::------......`````````````
  ``.-:://+osssyhhyyyyyhsyyyy/` ```````.................----------------------........```````````````
 `.:+ossssssyyhhhyhhhhhhhhyyy-` ```````````.................--------...............````````````````
 osssssyyyyyhhhhhhhhhhhhhhyhy-`  ```````````````````````....................``````````````````````
 syyyyyhhhhhhhhhhhhhhdhhhhhhy.    ``````````````````````````````..``..````````````````````````     */

#include <bits/stdc++.h>

#define int unsigned long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define vr swap(b, d);
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e18;
const int MOD = 1e9 + 7;

int gcd(int a, int b) {
    if (a <= 0 || b <= 0){
        exit(1);
    }
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

class TaskA {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;
        for (int i = 0; i < n; i++) {
            int p, q, b;
            in >> p >> q >> b;
            if (p == 0) {
                out << "Finite" << "\n";
                continue;
            }
            int d = gcd(p, q);
            if (d > 1 && d <= p && d <= q) {
                p /= d;
                q /= d;
            }
            while(q % b == 0){
                q /= b;
            }
            for (int j = 0; j < 60; j++) {
                if (q == 1){
                    break;
                }
                int d = gcd(q, b);
                if (d <= 1){
                    break;
                }
                q /= d;                                                                                vr
                while(q % b == 0){
                    q /= b;
                }                                                                                vr
            }
            if (gcd(q, b) <= 1 && q > 1) {
                out << "Infinite" << "\n";
            } else {
                out << "Finite" << "\n";
            }
        }
    }
};

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
	TaskA solver;
	std::istream& in(std::cin);
	std::ostream& out(std::cout);
	solver.solve(in, out);
	return 0;
}


/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author Azat Ismagilov
 */

/*-.``                                          ``                                                   .
 .``                               ```````..-:oss+/-``                                              .
 .`                        `-.-://::::/+/+osyyhhyyysss+/:-.`                                        .
 `                      .::++:/+++syyyyyyyyyyyyydmmddhdmhhho-.`                                     -
                    ``./o+/++oooooooyhmmNddmmmdddhhmmdmmNmmmhhs:-..`                                -
                   `.-/++/oshddmmmmhhdmmNmmmNMNNmmmhyddmNNNNNNmmdhhso+/-.`                          :
                  `./+soooshNNMMNMNNNNNNmMMNNNMMNNmmdhddmmNNNNmNNddmmmmdhs+-`                       :
                `.:/+osyddhhdmNMMMMNMNNMNMMMMMMMMMMNmdmmmmmNMNNNNdddNNmNNNmdy/`                     -
 ``           `-osssydmNNNNNNNNmNMMMMMNMMMMMMMMMMMMMNmmNNNmmNNMNmmydNNNNNNmNNNy:`                   `
            `-+syoyhmNNNMMMMMMMMMMMMMNNNNNNNMMMMMMMMMMNNmmmmmmmNdmhsyNNmMNmmmNdho-`                 `
 `         -oddyoohyyyhdmNMMMMMMMMNmmdmmmmmddmmmmmmNNmmmdddddmmmmNdshdmhMNNdhdmmmho-`
 `        :hmmysosoooooshdmmNMMMNmdysshhddddhysysyshyyhhyyyyhhhdmmyhmym+hmhyyydhmNmh+.
 `       .ohmyyhhhhyysssssyhdmmmhyo+++osydhssoooo++oo+/+++/++++ososhmhss+hy++sshydmmds-
 `       :shhdddddddmmddddddhhyso/--::/+oyyso+o++//::-----:---:://oyhhsoosos+osydhdddh+.
 `      `/yddmdmmmmNNmNNNNdyo+/:-.````...-:///+/+/:--............--:+syhhdshshhdddhddhy/`
 `       :ydmdmNNNNNNMNNdyo/--..````````````..---:-......````````...-:+yhmmddhmmmmddmddy-
 `      `/ydhmNNNNNMMNmy+:...`````````````````....-....``.`````````...-/ohmmmmmNNmmmmmmd+`
        `:oydNNMNNNNmho:-..`````````````````````.....`......```````....-/oyhmmmNNNNdddddy.
        `:oddNNNNNNds/-..`````````````````````````````..........```.....:/syhmmmNNNNmmhhy:
        `-ymmNNNNdho:...``````````````````````````````````..............-:+yhmmmmNmNNmmmh+`
        `:dmNNMNdhs:-...`````````````````````````````````````...........-:+shdmmmNNmmmmmmo`
        `/dNNMMNdh+-.....``````````````````````````````````````````.....--/oyhmNmmNNNNNNNh-
        .omNMNNNdy/-......```````````````````````````````````````````....--:+sdmmNNNNMNNNm+`
        .sNNNNmdds/-......`````````````````````````````````````````````...---:+hmmNNNMNMMNh:`
        `oNMMNNNdy/:-.....````````````````````````````````....--:::----.....---/ymNNNNMMMNho/.
         .dNMMMNmy+:-.....````````````````````````....--:/+osyhhhyyyssoo+:------/hNNNNNNNdso+/-`
         .oNMMMNmds/--...`````````````............-:/oyddmmmmmddhhysoooooo+/:-:/+ymmmmNNdo/++/:-`
 `        -yNMMNNdy/--..``````....--:://::---....-:oyhdmddhyyssssssssoooo+++shoddhhmmNNmy+shhyo:`
 `        `+mNNNNms/--..`..-:/osyyhhdddhyso/-....-:ohhyssyyyyysssssyyhhyssoosdo+/:/ymmNdss+ohhy/.
           -dNNNNNs:--..:/+syyhddmmmdddddhyo:--://sssyyyhhhyhddmmmhhhhhhyso+/o-..-:+hmdy++//+dh+.
           `sNNNNNo---:+syyyyssooosyyhdddhhhyo/::::sosyhhhso+sssyssssso++/:--/...--:odhoso++:yho:.
            :dNNNN+--:+oso++osssyyhdmmhddhyys:`````:sssso+++++++++++++/:--...:`...--/yyohhys++yoo+`
 ``         `:hmNNo/:///++osyhhhdmdmddssso+so:`````./ys+::::://////::--...``..`...--:oy/yhhs++ooo:`
 ``         ./syhdhs-::/+oshhhdhssso+++++/+yo-.````..:o:--............`````````...--:/s:oyys+++/.
            `.-:odNd:-::/+ooooo++//++++/-.-+:..`````...//:-.........```````````...--:/+/sss+//-`
            `:+//dmo+:-------:://///:-..``./-..`````````.://:--....```````````....--::+++///:.
            .oysoyd++/--............````..-:..``````````.:/o+:-....`````````......--::/+//:-`
            .sy+:/y+:/:..................-::-.```` ``````../o/:--...............---::://::-`
            .:s/:/oo::--........``````..-:/+-..```````.-----o/::::----------------:::::/+:.
            `://-:/o/--...`.```````....--://.--:----::/+osooo/--::///::::::::::::::::::/o:`
             -//---//:-.....````.......-:::--:oyysoosssyhhyyo/-..--://///:::::::::::::://.
              `-...-::--.............--::-...-oyhhyyhhhssooo+/-..---::///::::::::::::::-.
                  ``.-:--...........-:::-..``.-/////osso++//:---..-:::::::-::::/::::::-``
 `                  ``.----........--::-..`.``...---::::////:-----:/++/::----:::::::::.`
 `                     `..------..--::-.....``..-::////oososssssyyyyyo+/:.--::::::::::.`
 .                       `.-----------.----..-://+++++ossyyyysssso+//:::---://////::::.`
 -                        `-:::---------/+/+ssssssssssyss+/////:--....---::/++///////:.`
 .                        `-::::--...--/ossssoo+//:---::::/++/:::--------:/++++//////:-`
 .`                        .-:::------:/+/--..-::/+/////+++oo++////::::::://++++//////:.
 -`                         .-::::::--:::-.....-://+ooosssyssoooo++//::::://+++///////:`
 .`                         `.:////////::-..--:::/+oosyyyyyyssoo+/::----://+++///////:-`
 ``                          `:++++/++//:----:://++oossssoo++//::--.---:///+++///+//::-`
 -`          ````             .:+ooo+++/::-------::::::-----.--....--::////+///+++//::--
 `.`    ``    ````````        `./++oooo++/:--.........`........---::://////++++++///::::-
 :-.`   ``    ``` `            `-:/oooooo++/:--.........-----::////++++++++++++++///::::o.
 ++-``   `     ``               .:/o+osoooo++//:------::////++ooooooo+++++ooo+++////::::/+       `  `
 `/:```         `   ```         `:os/+ossoooo++++///+++oosoosssssooooo+ooooooo++////:::::+-``` `
 `...               .`.          -sh+//++osssoooooooooosssssssssooooooooooooo+++/////:::::/s--:-   `
                   `:`           `:oo/:://+osssssoosooooossssssssssooooooooooo++/////::::-::::o.    .
                   ``              `::::::://+ossssssyssyyyyyyyyyssooooooooooo++/////::::----:o/.`..+
                                    .-:::::::://++osssyyyyyyyyyyyssooooooooooo++/////::::--------+ydy
                                    .---------::://++oossssyyysssssoooooossooo+++///::::------.....:+
                                    `----------:::::://++oooossssoooooooooooo+++///::::-----.......``
                                    .--------------::::///++oooooooooooooo++++/////:::----.......````
                        ```         `-----------------::::////++++++++++++++/////::::----......``````
                     ```.-:::--`   `.----.-.....-------::::://////////////////:::::----.......```````
                  `.-:+osyyyys: ````...............------::::::::::::::::::::::::----.......`````````
    `...`````.--:/osysyyhyyyyo```````..................-------::::--::--::::------......`````````````
  ``.-:://+osssyhhyyyyyhsyyyy/` ```````.................----------------------........```````````````
 `.:+ossssssyyhhhyhhhhhhhhyyy-` ```````````.................--------...............````````````````
 osssssyyyyyhhhhhhhhhhhhhhyhy-`  ```````````````````````....................``````````````````````
 syyyyyhhhhhhhhhhhhhhdhhhhhhy.    ``````````````````````````````..``..````````````````````````     */

#include <bits/stdc++.h>

#define int unsigned long long
#define fs first
#define sc second
#define pb push_back
#define ppb pop_back
#define pf push_front
#define ppf pop_front
#define mp make_pair
#define len(v) ((int)v.size())
#define vc vector
#define pr pair
#define endd "\n"; return 0;
#define all(v) v.begin(), v.end()

using namespace std;

const int MAXN = 1e5;
const int INF = 1e18;
const int MOD = 1e9 + 7;

int gcd(int a, int b) {
    if (a <= 0 || b <= 0) {
        exit(1);
    }
    while (b) {
        a %= b;
        swap(a, b);
    }
    return a;
}

class TaskA {
public:
    void solve(std::istream &in, std::ostream &out) {
        int n;
        in >> n;
        for (int i = 0; i < n; i++) {
            int p, q, b;
            in >> p >> q >> b;
            if (p == 0) {
                out << "Finite" << "\n";
                continue;
            }
            int d = gcd(p, q);
            p /= d;
            q /= d;
            for (int j = 0; j < 35; j++) {
                int d = gcd(q, b);
                if (d <= 1) {
                    break;
                }
                q /= d;
            }
            if (q == 1) {
                out << "Finite" << "\n";
            } else {
                out << "Infinite" << "\n";
            }
        }
    }
};

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    TaskA solver;
    std::istream &in(std::cin);
    std::ostream &out(std::cout);
    solver.solve(in, out);
    return 0;
}