#include <bits/stdc++.h>
long long gcd(long long p, long long q) { return q ? gcd(q, p % q) : p; }
long long p, q, b, tmp, i, n;
int main() {
  for (i = 0, scanf("%lld", &n); i < n; i++) {
    scanf("%lld%lld%lld", &p, &q, &b);
    q = q / gcd(p, q);
    while ((tmp = gcd(q, b)) != 1)
      while (q % tmp == 0) q /= tmp;
    puts(q == 1 ? "Finite" : "Infinite");
  }
  return 0;
}
