#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main(void) {
  int T;
  scanf("%d", &T);
  long long p, q, b;
  while (T--) {
    scanf("%I64d%I64d%I64d", &p, &q, &b);
    if (p == 0) {
      puts("Finite");
      continue;
    }
    long long g = gcd(p, q);
    p /= g, q /= g;
    g = gcd(q, b);
    while (g != 1) {
      q /= g;
      g = gcd(q, g);
    }
    if (q == 1)
      puts("Finite");
    else
      puts("Infinite");
  }
  return 0;
}
