#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int x, long long int y) {
  long long int ma = max(x, y);
  long long int mi = min(x, y);
  if (ma % mi == 0) return mi;
  return gcd(mi, ma % mi);
}
int main() {
  ios::sync_with_stdio(0);
  int n;
  cin >> n;
  for (int N = 0; N < n; N++) {
    long long int p, q, b;
    cin >> p >> q >> b;
    if (p == 0) {
      cout << "Finite\n";
      continue;
    }
    long long int tmp = gcd(p, q);
    q /= tmp;
    tmp = b;
    while (true) {
      tmp = gcd(q, tmp);
      if (tmp == 1) break;
      while (q % tmp == 0) q /= tmp;
    }
    if (b % q == 0)
      cout << "Finite\n";
    else
      cout << "Infinite\n";
  }
  return 0;
}
