#include <bits/stdc++.h>
using namespace std;
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);
  long long p, q, b;
  while (t--) {
    scanf("%lld%lld%lld", &p, &q, &b);
    q /= gcd(p, q);
    long long tmp = b;
    while (q != 1) {
      tmp = gcd(b, q);
      if (tmp == 1) break;
      while (q % tmp == 0) q /= tmp;
    }
    if (q == 1)
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
