#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  std::ios::sync_with_stdio(false), cin.tie(nullptr);
  long long t;
  cin >> t;
  long long p, q, b;
  for (int T = 0; T < t; T++) {
    cin >> p >> q >> b;
    long long gg = gcd(p, q);
    q /= gg;
    while (gg = gcd(q, b), gg != 1) {
      while (q % gg == 0) q /= gg;
    }
    if (q == 1)
      cout << "Finite" << endl;
    else
      cout << "Infinite" << endl;
  }
  return 0;
}
