#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>

#ifdef LOCAL
# include <opencv2/core/core.hpp>
# include <opencv2/highgui/highgui.hpp>
# include <opencv2/imgproc/imgproc.hpp>
#endif

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define PREFETCH(ptr, rw, level) ((void)0)
#else
#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME()
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<(n);++i)
#define FJ(n) for(int j=0;j<(n);++j)
#define FK(n) for(int k=0;k<(n);++k)
#define FL(n) for(int l=0;l<(n);++l)
#define FQ(n) for(int q=0;q<(n);++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b)
#define COMPARE_BY(obj, field) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
inline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); }
inline int countBits(ll v){return countBits((ull)v); }
inline int countBits(int v){return countBits((uint)v); }
unsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T> inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); }
//template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
unsigned long long rdtsc() { unsigned long long ret = 0;
#ifdef __clang__
    return __builtin_readcyclecounter();
#endif
#ifndef _MSC_VER
    asm volatile("rdtsc" : "=A" (ret) : :);
#endif
    return ret; }
// Fast IO ********************************************************************************************************
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template<class T = int> T readInt() {
    auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
    c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();
    bool m = false; if (*__ir == '-') ++__ir, c(), m = true;
    T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
    ++__ir; return m ? -r : r;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template<class T>
void writeInt(T x, char endc = '\n') {
    if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0';
    char* s = __iw;
    while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; }
    char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f;
    if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
    if (endc) { *__iw++ = endc; }
}
template<class T>
void writeStr(const T& str) {
    int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; }
}
struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } };
static __FL__ __flushVar__;

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)
#ifdef LOCAL
#define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3;
#define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++;
#else
#define BEGIN_PROFILE(idx, name)
#define END_PROFILE
#endif

#define mod         1000000009
const int USUAL_MOD = mod;
template<class T> inline void normmod(T &x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; }
template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; }
template<class T1, class T2, class T3 = int> inline void addmodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; }
template<class T1, class T2, class T3 = int> inline void submodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ x %= m; n %= m; ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; }
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }
template<class T>
T fast_gcd(T u, T v) {
    int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1;
        shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif;
    } return std::max(u, v) << shl;
}

inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
    return d;
}
template<class T> bool isPrime(T x) { if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
    for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }
bool millerRabin(long long n) {
    if (n <= 1000) return isPrime(n);
    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
    for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;
        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
        for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
        return false; nextp:;
    } return true;
}

// Useful constants

//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431,
#define T9          1000000000
#define T18         1000000000000000000LL
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define mod         1000000009
#define fftmod      998244353
#define EPS         (double)1e-10
#define PI          3.14159265358979323846264
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

int32_t solve();
int32_t main(int argc, char** argv) {
    ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
        FREIN("input.txt");
    //            FREOUT("out.txt");
#endif
    return solve();
}

inline ll mulmodfast(ll x, ll n, ll m){ ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; if (r < 0) r += m; if (r >= m) r -= m; return r; }
int solve() {
    int n = readInt();
    while (n--) {
        ll p = readInt<ll>();
        ll q = readInt<ll>();
        ll b = readInt<ll>();
        q /= gcd(p, q);
        b %= q;
        b = mulmodfast(b, b, q);
        b = mulmodfast(b, b, q);
        b = mulmodfast(b, b, q);
        b = mulmodfast(b, b, q);
        b = mulmodfast(b, b, q);
        b = mulmodfast(b, b, q);
        writeStr(b == 0 ? "Finite\n" : "Infinite\n");
    }
    return 0;
}


#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>

#ifdef LOCAL
# include <opencv2/core/core.hpp>
# include <opencv2/highgui/highgui.hpp>
# include <opencv2/imgproc/imgproc.hpp>
#endif

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define PREFETCH(ptr, rw, level) ((void)0)
#else
#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME()
#endif

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<(n);++i)
#define FJ(n) for(int j=0;j<(n);++j)
#define FK(n) for(int k=0;k<(n);++k)
#define FL(n) for(int l=0;l<(n);++l)
#define FQ(n) for(int q=0;q<(n);++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

#define COMPARE(obj) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b)
#define COMPARE_BY(obj, field) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) { return a.field < b.field; }

#define checkbit(n, b) (((n) >> (b)) & 1)
#define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b)))
#define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
inline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
inline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); }
inline int countBits(ll v){return countBits((ull)v); }
inline int countBits(int v){return countBits((uint)v); }
unsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
template<class T> inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); }
//template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
unsigned long long rdtsc() { unsigned long long ret = 0;
#ifdef __clang__
    return __builtin_readcyclecounter();
#endif
#ifndef _MSC_VER
    asm volatile("rdtsc" : "=A" (ret) : :);
#endif
    return ret; }
// Fast IO ********************************************************************************************************
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template<class T = int> T readInt() {
    auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
    c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();
    bool m = false; if (*__ir == '-') ++__ir, c(), m = true;
    T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
    ++__ir; return m ? -r : r;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template<class T>
void writeInt(T x, char endc = '\n') {
    if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0';
    char* s = __iw;
    while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; }
    char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f;
    if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
    if (endc) { *__iw++ = endc; }
}
template<class T>
void writeStr(const T& str) {
    int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; }
}
struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } };
static __FL__ __flushVar__;

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)
#ifdef LOCAL
#define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3;
#define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++;
#else
#define BEGIN_PROFILE(idx, name)
#define END_PROFILE
#endif

#define mod         1000000009
const int USUAL_MOD = mod;
template<class T> inline void normmod(T &x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; }
template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; }
template<class T1, class T2, class T3 = int> inline void addmodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; }
template<class T1, class T2, class T3 = int> inline void submodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; }
inline ll mulmod(ll x, ll n, ll m){ x %= m; n %= m; ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; }
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }
template<class T>
T fast_gcd(T u, T v) {
    int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1;
        shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif;
    } return std::max(u, v) << shl;
}

inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
    return d;
}
template<class T> bool isPrime(T x) { if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
    for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }
bool millerRabin(long long n) {
    if (n <= 1000) return isPrime(n);
    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
    for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;
        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
        for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
        return false; nextp:;
    } return true;
}

// Useful constants

//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, #define T18         1000000000000000000LL
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define mod         1000000009
#define fftmod      998244353
#define EPS         (double)1e-10
#define PI          3.14159265358979323846264
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

int32_t solve();
int32_t main(int argc, char** argv) {
    ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
    //    FREIN("input.txt");
    //            FREOUT("out.txt");
#endif
    return solve();
}

int solve() {
    int n = readInt();
    FI(n) {
        ll p, q, b;
        p = readInt<ll>();
        q = readInt<ll>();
        b = readInt<ll>();
        q /= gcd(p, q);
        while (true) {
            b = gcd(b, q);
            if (b == 1) break;
            do {
            q /= b;
            } while (q % b == 0);
        }
        puts(q == 1 ? "Finite" : "Infinite");
    }

    return 0;
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
////#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
//
//#define _CRT_SECURE_NO_WARNINGS
//# include <iostream>
//# include <cmath>
//# include <algorithm>
//# include <stdio.h>
//# include <cstdint>
//# include <cstring>
//# include <string>
//# include <cstdlib>
//# include <vector>
//# include <bitset>
//# include <map>
//# include <queue>
//# include <ctime>
//# include <stack>
//# include <set>
//# include <list>
//# include <random>
//# include <deque>
//# include <functional>
//# include <iomanip>
//# include <sstream>
//# include <fstream>
//# include <complex>
//# include <numeric>
//# include <immintrin.h>
//# include <cassert>
//# include <array>
//# include <tuple>
//
//#ifdef LOCAL
//# include <opencv2/core/core.hpp>
//# include <opencv2/highgui/highgui.hpp>
//# include <opencv2/imgproc/imgproc.hpp>
//#endif
//
//using namespace std;
//
//// Let's define unordered map
//# ifdef __GNUC__
//# if __cplusplus > 199711L
//# include <unordered_set>
//# include <unordered_map>
//# else
//# include <tr1/unordered_map>
//# include <tr1/unordered_set>
//using namespace tr1;
//# endif
//# else
//# include <unordered_map>
//# include <unordered_set>
//# endif
//
//#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
//#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
//#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
//#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
//#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
//#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
//#define macro_dispatcher___(macro, nargs) macro ## nargs
//#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
//#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
//#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
//#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
//#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
//#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
//#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
//#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
//#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"
//
//#ifdef _MSC_VER
//#define PREFETCH(ptr, rw, level) ((void)0)
//#else
//#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
//#endif
//
//#ifdef LOCAL
//#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
//#else
//#define CURTIME()
//#endif
//
//typedef long long ll;
//typedef long double ld;
//typedef unsigned int uint;
//typedef unsigned long long ull;
//typedef pair<int, int> pii;
//typedef pair<long long, long long> pll;
//typedef vector<int> vi;
//typedef vector<long long> vll;
//typedef int itn;
//
//template<class T1, class T2, class T3>
//struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
//template<class T1, class T2, class T3>
//bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
//template<class T1, class T2, class T3>
//bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
//#define tri triple<int,int,int>
//#define trll triple<ll,ll,ll>
//
//#define FI(n) for(int i=0;i<(n);++i)
//#define FJ(n) for(int j=0;j<(n);++j)
//#define FK(n) for(int k=0;k<(n);++k)
//#define FL(n) for(int l=0;l<(n);++l)
//#define FQ(n) for(int q=0;q<(n);++q)
//#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
//#define all(a) std::begin(a), std::end(a)
//#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())
//
//#define sqr(x) ((x) * (x))
//#define sqrt(x) sqrt(1.0 * (x))
//#define pow(x, n) pow(1.0 * (x), n)
//
//#define COMPARE(obj) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b)
//#define COMPARE_BY(obj, field) [&](const std::decay_t<decltype(obj)>& a, const std::decay_t<decltype(obj)>& b) { return a.field < b.field; }
//
//#define checkbit(n, b) (((n) >> (b)) & 1)
//#define setbit(n, b) ((n) | (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
//#define removebit(n, b) ((n) & ~(static_cast<std::decay<decltype(n)>::type>(1) << (b)))
//#define flipbit(n, b) ((n) ^ (static_cast<std::decay<decltype(n)>::type>(1) << (b)))
//inline int countBits(uint v){v=v-((v>>1)&0x55555555);v=(v&0x33333333)+((v>>2)&0x33333333);return((v+(v>>4)&0xF0F0F0F)*0x1010101)>>24;}
//inline int countBits(ull v){uint t=v>>32;uint p=(v & ((1ULL << 32) - 1)); return countBits(t) + countBits(p); }
//inline int countBits(ll v){return countBits((ull)v); }
//inline int countBits(int v){return countBits((uint)v); }
//unsigned int reverseBits(uint x){ x = (((x & 0xaaaaaaaa) >> 1) | ((x & 0x55555555) << 1)); x = (((x & 0xcccccccc) >> 2) | ((x & 0x33333333) << 2)); x = (((x & 0xf0f0f0f0) >> 4) | ((x & 0x0f0f0f0f) << 4)); x = (((x & 0xff00ff00) >> 8) | ((x & 0x00ff00ff) << 8)); return((x >> 16) | (x << 16)); }
//template<class T> inline int sign(T x){ return x > 0 ? 1 : x < 0 ? -1 : 0; }
//inline bool isPowerOfTwo(int x){ return (x != 0 && (x&(x - 1)) == 0); }
//constexpr ll power(ll x, int p) { return p == 0 ? 1 : (x * power(x, p - 1)); }
////template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
//unsigned long long rdtsc() { unsigned long long ret = 0;
//#ifdef __clang__
//    return __builtin_readcyclecounter();
//#endif
//#ifndef _MSC_VER
//    asm volatile("rdtsc" : "=A" (ret) : :);
//#endif
//    return ret; }
//// Fast IO ********************************************************************************************************
//const int __BS = 4096;
//static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
//template<class T = int> T readInt() {
//    auto c = [&]() { if (__ir == __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
//    c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();
//    bool m = false; if (*__ir == '-') ++__ir, c(), m = true;
//    T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
//    ++__ir; return m ? -r : r;
//}
//static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
//template<class T>
//void writeInt(T x, char endc = '\n') {
//    if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0';
//    char* s = __iw;
//    while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; }
//    char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f;
//    if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
//    if (endc) { *__iw++ = endc; }
//}
//template<class T>
//void writeStr(const T& str) {
//    int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; }
//}
//struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } };
//static __FL__ __flushVar__;
//
////STL output *****************************************************************************************************
//#define TT1 template<class T>
//#define TT1T2 template<class T1, class T2>
//#define TT1T2T3 template<class T1, class T2, class T3>
//TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
//TT1 ostream& operator << (ostream& os, const vector<T>& v);
//TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
//TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
//TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
//TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
//TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
//TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
//template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
//TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
//TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
//template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
//TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
//TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
//TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
//TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
//TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
//TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
//TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }
//
////STL input *****************************************************************************************************
//TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
//TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
//TT1 inline istream& operator >> (istream& os, vector<T>& v) {
//    if (v.size()) for (T& t : v) os >> t; else {
//        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
//        stringstream ss(s); while (ss >> obj) v.push_back(obj);
//    }
//    return os;
//}
//TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }
//
////Pair magic *****************************************************************************************************
//#define PT1T2 pair<T1, T2>
//TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
//TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
//TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
//TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }
//
//#undef TT1
//#undef TT1T2
//#undef TT1T2T3
//
//#define FREIN(FILE) freopen(FILE, "rt", stdin)
//#define FREOUT(FILE) freopen(FILE, "wt", stdout)
//#ifdef LOCAL
//#define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3;
//#define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++;
//#else
//#define BEGIN_PROFILE(idx, name)
//#define END_PROFILE
//#endif
//
//#define mod         1000000009
//const int USUAL_MOD = mod;
//template<class T> inline void normmod(T &x, T m = USUAL_MOD) { x %= m; if (x < 0) x += m; }
//template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m = USUAL_MOD) { T2 res = x + y; if (res >= m) res -= m; return res; }
//template<class T1, class T2, class T3 = int> inline void addmodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x += y; if (x >= m) x -= m; }
//template<class T1, class T2, class T3 = int> inline void submodfast(T1 &x, T2 y, T3 m = USUAL_MOD) { x -= y; if (x < 0) x += m; }
//inline ll mulmod(ll x, ll n, ll m){ x %= m; n %= m; ll r = x * n - ll(ld(x)*ld(n) / ld(m)) * m; while (r < 0) r += m; while (r >= m) r -= m; return r; }
//inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }
//inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
//template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }
//template<class T>
//T fast_gcd(T u, T v) {
//    int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1;
//        shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif;
//    } return std::max(u, v) << shl;
//}
//
//inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
//template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
//ll gcdex(ll a, ll b, ll& x, ll& y) {
//    if (!a) { x = 0; y = 1; return b; }
//    ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
//    return d;
//}
//template<class T> bool isPrime(T x) { if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
//    for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }
//bool millerRabin(long long n) {
//    if (n <= 1000) return isPrime(n);
//    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
//    for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;
//        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
//        for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
//        return false; nextp:;
//    } return true;
//}
//
//// Useful constants
//
////int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, #define T18         1000000000000000000LL
//#define INF         1011111111
//#define LLINF       1000111000111000111LL
//#define mod         1000000009
//#define fftmod      998244353
//#define EPS         (double)1e-10
//#define PI          3.14159265358979323846264
//#define link        asaxlajrewqwe
//#define rank        wahayawehasdakw
////*************************************************************************************
//
//
//int32_t solve();
//int32_t main(int argc, char** argv) {
//    ios_base::sync_with_stdio(0);cin.tie(0);
//#ifdef LOCAL
////    FREIN("input.txt");
////    FREOUT("out.txt");
//#endif
//    return solve();
//}
//
//using int128 = __int128;
//int128 abs(int128 x) {
//    return x < 0 ? -x : x;
//}
//int solve() {
//    {
//        ll x = 1;
//        double s = 0;
//        int cnt = 0;
//        for (int i = 2; i <= 1000; ++i) {
//            if (!isPrime(i)) continue;
//            if (x * 1.0 * i > 1e18) break;
//            x *= i;
//            s += 1.0 / i;
//            ++cnt;
//            DBN(cnt);
//        }
//        cout << s << endl;
//        return 0;
//    }
//    ll a, b, s;
//    cin >> a >> b >> s;
//    if (s == 0) { cout << (a == 0 || b == 0 ? "YES" : "NO") << endl; return 0; }
//    if (a == 0 && b == 0) { cout << "NO" << endl; return 0; }
//    if (a == 0 || b == 0) { cout << (s % max(a, b) == 0 ? "YES" : "NO") << endl; return 0; }
//    ll xx, yy;
//    ll g = gcdex(a, b, xx, yy);
//    if (s % g != 0) {
//        cout << "NO" << endl; return 0;
//    }
//    a /= g;
//    b /= g;
//    s /= g;
//    int128 x = xx;
//    int128 y = yy;
//    x *= s;
//    y *= s;
//    if (x < 0) {
//        auto t = (abs(x) + b - 1) / b;
//        x += t * b;
//        y -= t * a;
//    }
//    if (y < 0) {
//        auto t = (abs(y) + a - 1) / a;
//        x -= t * b;
//        y += t * a;
//    }
//    if (x < 0 || y < 0) {
//        cout << "NO" << endl; return 0;
//    }
//    auto t = x / b;
//    x -= t * b;
//    y += t * a;
//    int iter = 0;
//    while (y >= 0) {
//        if (gcd(x, y) == 1) {
//            cout << "YES" << endl;
//            return 0;
//        }
//        ++iter;
//        if (iter % 1000 == 0) {
//            if (clock() * 1.0 / CLOCKS_PER_SEC > 0.4) {
//                break;
//            }
//        }
//        x += b;
//        y -= a;
//    }
//    cout << "NO" << endl;
//    return 0;
//}

#pragma comment(linker, "/stack:200000000")
#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")

#define _CRT_SECURE_NO_WARNINGS
# include <iostream>
# include <cmath>
# include <algorithm>
# include <stdio.h>
# include <cstdint>
# include <cstring>
# include <string>
# include <cstdlib>
# include <vector>
# include <bitset>
# include <map>
# include <queue>
# include <ctime>
# include <stack>
# include <set>
# include <list>
# include <random>
# include <deque>
# include <functional>
# include <iomanip>
# include <sstream>
# include <fstream>
# include <complex>
# include <numeric>
# include <immintrin.h>
# include <cassert>
# include <array>
# include <tuple>

#ifdef LOCAL
# include <opencv2/core/core.hpp>
# include <opencv2/highgui/highgui.hpp>
# include <opencv2/imgproc/imgproc.hpp>
#endif

using namespace std;

// Let's define unordered map
# ifdef __GNUC__
# if __cplusplus > 199711L
# include <unordered_set>
# include <unordered_map>
# else
# include <tr1/unordered_map>
# include <tr1/unordered_set>
using namespace tr1;
# endif
# else
# include <unordered_map>
# include <unordered_set>
# endif

#define VA_NUM_ARGS(...) VA_NUM_ARGS_IMPL_((0,__VA_ARGS__, 6,5,4,3,2,1))
#define VA_NUM_ARGS_IMPL_(tuple) VA_NUM_ARGS_IMPL tuple
#define VA_NUM_ARGS_IMPL(_0,_1,_2,_3,_4,_5,_6,N,...) N
#define macro_dispatcher(macro, ...) macro_dispatcher_(macro, VA_NUM_ARGS(__VA_ARGS__))
#define macro_dispatcher_(macro, nargs) macro_dispatcher__(macro, nargs)
#define macro_dispatcher__(macro, nargs) macro_dispatcher___(macro, nargs)
#define macro_dispatcher___(macro, nargs) macro ## nargs
#define DBN1(a)           cerr<<#a<<"="<<(a)<<"\n"
#define DBN2(a,b)         cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBN3(a,b,c)       cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
#define DBN4(a,b,c,d)     cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<"\n"
#define DBN5(a,b,c,d,e)   cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<"\n"
#define DBN6(a,b,c,d,e,f) cerr<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<", "<<#d<<"="<<(d)<<", "<<#e<<"="<<(e)<<", "<<#f<<"="<<(f)<<"\n"
#define DBN(...) macro_dispatcher(DBN, __VA_ARGS__)(__VA_ARGS__)
#define DA(a,n) cerr<<#a<<"=["; printarray(a,n); cerr<<"]\n"
#define DAR(a,n,s) cerr<<#a<<"["<<s<<"-"<<n-1<<"]=["; printarray(a,n,s); cerr<<"]\n"

#ifdef _MSC_VER
#define PREFETCH(ptr, rw, level) ((void)0)
#else
#define PREFETCH(ptr, rw, level) __builtin_prefetch(ptr, rw, level)
#endif

#ifdef LOCAL
#define CURTIME() cerr << clock() * 1.0 / CLOCKS_PER_SEC << endl
#else
#define CURTIME()
#endif

#define mp make_pair
typedef long long ll;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef int itn;

template<class T1, class T2, class T3>
struct triple{ T1 a; T2 b; T3 c; triple() : a(T1()), b(T2()), c(T3()) {}; triple(T1 _a, T2 _b, T3 _c) :a(_a), b(_b), c(_c){} };
template<class T1, class T2, class T3>
bool operator<(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a<t2.a;else if(t1.b!=t2.b)return t1.b<t2.b;else return t1.c<t2.c;}
template<class T1, class T2, class T3>
bool operator>(const triple<T1,T2,T3>&t1,const triple<T1,T2,T3>&t2){if(t1.a!=t2.a)return t1.a>t2.a;else if(t1.b!=t2.b)return t1.b>t2.b;else return t1.c>t2.c;}
#define tri triple<int,int,int>
#define trll triple<ll,ll,ll>

#define FI(n) for(int i=0;i<(n);++i)
#define FJ(n) for(int j=0;j<(n);++j)
#define FK(n) for(int k=0;k<(n);++k)
#define FL(n) for(int l=0;l<(n);++l)
#define FQ(n) for(int q=0;q<(n);++q)
#define FOR(i,a,b) for(int i = (a), __e = (int) (b); i < __e; ++i)
#define all(a) std::begin(a), std::end(a)
#define reunique(v) v.resize(std::unique(v.begin(), v.end()) - v.begin())

#define sqr(x) ((x) * (x))
#define sqrt(x) sqrt(1.0 * (x))
#define pow(x, n) pow(1.0 * (x), n)

//template<class T1, class T2, class T3> T1 inline clamp(T1 x, const T2& a, const T3& b) { if (x < a) return a; else if (x > b) return b; else return x; }
unsigned long long rdtsc() { unsigned long long ret = 0;
#ifdef __clang__
    return __builtin_readcyclecounter();
#endif
#ifndef _MSC_VER
    asm volatile("rdtsc" : "=A" (ret) : :);
#endif
    return ret; }
// Fast IO ********************************************************************************************************
const int __BS = 4096;
static char __bur[__BS + 16], *__er = __bur + __BS, *__ir = __er;
template<class T = int> T readInt() {
    auto c = [&]() { if (__ir >= __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
    c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();
    bool m = false; if (*__ir == '-') ++__ir, c(), m = true;
    T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
    ++__ir; return m ? -r : r;
}
template<class T = double> T readFloat() {
    auto c = [&]() { if (__ir >= __er) std::fill(__bur, __bur + __BS, 0), cin.read(__bur, __BS), __ir = __bur; };
    c(); while (*__ir && (*__ir < '0' || *__ir > '9') && *__ir != '-') ++__ir; c();
    bool m = false; if (*__ir == '-') ++__ir, c(), m = true;
    T r = 0; while (*__ir >= '0' && *__ir <= '9') r = r * 10 + *__ir - '0', ++__ir, c();
    if (c(), *__ir == '.') {
        ++__ir; c();
        T t = 1;
        while (*__ir >= '0' && *__ir <= '9') t = t / 10, r += (*__ir - '0') * t, ++__ir, c();
    }
    ++__ir; return m ? -r : r;
}
static char __buw[__BS + 20], *__iw = __buw, *__ew = __buw + __BS;
template<class T>
void writeInt(T x, char endc = '\n') {
    if (x < 0) *__iw++ = '-', x = -x; if (x == 0) *__iw++ = '0';
    char* s = __iw;
    while (x) { T t = x / 10; char c = x - 10 * t + '0'; *__iw++ = c; x = t; }
    char* f = __iw - 1; while (s < f) swap(*s, *f), ++s, --f;
    if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw;
    if (endc) { *__iw++ = endc; }
}
template<class T>
void writeStr(const T& str) {
    int i = 0; while (str[i]) { *__iw++ = str[i++]; if (__iw > __ew) cout.write(__buw, __iw - __buw), __iw = __buw; }
}
struct __FL__ { ~__FL__() { if (__iw != __buw) cout.write(__buw, __iw - __buw); } };
static __FL__ __flushVar__;

//STL output *****************************************************************************************************
#define TT1 template<class T>
#define TT1T2 template<class T1, class T2>
#define TT1T2T3 template<class T1, class T2, class T3>
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p);
TT1 ostream& operator << (ostream& os, const vector<T>& v);
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, const multiset<T1, T2>&v);
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v);
TT1T2T3 ostream& operator << (ostream& os, const map<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const multimap<T1, T2, T3>& v);
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t);
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v);
TT1T2 ostream& operator << (ostream& os, const pair<T1, T2>& p){ return os <<"("<<p.first<<", "<< p.second<<")"; }
TT1 ostream& operator << (ostream& os, const vector<T>& v){       bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
template<class T, size_t N> ostream& operator << (ostream& os, const array<T, N>& v) {     bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const set<T1, T2>&v){    bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multiset<T1,T2>&v){bool f=1;os<<"[";for(auto& i : v) { if (!f)os << ", ";os<<i;f=0;}return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const map<T1,T2,T3>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, const multimap<T1, T2>& v){ bool f = 1; os << "["; for (auto& ii : v) { if (!f)os << ", "; os << "(" << ii.first << " -> " << ii.second << ") "; f = 0; }return os << "]"; }
TT1T2 ostream& operator << (ostream& os, priority_queue<T1, T2> v) { bool f = 1; os << "["; while (!v.empty()) { auto x = v.top(); v.pop(); if (!f) os << ", "; f = 0; os << x; } return os << "]"; }
TT1T2T3 ostream& operator << (ostream& os, const triple<T1, T2, T3>& t){ return os << "(" << t.a << ", " << t.b << ", " << t.c << ")"; }
TT1T2 void printarray(const T1& a, T2 sz, T2 beg = 0){ for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }

//STL input *****************************************************************************************************
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t);
TT1T2 inline istream& operator >> (istream& os, pair<T1, T2>& p) { return os >> p.first >> p.second; }
TT1 inline istream& operator >> (istream& os, vector<T>& v) {
    if (v.size()) for (T& t : v) os >> t; else {
        string s; T obj; while (s.empty()) {getline(os, s); if (!os) return os;}
        stringstream ss(s); while (ss >> obj) v.push_back(obj);
    }
    return os;
}
TT1T2T3 inline istream& operator >> (istream& os, triple<T1, T2, T3>& t) { return os >> t.a >> t.b >> t.c; }

//Pair magic *****************************************************************************************************
#define PT1T2 pair<T1, T2>
TT1T2 inline PT1T2 operator+(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first + p2.first, p1.second + p2.second); }
TT1T2 inline PT1T2& operator+=(PT1T2 &p1 , const PT1T2 &p2) { p1.first += p2.first, p1.second += p2.second; return p1; }
TT1T2 inline PT1T2 operator-(const PT1T2 &p1 , const PT1T2 &p2) { return PT1T2(p1.first - p2.first, p1.second - p2.second); }
TT1T2 inline PT1T2& operator-=(PT1T2 &p1 , const PT1T2 &p2) { p1.first -= p2.first, p1.second -= p2.second; return p1; }

#undef TT1
#undef TT1T2
#undef TT1T2T3

#define FREIN(FILE) freopen(FILE, "rt", stdin)
#define FREOUT(FILE) freopen(FILE, "wt", stdout)
#ifdef LOCAL
#define BEGIN_PROFILE(idx, name) int profileIdx = idx; profileName[profileIdx] = name; totalTime[profileIdx] -= rdtsc() / 1e3;
#define END_PROFILE totalTime[profileIdx] += rdtsc() / 1e3; totalCount[profileIdx]++;
#else
#define BEGIN_PROFILE(idx, name)
#define END_PROFILE
#endif

template<class T> inline void normmod(T &x, T m) { x %= m; if (x < 0) x += m; }
template<class T1, class T2> inline T2 summodfast(T1 x, T1 y, T2 m) { T2 res = x + y; if (res >= m) res -= m; return res; }
template<class T1, class T2, class T3> inline void addmodfast(T1 &x, T2 y, T3 m) { x += y; if (x >= m) x -= m; }
template<class T1, class T2, class T3> inline void submodfast(T1 &x, T2 y, T3 m) { x -= y; if (x < 0) x += m; }
#if INTPTR_MAX == INT32_MAX or !defined(__SIZEOF_INT128__)
inline ll mulmod(ll x, ll n, ll m){ ll r = 0; normmod(x, m); normmod(n, m); while (n) { if (n & 1) r += x; x += x; if (r >= m) r -= m; if (x >= m) x -= m; n /= 2; } return r; }
#else
using int128 = __int128;
inline ll mulmod(ll x, ll n, ll m){ return __int128(x) * n % m; }
#endif
inline ll powmod(ll x, ll n, ll m){ ll r = 1; normmod(x, m); while (n){ if (n & 1) r *= x; x *= x; r %= m; x %= m; n /= 2; }return r; }
inline ll powmulmod(ll x, ll n, ll m) { ll res = 1; normmod(x, m); while (n){ if (n & 1)res = mulmod(res, x, m); x = mulmod(x, x, m); n /= 2; } return res; }
template<class T> inline T gcd(T a, T b) { while (b) { T t = a % b; a = b; b = t; } return a; }
template<class T>
T fast_gcd(T u, T v) {
    int shl = 0; while ( u && v && u != v) { T eu = u & 1; u >>= eu ^ 1; T ev = v & 1; v >>= ev ^ 1;
        shl += (~(eu | ev) & 1); T d = u & v & 1 ? (u + v) >> 1 : 0; T dif = (u - v) >> (sizeof(T) * 8 - 1); u -= d & ~dif; v -= d & dif;
    } return std::max(u, v) << shl;
}

inline ll lcm(ll a, ll b){ return a / gcd(a, b) * b; }
template<class T> inline T gcd(T a, T b, T c){ return gcd(gcd(a, b), c); }
ll gcdex(ll a, ll b, ll& x, ll& y) {
    if (!a) { x = 0; y = 1; return b; }
    ll y1; ll d = gcdex(b % a, a, y, y1); x = y1 - (b / a) * y;
    return d;
}
template<class T> bool isPrime(T x) { if (x <= 4 || x % 2 == 0 || x % 3 == 0) return x == 2 || x == 3;
    for (T i = 5; i * i <= x; i += 6) if (x % i == 0 || x % (i + 2) == 0) return 0; return 1; }
bool millerRabin(long long n) {
    if (n <= 1000) return isPrime(n);
    long long s = n - 1; int t = 0; while (s % 2 == 0) s /= 2, ++t;
    for (int a : {2, 325, 9375, 28178, 450775, 9780504, 1795265022}) { if (!(a %= n)) return true;
        long long f = powmulmod(a, s, n); if (f == 1 || f == n - 1) continue;
        for (int i = 1; i < t; ++i) if ((f = mulmod(f, f, n)) == n - 1) goto nextp;
        return false; nextp:;
    } return true;
}

// Useful constants

//int some_primes[7] = {24443, 100271, 1000003, 1000333, 5000321, 98765431, 1000000123};
#define T9          1000000000
#define T18         1000000000000000000LL
#define INF         1011111111
#define LLINF       1000111000111000111LL
#define mod         1000000007
#define fftmod      998244353
#define EPS         (double)1e-10
#define PI          3.14159265358979323846264
#define link        asaxlajrewqwe
#define rank        wahayawehasdakw
//*************************************************************************************

int32_t solve();
int32_t main(int argc, char** argv) {
    ios_base::sync_with_stdio(0);cin.tie(0);
#ifdef LOCAL
//    FREIN("input.txt");
    //            FREOUT("out.txt");
#endif
    return solve();
}

int solve() {
    int n = readInt();
    FI(n) {
        ll p, q, b;
        p = readInt<ll>();
        q = readInt<ll>();
        b = readInt<ll>();
//        cin >> p >> q >> b;
        ll g = gcd(p, q);
        p /= g;
        q /= g;
        b = gcd(b, q);
        while (true) {
            ll t = gcd(b, q);
            if (t == 1) break;
            q /= t;
        }
        puts(p == 0 || q == 1 ? "Finite" : "Infinite");
    }

    return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

INT multi(INT x, INT y, INT mod){
	return (x * y - (INT)(x / (long double)mod * y + 1e-5) * mod + mod) % mod;
}

int power(INT a, INT b, INT m, int ans=1) {
	for (; b; b>>=1, a=multi(a,a,m)) if (b&1) ans=multi(ans, a, m);
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	INT p, q, b;
	scanf("%d", &n);
	while(n--){
		cin>>p>>q>>b;
		q /= __gcd(p, q);
		INT temp = __gcd(q, b);
		if(power(temp, 60, q) == 0) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	INT p, q, b;
	scanf("%d", &n);
	while(n--){
		cin>>p>>q>>b;
		q /= __gcd(p, q);
		while(1){
			q /= __gcd(q, b);
			if(__gcd(q, b) == 1) break;
		}
		if(q == 1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long INT;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		INT p,q,b;
		scanf("%I64d %I64d %I64d",&p,&q,&b);
		q/=__gcd(p,q);
		INT c=__gcd(q,b);
		int flag=0;
		if(q==1){
			puts("Finite");
			continue;
		}
		if(c==1){
			puts("Infinite");
			continue;
		}
		while(b>=c){
			if(b%c!=0){
				flag=1;
				break;
			}
			b/=c;
		}
		if(flag){
			puts("Infinite");
		}
		else puts("Finite");
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long INT;

int main(){

	int T;

	cin >> T;

	while(T--) {

		INT p, q, b;

		scanf("%I64d %I64d %I64d", &p, &q, &b);

		INT D = __gcd(p, q);

		q /= D;

		int step = 70;

		while(step--) {

			INT d = __gcd(q, b);

			if(d == 1) break;

			q /= d;

			b = d;

		}

		if(q == 1) puts("Finite");

		else puts("Infinite");

	}

}


#include<bits/stdc++.h>

using namespace std;

typedef long long INT;

INT multi(INT x,INT y,INT mod){
	return (x*y-(INT)(x/(long double)mod*y+1e-5)*mod+mod)%mod;
}

INT power(INT a,int b,INT m,INT ans=1){
	for(;b;b>>=1,a=multi(a,a,m))if(b&1)ans=multi(ans,a,m);
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		INT p,q,b;
		scanf("%I64d %I64d %I64d",&p,&q,&b);
		q/=__gcd(p,q);
		INT c=__gcd(q,b);
		int flag=0;
		if(q==1){
			puts("Finite");
			continue;
		}
		if(c==1){
			puts("Infinite");
			continue;
		}
		if(power(c,60,q)==0)puts("Finite");
		else puts("Infinite");
	}

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long INT;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		INT p,q,b;
		scanf("%I64d %I64d %I64d",&p,&q,&b);
		q/=__gcd(p,q);
		INT c=__gcd(q,b);
		int flag=0;
		if(q==1){
			puts("Finite");
			continue;
		}
		if(c==1){
			puts("Infinite");
			continue;
		}
		while(q>=c){
			if(q%c!=0){
				flag=1;
				break;
			}
			q/=c;
		}

		if(flag){
			puts("Infinite");
		}

		else {
			if(q==1)puts("Finite");
			else {
				int t=q;
				while(q<c)q*=t;
				if(q%c!=0)puts("Infinite");
				else puts("Finite");
				continue;
			}
		}
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	INT p, q, b;
	scanf("%d", &n);
	while(n--){
		cin>>p>>q>>b;
		q /= __gcd(p, q);
		INT temp = __gcd(q, b);
		while(1){
			if(temp == 1) break;
			if(q % temp == 0) {q /= temp; continue;}
			temp = __gcd(q, b);
		}
		if(q == 1) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

INT multi(INT x, INT y, INT mod){
	return (x * y - (INT)(x / (long double)mod * y + 1e-5) * mod + mod) % mod;
}

int power(INT a, int b, INT m, int ans=1) {
	for (; b; b>>=1, a=multi(a,a,m)) if (b&1) ans=multi(ans, a, m);
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	INT p, q, b;
	scanf("%d", &n);
	while(n--){
		scanf("%I64d%I64d%I64d", &p, &q, &b);
		q /= __gcd(p, q);
		INT temp = __gcd(q, b);
		if(power(temp, 60, q) == 0) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long INT;

int main(){

	int T;

	cin >> T;

	while(T--) {

		INT p, q, b;

		scanf("%I64d %I64d %I64d", &p, &q, &b);

		INT D = __gcd(p, q);

		q /= D;

		int step = 70;

		while(step--) {

			INT d = __gcd(q, b);

			if(d == 1) break;

			q /= d;

		}

		if(q == 1) puts("Finite");

		else puts("Infinite");

	}

}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

INT multi(INT x, INT y, INT mod){
	return (x * y - (INT)(x / (long double)mod * y + 1e-5) * mod + mod) % mod;
}

INT power(INT a, int b, INT m, INT ans=1) {
	for (; b; b>>=1, a=multi(a,a,m)) if (b&1) ans=multi(ans, a, m);
	return ans;
}

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	INT p, q, b;
	scanf("%d", &n);
	while(n--){
		scanf("%I64d%I64d%I64d", &p, &q, &b);
		q /= __gcd(p, q);
		INT temp = __gcd(q, b);
		if(power(temp, 60, q) == 0) puts("Finite");
		else puts("Infinite");
	}
	return 0;
}


#include<bits/stdc++.h>

using namespace std;

typedef long long INT;

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
	freopen("out.out","w",stdout);
#endif
	int n;
	cin>>n;
	for(int i=1;i<=n;i++){
		INT p,q,b;
		scanf("%I64d %I64d %I64d",&p,&q,&b);
		q/=__gcd(p,q);
		INT c=__gcd(q,b);
		int flag=0;
		if(q==1){
			puts("Finite");
			continue;
		}
		if(c==1){
			puts("Infinite");
			continue;
		}
		while(q>=c){
			if(q%c!=0){
				flag=1;
				break;
			}
			q/=c;
		}
		if(flag){
			puts("Infinite");
		}
		else puts("Finite");
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    long long p, q, b, temp;
    for (int i = 0; i < n; i++)
    {
        cin >> p >> q >> b;
        p %= q;
        q /= gcd(p, q);
        for (int j = 0; j < 65 || temp != 1; j++)
        {
            temp = gcd(q, b);
            q /= temp;
        }
        if (min(q, b) == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        long long p, q, b;
        cin >> p >> q >> b;
        q /= __gcd(p, q);
        long long temp = __gcd(q, b);
        q /= temp;
        b /= temp;
        if (min(q, b) == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        long long p, q, b;
        cin >> p >> q >> b;
        p %= q;
        q /= gcd(p, q);
        for (int j = 0; j < 70; j++)
        {
            long long temp = gcd(q, b);
            q /= temp;
        }
        if (min(q, b) == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    long long p, q, b, temp;
    for (int i = 0; i < n; i++)
    {
        cin >> p >> q >> b;
        p %= q;
        q /= gcd(p, q);
        temp = gcd(q, b);
        for (int j = 0; j < 65 && temp != 1; j++)
        {
            temp = gcd(q, temp);
            q /= temp;
        }
        if (q == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        long long p, q, b;
        cin >> p >> q >> b;
        p %= q;
        q /= __gcd(p, q);
        long long temp = __gcd(q, b);
        q /= temp;
        b /= temp;
        if (min(q, b) == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    long long p, q, b, temp;
    for (int i = 0; i < n; i++)
    {
        cin >> p >> q >> b;
        p %= q;
        q /= gcd(p, q);
        for (int j = 0; j < 65; j++)
        {
            temp = gcd(q, b);
            q /= temp;
        }
        if (min(q, b) == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    int n;
    cin >> n;
    for (int i = 0; i < n; i++)
    {
        long long p, q, b;
        cin >> p >> q >> b;
        p %= q;
        q /= gcd(p, q);
        long long temp = gcd(q, b);
        q /= temp;
        b /= temp;
        if (min(q, b) == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

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

int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    long long p, q, b, temp;
    for (int i = 0; i < n; i++)
    {
        cin >> p >> q >> b;
        p %= q;
        q /= gcd(p, q);
        for (int j = 0; j < 65 && temp != 1; j++)
        {
            temp = gcd(q, temp);
            q /= temp;
        }
        if (q == 1)
        {
            cout << "Finite\n";
        }
        else
        {
            cout << "Infinite\n";
        }
    }
    return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

inline ll mul(ll a,ll b,ll p){
  a%=p; b%=p;
  return ((a*b-(ll)((ll)((long double)a/p*b+1e-3)*p))%p+p)%p;
}

inline ll Pow(ll x,ll y,ll p){
  ll ret=1;
  for(;y;y>>=1,x=mul(x,x,p)) if(y&1) ret=mul(ret,x,p);
  return ret;
}

ll gcd(ll x,ll y){
  return y?gcd(y,x%y):x;
}

int main(){
#ifdef ljn
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
#endif
  int n; scanf("%d",&n);
  while(n--){
    ll p,q,b;
    scanf("%lld%lld%lld",&p,&q,&b); p%=q;
    int cnt=1; ll _b=b,g=gcd(p,q); p/=g; q/=g;
    b=Pow(b,1000,q);
    p=mul(p,b,q);
    puts(p?"Infinite":"Finite");
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

inline ll mul(ll a,ll b,ll p){
  a%=p; b%=p;
  return ((a*b-(ll)((ll)((long double)a/p*b+1e-3)*p))%p+p)%p;
}

inline ll Pow(int x,int y,int p){
  ll ret=1;
  for(;y;y>>=1,x=mul(x,x,p)) if(y&1) ret=mul(ret,x,p);
  return ret;
}

int main(){
#ifdef ljn
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
#endif
  int n; scanf("%d",&n);
  while(n--){
    ll p,q,b;
    scanf("%lld%lld%lld",&p,&q,&b); p%=q;
    int cnt=1; ll _b=b;
    while(b<q/_b) _b*=b,cnt++;
    b=Pow(b,cnt,q);
    p=mul(p,b,q);
    puts(p?"Infinite":"Finite");
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

inline ll mul(ll a,ll b,ll p){
  a%=p; b%=p;
  return ((a*b-(ll)((ll)((long double)a/p*b+1e-3)*p))%p+p)%p;
}

inline ll Pow(int x,int y,int p){
  ll ret=1;
  for(;y;y>>=1,x=mul(x,x,p)) if(y&1) ret=mul(ret,x,p);
  return ret;
}

int main(){
#ifdef ljn
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
#endif
  int n; scanf("%d",&n);
  while(n--){
    ll p,q,b;
    scanf("%lld%lld%lld",&p,&q,&b);
    int cnt=1; ll _b=b;
    while(b<p/_b) _b*=b,cnt++;
    b=Pow(b,cnt,p);
    q=mul(q,b,p);
    puts(q?"Infinite":"Finite");
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

inline ll mul(ll a,ll b,ll p){
  a%=p; b%=p;
  return ((a*b-(ll)((ll)((long double)a/p*b+1e-3)*p))%p+p)%p;
}

inline ll Pow(ll x,ll y,ll p){
  ll ret=1;
  for(;y;y>>=1,x=mul(x,x,p)) if(y&1) ret=mul(ret,x,p);
  return ret;
}

int main(){
#ifdef ljn
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
#endif
  int n; scanf("%d",&n);
  while(n--){
    ll p,q,b;
    scanf("%lld%lld%lld",&p,&q,&b); p%=q;
    int cnt=1; ll _b=b;
    while(b<=q/_b) _b*=b,cnt++; cnt++;
    b=Pow(b,cnt,q);
    p=mul(p,b,q);
    puts(p?"Infinite":"Finite");
  }
  return 0;
}


#include <cstdio>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

inline ll mul(ll a,ll b,ll p){
  a%=p; b%=p;
  return ((a*b-(ll)((ll)((long double)a/p*b+1e-3)*p))%p+p)%p;
}

inline ll Pow(int x,int y,int p){
  ll ret=1;
  for(;y;y>>=1,x=mul(x,x,p)) if(y&1) ret=mul(ret,x,p);
  return ret;
}

int main(){
#ifdef ljn
  freopen("1.in","r",stdin);
  freopen("1.out","w",stdout);
#endif
  int n; scanf("%d",&n);
  while(n--){
    ll p,q,b;
    scanf("%lld%lld%lld",&p,&q,&b); p%=q;
    int cnt=1; ll _b=b;
    while(b<=q/_b) _b*=b,cnt++; cnt++;
    b=Pow(b,cnt,q);
    p=mul(p,b,q);
    puts(p?"Infinite":"Finite");
  }
  return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 2000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	d = b;
	for(int i = 0; i < 100 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, d);
		q /= g, d /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 2000000000000000007;
long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	for(int i = 0; i < 100 && __gcd(q, b) != 1; i++){
		q /= __gcd(q, b);
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	if(!p){
		cout << "Finite" << endl;
		return;
	}

	g = __gcd(p, q);
	p /= g, q /= g;

	d = b;
	for(int i = 0; i < 100 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, d);
		q /= g, d /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	cout << (__gcd(q, b) == q || __gcd(q, b) == b ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	cout << (q % b == 0 || q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	g = __gcd(q, b);
	for(int i = 0; i < 64; i++){
		if(q % g == 0) q /= g;
		else{
			q /= __gcd(q, g);
			break;
		}
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	d = 1;
	while(d < inf / b) d *= b;
	for(int i = 0; i < 30 && __gcd(q, d) != 1; i++){
		q /= __gcd(q, d);
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	d = b;
	for(int i = 0; i < 100 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, d);
		q /= g, d /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	g = __gcd(q, b);
	for(int i = 0; i < 30 && q != 1; i++){
		q /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	for(int i = 0; i < 50 && q != 1; i++){
		while(b < inf / b) b *= b;
		g = __gcd(q, b);
		q /= g, b /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	for(int i = 0; i < 60 && __gcd(q, b) != 1; i++){
		q /= __gcd(q, b);
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	if(!p){
		cout << "finite" << endl;
		return;
	}

	g = __gcd(p, q);
	p /= g, q /= g;

	d = b;
	for(int i = 0; i < 100 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, d);
		q /= g, d /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	d = b;
	for(int i = 0; i < 70 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, d);
		q /= g, d /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	long long d = b;
	for(int i = 0; i < 70 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, b);
		q /= g, b /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	g = __gcd(q, b);
	for(int i = 0; i < 64 && q != 1; i++){
		q /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g, d;

void answer(){
	cin >> p >> q >> b;

	if(!p){
		cout << "infinite" << endl;
		return;
	}

	g = __gcd(p, q);
	p /= g, q /= g;

	d = b;
	for(int i = 0; i < 100 && q != 1; i++){
		while(d < inf / b) d *= b;
		g = __gcd(q, d);
		q /= g, d /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

const long long inf = 1000000000000000007;
long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	for(int i = 0; i < 1000 && q != 1; i++){
		while(b < inf / b) b *= b;
		g = __gcd(q, b);
		q /= g, b /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


/*
This is code of SuperJ6 for Codeforces.
Don't copy my code during f*kin' contests.

2.71828182845904523536
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
#define endl '\n'
#define pi pair<int, int>

long long p, q, b;
long long g;

void answer(){
	cin >> p >> q >> b;

	g = __gcd(p, q);
	p /= g, q /= g;

	g = __gcd(q, b);
	for(int i = 0; i < 100 && q != 1; i++){
		if(q % g != 0) g = __gcd(q, g);
		q /= g;
	}

	cout << (q == 1 ? "Finite" : "Infinite") << endl;
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(NULL);

	int t;
	cin >> t;

	for(int i = 0; i < t; i++) answer();

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b;
	cin>>n;
	while(n--){
		cin>>p>>q>>b;
		ll prod = p;
		int flag=1;
		while(prod%q!=0){
			ll temp = prod*b;
			if(temp/b != prod){
				flag=0;
				break;
			}
			prod=temp;
		}

		if(flag)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

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

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b,temp,prod;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p>>q>>b;
		q = q/gcd(p,q);
		ll r = gcd(q,b);
		while(r!=1 && q%r==0){
			q=q/r;
			r = gcd(q,b);
		}

		if(q==1)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

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

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b,temp,prod;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p>>q>>b;
		q = q/gcd(p,q);
		ll r = gcd(q,b);
		while(r!=1 && q%r==0){
			q=q/r;
			r = gcd(q,r);
		}

		if(q==1)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

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

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b,temp,prod;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p>>q>>b;
		q /= gcd(p,q);
		int flag=1;
		prod=1;
		while(prod%q!=0){
			temp = prod*b;
			if((temp/b != prod) || (temp%b != 0)){
				flag=0;
				break;
			}
			prod=temp;
		}

		if(flag)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

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

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b,temp,prod;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p>>q>>b;
		q /= gcd(p,q);
		ll r = gcd(q,b);
		while(r!=1 && q%r==0)
			q=q/r;
		if(q==1)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

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

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b,temp,prod;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p>>q>>b;
		q = q/gcd(p,q);
		ll r = gcd(q,b);

		while(r!=1 && q%r==0){
			q=q/r;
		}

		if(r%q==0)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define pb push_back
#define mp make_pair
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAXN 200005
#define MOD 1000000007

int main(){
	ios_base::sync_with_stdio(0);cin.tie(0);

	ll n,p,q,b;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>p>>q>>b;
		ll prod = p;
		int flag=1;
		while(prod%q!=0){
			ll temp = prod*b;
			if(temp/b != prod){
				flag=0;
				break;
			}
			prod=temp;
		}

		if(flag)
			cout<<"Finite\n";
		else
			cout<<"Infinite\n";
	}
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT{
    __INIT(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(15);
    }
}__init;

ll gcd(ll a,ll b){
    if(a<b) return gcd(b,a);
    ll r;
    while((r=a%b)){
        a = b;
        b = r;
    }
    return b;
}

int main(void){
    //map<ll,ll> test = primefact(11);
    //for(pl ngo:test) cout<<ngo.first<<" "<<ngo.second<<endl;
    ll n;
    cin>>n;
    rep(i,n){
        ll a,b,c;
        cin>>a>>b>>c;
        ll ab = gcd(a,b);
        b /= ab;
        if(b == 1){
            cout<<"Finite"<<endl;
            continue;
        }
        if(gcd(b,c) == b){
            cout<<"Finite"<<endl;
        }
        else{
            cout<<"Infinite"<<endl;
        }
    }
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT{
    __INIT(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(15);
    }
}__init;

map<ll,ll> primefact(ll n){
    ll a = 2;
    map<ll,ll> ret;
    while(a*a <= n){
        if(n%a == 0){
            n /= a;
            ret[a]++;
        }
        else{
            a++;
        }
    }
    ret[n]++;
    return ret;
}

int main(void){
    //map<ll,ll> test = primefact(100000);
    //for(pl ngo:test) cout<<ngo.first<<" "<<ngo.second<<endl;
    ll n;
    cin>>n;
    rep(i,n){
        ll a,b,c;
        cin>>a>>b>>c;
        map<ll,ll> pa = primefact(a);
        map<ll,ll> pb = primefact(b);
        map<ll,ll> pc = primefact(c);
        for(pair<ll,ll> ngo:pc){
            pa[ngo.first] = inf;
        }
        bool dame = false;
        for(pair<ll,ll> ngo:pb){
            //cout<<ngo.first<<endl;
            if(ngo.first == 1) continue;
            if(pa[ngo.first] < ngo.second){
                cout<<"Infinite"<<endl;
                dame = true;
                break;
            }
        }
        if(dame == false){
            cout<<"Finite"<<endl;
        }
    }
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT{
    __INIT(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(15);
    }
}__init;

map<ll,ll> primefact(ll n){
    ll a = 2;
    map<ll,ll> ret;
    while(a*a <= n){
        if(n%a == 0){
            n /= a;
            ret[a]++;
        }
        else{
            a++;
        }
    }
    ret[n]++;
    return ret;
}

int main(void){
    //map<ll,ll> test = primefact(3*3*2);
    //for(pl ngo:test) cout<<ngo.first<<" "<<ngo.second<<endl;
    ll n;
    cin>>n;
    rep(i,n){
        ll a,b,c;
        cin>>a>>b>>c;
        if(a == 0){
            cout<<"Finite"<<endl;
            continue;
        }
        map<ll,ll> pa = primefact(a);
        map<ll,ll> pb = primefact(b);
        map<ll,ll> pc = primefact(c);
        for(pair<ll,ll> ngo:pc){
            pa[ngo.first] = inf;
        }
        bool dame = false;
        for(pair<ll,ll> ngo:pb){
            //cout<<ngo.first<<endl;
            if(pa[ngo.first] < ngo.second){
                cout<<"Infinite"<<endl;
                dame = true;
                break;
            }
        }
        if(dame == false){
            cout<<"Finite"<<endl;
        }
    }
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT{
    __INIT(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(15);
    }
}__init;

ll gcd(ll a,ll b){
    if(a<b) return gcd(b,a);
    ll r;
    while((r=a%b)){
        a = b;
        b = r;
    }
    return b;
}

int main(void){
    //map<ll,ll> test = primefact(11);
    //for(pl ngo:test) cout<<ngo.first<<" "<<ngo.second<<endl;
    ll n;
    cin>>n;
    rep(i,n){
        ll a,b,c;
        cin>>a>>b>>c;
        if(a == 0){
            cout<<"Finite"<<endl;
            continue;
        }
        ll ab = gcd(a,b);
        b /= ab;
        if(b == 1){
            cout<<"Finite"<<endl;
            continue;
        }
        ll dame = false;
        ll bc = gcd(b,c);
        if(bc == 1){
            cout<<"Infinite"<<endl;
            continue;
        }
        while(b > 1){
            if(b%bc == 0) b /= bc;
            else{
                dame = true;
                break;
            }
        }
        if(dame){
            cout<<"Infinite"<<endl;
        }
        else{
            cout<<"Finite"<<endl;
        }
    }
}


#include <iostream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <math.h>
#include <string>
#include <numeric>
#include <queue>
#include <cstdio>
#include <cstring>
#define ll long long
#define rep(i,n) for(ll i=0;i<n;++i)
#define rep1(i,n) for(ll i=1;i<n;++i)
#define mrep(i,n) for(ll i=n;i>=0;--i)
#define all(a) (a).begin(),(a).end()
#define vl vector<ll>
#define vvl vector<vector<ll> >
#define vb vector<bool>
#define vvb vector<vector<bool> >
#define pl pair<ll,ll>
#define inf 1001001001001001000
//#define mod 1000000007
#define mod 998244353
#define pi 3.1415926535
using namespace std;
struct __INIT{
    __INIT(){
        cin.tie(0);
        ios::sync_with_stdio(false);
        cout<<fixed<<setprecision(15);
    }
}__init;

ll gcd(ll a,ll b){
    if(a<b) return gcd(b,a);
    ll r;
    while((r=a%b)){
        a = b;
        b = r;
    }
    return b;
}

int main(void){
    //map<ll,ll> test = primefact(11);
    //for(pl ngo:test) cout<<ngo.first<<" "<<ngo.second<<endl;
    ll n;
    cin>>n;
    rep(i,n){
        ll a,b,c;
        cin>>a>>b>>c;
        ll ab = gcd(a,b);
        b /= ab;
        if(b == 1){
            cout<<"Finite"<<endl;
            continue;
        }
        while(c < b) c*=c;
        if(gcd(b,c) == b){
            cout<<"Finite"<<endl;
        }
        else{
            cout<<"Infinite"<<endl;
        }
    }
}