#include <bits/stdc++.h>
using namespace std;
template <typename node>
void read(node &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-') f = -1;
    c = getchar();
  }
  while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
  x *= f;
}
long long gcd(register long long x, register long long y) {
  while (y) {
    register long long tmp = x;
    x = y, y = tmp % y;
  }
  return x;
}
int main() {
  int T;
  read(T);
  while (T--) {
    register long long p, q, b;
    read(p), read(q), read(b);
    register long long tmp = gcd(p, q);
    q /= tmp, b = gcd(b, q);
    while (b > 1) {
      while (q % b == 0) q /= b;
      b = gcd(b, q);
    }
    if (q > 1)
      cout << "Infinite\n";
    else
      cout << "Finite\n";
  }
  return 0;
}
