#include <bits/stdc++.h>
using namespace std;
int64_t gcd(int64_t x, int64_t y) { return y == 0 ? x : gcd(y, x % y); }
int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int64_t tt;
  cin >> tt;
  while (tt--) {
    int64_t p, q, b;
    cin >> p >> q >> b;
    int64_t d = gcd(p, q);
    p /= d;
    q /= d;
    int64_t t = (int64_t)1e18;
    while (t > 1) {
      t = gcd(q, b);
      q /= t;
      b = t;
    }
    if (t == 1 && q > 1 && p) {
      cout << "Infinite" << '\n';
    } else {
      cout << "Finite" << '\n';
    }
  }
  return 0;
}
