#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long p, b, q;
    scanf("%lld%lld%lld", &p, &q, &b);
    if (p == 0) {
      puts("Finite");
      continue;
    }
    q = q / gcd(p, q);
    long long g = b;
    while (g != 1) {
      while (q % g == 0) q /= g;
      g = gcd(b, q);
    }
    if (q != 1) {
      puts("Infinite");
    } else {
      puts("Finite");
    }
  }
}
