#include <bits/stdc++.h>
using namespace std;
const int maxn = 300010;
long long p, q, b;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
bool ok() {
  long long m = gcd(p, q);
  q /= m;
  m = b;
  while (q != 1) {
    m = gcd(q, m);
    q /= m;
    if (m == 1) break;
  }
  if (q == 1) return true;
  return false;
}
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    scanf("%I64d%I64d%I64d", &p, &q, &b);
    if (!p || ok())
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
