#include <bits/stdc++.h>
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int main() {
  int n;
  long long p, q, b;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%I64d %I64d %I64d", &p, &q, &b);
    long long tmp = gcd(p, q);
    long long fur;
    p /= tmp;
    q /= tmp;
    b = gcd(q, b);
    while (b != 1) {
      tmp = b;
      fur = tmp * tmp;
      q /= tmp;
      while ((gcd(q, fur) == fur) && (tmp < (1LL << 16)) && (fur < q)) {
        tmp = fur;
        fur = tmp * tmp;
        q /= tmp;
      }
      b = gcd(q, b);
    }
    printf("%s\n", (q == 1) ? "Finite" : "Infinite");
  }
  return 0;
}
