#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (b) {
    long long t = a % b;
    a = b;
    b = t;
  }
  return a;
}
int main() {
  int n;
  long long p, q, b;
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &p, &q, &b);
    if (p % q == 0) {
      printf("Finite\n");
      continue;
    }
    q /= gcd(q, p);
    long long x = gcd(q, b);
    while (x != 1) {
      while (q % x == 0) q /= x;
      x = gcd(b, q);
    }
    if (q == 1)
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
}
