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