#include <bits/stdc++.h>
using namespace std;
const string Infi = "Infinite";
const string Fi = "Finite";
const int maxMN = 105;
int n;
long long p, q, b;
void ReadInput() { cin >> n; }
long long GCD(long long a, long long b) {
  while (b != 0) {
    long long r = a % b;
    a = b;
    b = r;
  }
  return a;
}
void Solve() {
  long long k = GCD(p, q);
  if (k != 1) {
    p /= k;
    q /= k;
  }
  while (GCD(b, q) != 1) {
    q /= GCD(b, q);
    b = GCD(b, q);
  }
  if (q != 1)
    cout << Infi << '\n';
  else
    cout << Fi << '\n';
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  ReadInput();
  while (n-- > 0) {
    cin >> p >> q >> b;
    Solve();
  }
}
