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