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