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