#include <bits/stdc++.h>
using namespace std;
long long p, q, b, t, T;
long long GCD(long long x, long long y) { return (x % y) ? GCD(y, x % y) : y; }
inline long long read() {
  char ch;
  long long num, f = 0;
  while (!isdigit(ch = getchar())) f |= (ch == '-');
  num = ch - '0';
  while (isdigit(ch = getchar())) num = num * 10 + ch - '0';
  return f ? -num : num;
}
int main() {
  T = read();
  while (T--) {
    p = read();
    q = read();
    b = read();
    q /= GCD(p, q);
    while ((t = GCD(q, b)) != 1)
      while (q % t == 0) q /= t;
    puts(q == 1 ? "Finite" : "Infinite");
  }
  return 0;
}
