#include <bits/stdc++.h>
using namespace std;
const int N = 5e6 + 5;
const long long inf = 1791791791;
const long long mod = 1e9 + 7;
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long p, q, b;
    scanf("%lld %lld %lld", &p, &q, &b);
    q /= gcd(p, q);
    while (q != 1) {
      long long d = gcd(q, b);
      if (d == 1) {
        printf("Infinite\n");
        break;
      }
      while (q % d == 0) q /= d;
    }
    if (q == 1) printf("Finite\n");
  }
  return 0;
}
