#include <bits/stdc++.h>
using namespace std;
int get() {
  char ch;
  while (ch = getchar(), (ch < '0' || ch > '9') && ch != '-')
    ;
  if (ch == '-') {
    int s = 0;
    while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
    return -s;
  }
  int s = ch - '0';
  while (ch = getchar(), ch >= '0' && ch <= '9') s = s * 10 + ch - '0';
  return s;
}
long long gcd(long long x, long long y) { return y == 0 ? x : gcd(y, x % y); }
int main() {
  for (int T = get(); T; T--) {
    long long p, q, b;
    scanf("%lld%lld%lld", &p, &q, &b);
    if (p == 0) {
      printf("Finite\n");
      continue;
    }
    long long d = gcd(p, q);
    q /= d;
    long long v = gcd(q, b);
    for (; v > 1;) {
      for (; q % v == 0;) q = q / v;
      v = gcd(q, b);
    }
    if (q == 1)
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
