#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (a < b) swap(a, b);
  return b ? gcd(b, a % b) : a;
}
string go(long long p, long long q, long long b) {
  long long g = gcd(p, q);
  q /= g;
  while ((g = gcd(q, b)) != 1) {
    while (!(q % g)) {
      q /= g;
    }
  }
  return q == 1 ? "Finite" : "Infinite";
}
int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int tc;
  long long p, q, b;
  cin >> tc;
  while (tc--) {
    cin >> p >> q >> b;
    cout << go(p, q, b) << '\n';
  }
  return 0;
}
