#include <bits/stdc++.h>
int32_t geti32() {
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  bool neg = c == '-';
  int32_t r = neg ? 0 : c - '0';
  while (isdigit(c = getchar())) r = r * 10 + c - '0';
  return neg ? -r : r;
}
int64_t geti64() {
  char c;
  while (!isdigit(c = getchar()) && c != '-')
    ;
  bool neg = c == '-';
  int64_t r = neg ? 0 : c - '0';
  while (isdigit(c = getchar())) r = r * 10 + c - '0';
  return neg ? -r : r;
}
uint32_t getu32() {
  char c;
  while (!isdigit(c = getchar()))
    ;
  uint32_t r = c - '0';
  while (isdigit(c = getchar())) r = r * 10 + c - '0';
  return r;
}
uint64_t getu64() {
  char c;
  while (!isdigit(c = getchar()))
    ;
  uint64_t r = c - '0';
  while (isdigit(c = getchar())) r = r * 10 + c - '0';
  return r;
}
char getchr() {
  char c;
  while (!isgraph(c = getchar()))
    ;
  return c;
}
using namespace std;
template <typename T>
T stein(T m, T n) {
  if (!m || !n) return m | n;
  int p = 0;
  while (!(m & 1) && !(n & 1)) m >>= 1, n >>= 1, ++p;
  while (n) {
    while (!(m & 1)) m >>= 1;
    while (!(n & 1)) n >>= 1;
    if (m >= n) std::swap(m, n);
    n = (n - m) >> 1;
  }
  return m << p;
}
int main() {
  for (int32_t n = geti32(); n; --n) {
    uint64_t p = getu64(), q = getu64(), b = getu64();
    if (p == 0) {
      puts("Finite");
      continue;
    }
    uint64_t g = stein(p, q);
    p /= g;
    q /= g;
    if (q == 1) {
      puts("Finite");
      continue;
    }
    while ((g = stein(q, b)) != 1) {
      while (q % g == 0) q /= g;
    }
    if (q != 1 && g == 1) {
      puts("Infinite");
      continue;
    }
    puts(b % q ? "Infinite" : "Finite");
  }
}
