#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  ll p, q, b;
  for (int i = 0; i < n; ++i) {
    cin >> p >> q >> b;
    ll d = gcd(p, q);
    q /= d;
    d = b;
    do {
      d = gcd(d, q);
      q /= d;
    } while (d > 1);
    if (q == 1) {
      cout << "Finite\n";
    } else {
      cout << "Infinite\n";
    }
  }
}
