#include <bits/stdc++.h>
using namespace std;
inline 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);
}
inline void getll(long long &x) {
  char c = getchar();
  while (c < '0' || c > '9') c = getchar();
  x = 0;
  while (c >= '0' && c <= '9') {
    x = (x << 3) + (x << 1) + c - '0';
    c = getchar();
  }
}
int main() {
  long long n;
  getll(n);
  for (int i = 0; i < n; i++) {
    long long p, q, b;
    getll(p);
    getll(q);
    getll(b);
    long long gcd = _gcd(p, q);
    p /= gcd;
    q /= gcd;
    while (true) {
      gcd = _gcd(q, b);
      if (gcd <= 1) break;
      q /= gcd;
      b = gcd;
    }
    if (q == 1)
      puts("Finite");
    else
      puts("Infinite");
  }
  return 0;
}
