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