#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() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  while (n--) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long d = gcd(p, q);
    p /= d;
    q /= d;
    d = gcd(q, b);
    while (d > 1) {
      q /= d;
      d = gcd(q, d);
    }
    if (q == 1) {
      cout << "Finite\n";
    } else
      cout << "Infinite\n";
  }
}
