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