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