#include <bits/stdc++.h>   using namespace std;   long long p, q, b, t, g;   long long gcd(long long a, long long b) {     if (a < b) swap(a, b);     while (b != 0) {       a %= b;       swap(a, b);     }     return a;   }   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cin >> t;     while (t--) {       cin >> p >> q >> b;       g = gcd(p, q);       q /= g;       while (true) {         g = gcd(b, q);         if (g == 1) break;         while (q % g == 0) q /= g;       }       if (q == 1)         cout <<  Finite ;       else         cout <<  Infinite ;       cout <<   n ;     }     return 0;   }   