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