#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  auto gcd = [](long long a, long long b) {
    long long r;
    while (b) {
      r = a % b;
      a = b;
      b = r;
    }
    return a;
  };
  int n;
  cin >> n;
  while (n--) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    while (q > 1) {
      g = gcd(q, b);
      if (g == 1) break;
      while (q % g == 0) q /= g;
    }
    if (q == 1)
      cout << "Finite\n";
    else
      cout << "Infinite\n";
  }
  return 0;
}
