#include <bits/stdc++.h>
using namespace std;
long long MOD = 1000000007;
string yes = "YES\n";
string no = "NO\n";
long long mod(long long a, long long prime = MOD) {
  return (prime + a) % prime;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n;
  cin >> n;
  while (n--) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long gc = gcd(p, q);
    p /= gc;
    q /= gc;
    long long g = gcd(b, q);
    while (b != 1) {
      while (q % b == 0) q /= b;
      b = gcd(q, b);
    }
    if (q == 1) {
      cout << "Finite\n";
    } else {
      cout << "Infinite\n";
    }
  }
  return 0;
}
