#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long n, p, q, base;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> p >> q >> base;
    long long gecede = gcd(p, q);
    p /= gecede;
    q /= gecede;
    p %= q;
    long long tmp = gcd(q, base);
    while (tmp != 1) {
      while (q % tmp == 0) q /= tmp;
      tmp = gcd(q, tmp);
    }
    if (q == 1) {
      cout << "Finite\n";
    } else
      cout << "Infinite\n";
  }
}
