#include <bits/stdc++.h>
using namespace std;
void inline precompute() {}
long long power(long long x, long long y) {
  long long res = 1;
  while (y > 0) {
    if (y & 1) res = (res * x);
    x = (x * x);
    y /= 2;
  }
  return res;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
void solve(long long tc) {
  long long p, q, b;
  cin >> p >> q >> b;
  q = q / gcd(p, q);
  long long k = gcd(q, b);
  while (k != 1) {
    while (q % k == 0) q /= k;
    k = gcd(q, b);
  }
  if (q == 1)
    cout << "Finite\n";
  else
    cout << "Infinite\n";
}
int32_t main(void) {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cout << fixed;
  cerr.tie(NULL);
  cout.precision(20);
  long long t = 1, ctr = 0;
  cin >> t;
  while (t--) {
    solve(ctr + 1);
    ctr++;
  }
}
