#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
void fast_in_out() {
  std::ios_base::sync_with_stdio(NULL);
  cin.tie(NULL);
  cout.tie(NULL);
}
int dx[] = {0, 0, -1, 1, -1, 1, -1, 1, 0};
int dy[] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
const int N = 2e5 + 5, M = 500 + 5, mod = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int main() {
  fast_in_out();
  int t;
  cin >> t;
  while (t--) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    b = gcd(q, b);
    while (b != 1) {
      while (q % b == 0) q /= b;
      b = gcd(q, b);
    }
    cout << ((q == 1) ? "Finite\n" : "Infinite\n");
  }
  return 0;
}
