#include <bits/stdc++.h>
using namespace std;
long long int gcd(long long int a, long long int b) {
  return b ? gcd(b, a % b) : a;
}
void solve() {
  long long int p, q, b;
  scanf("%lld %lld %lld", &p, &q, &b);
  long long int g = gcd(q, p);
  q /= g;
  g = b;
  while (1) {
    g = gcd(q, b);
    if (g == 1) break;
    q /= g;
    while (q % g == 0) q /= g;
  }
  puts(q == 1 ? "Finite" : "Infinite");
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tc;
  scanf("%d", &tc);
  while (tc--) solve();
  return 0;
}
