#include <bits/stdc++.h>
using namespace std;
void init_ios() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
}
long long n, p, q, b;
long long nwd(long long a, long long b) {
  if (a < b) swap(a, b);
  if (b == 0) return a;
  return nwd(b, a % b);
}
signed main() {
  init_ios();
  cin >> n;
  while (n--) {
    cin >> p >> q >> b;
    long long nw = nwd(p, q);
    q /= nw;
    if (p == 0 || q == 1) {
      cout << "Finite\n";
      continue;
    }
    nw = nwd(q, b);
    while (nw > 1) {
      while (q % nw == 0) q /= nw;
      nw = nwd(q, b);
    }
    if (q == 1)
      cout << "Finite\n";
    else
      cout << "Infinite\n";
  }
}
