#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long t, p, q, b;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> t;
  for (int i = 0; i < t; ++i) {
    cin >> p >> q >> b;
    long long d = gcd(p, q);
    p /= d, q /= d;
    long long g = gcd(q, b);
    while (q != 1ll && g != 1ll) {
      while (q % g == 0) q /= g;
      g = gcd(q, b);
    }
    if (q == 1ll)
      cout << "Finite" << endl;
    else
      cout << "Infinite" << endl;
  }
}
