#include <bits/stdc++.h>
using namespace std;
const long long maxn = 1e3 + 100;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
};
long long p, q, b, temp, i, n;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  for (i = 0, cin >> n; i < n; i++) {
    cin >> p >> q >> b;
    q = q / gcd(p, q);
    while ((temp = gcd(q, b)) != 1)
      while (q % temp == 0) q /= temp;
    puts(q == 1 ? "Finite" : "Infinite");
  }
  return 0;
}
