#include <bits/stdc++.h>
long long n, p, q, b;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
void run() {
  scanf("%I64d%I64d%I64d", &p, &q, &b);
  q /= gcd(p, q);
  while (b > 1) q /= (b = gcd(q, b));
  if (q == 1)
    printf("Finite\n");
  else
    printf("Infinite\n");
}
int main() {
  scanf("%I64d", &n);
  while (n--) {
    run();
  }
  return 0;
}
