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