#include <bits/stdc++.h>
long long gcd(long long x, long long y) {
  if (x == 0 || y == 0)
    return (x + y);
  else
    return gcd((x < y ? x : y), (x > y ? x : y) % (x < y ? x : y));
}
void solve() {
  long long numerator, denominator, base;
  scanf("%I64d %I64d %I64d", &numerator, &denominator, &base);
  long long gcd_fraction = gcd(numerator, denominator);
  numerator /= gcd_fraction;
  denominator /= gcd_fraction;
  base = gcd(base, denominator);
  while (base > 1) {
    while (denominator % base == 0) denominator /= base;
    base = gcd(base, denominator);
  }
  printf(denominator == 1 ? "Finite\n" : "Infinite\n");
}
int main() {
  int no_of_queries;
  scanf("%d", &no_of_queries);
  while (no_of_queries--) solve();
  return 0;
}
