#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  long long t;
  while (b) {
    t = b;
    b = a % b;
    a = t;
  }
  return a;
}
int main() {
  cin.sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  long long n, p, q, b;
  cin >> n;
  string ans;
  while (n--) {
    cin >> p >> q >> b;
    long long d = gcd(p, q);
    q /= d;
    d = gcd(b, q);
    while (d != 1 && q != 1) {
      while (q % d == 0) q /= d;
      d = gcd(q, b);
    }
    if (q == 1)
      cout << "Finite" << endl;
    else
      cout << "Infinite" << endl;
  }
  return 0;
}
