#include <bits/stdc++.h>
long long int gcd(long long int a, long long int b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
long long int isFinite(long long int p, long long int q, long long int b) {
  long long int top, bot, g;
  bot = q;
  g = gcd(p, q);
  bot /= g;
  while ((g = gcd(bot, b)) > 1) {
    while (g < 1000000000 && bot % (g * g) == 0) {
      g = g * g;
    }
    bot /= g;
  }
  if (bot == 1) return 1;
  return 0;
}
int main() {
  int n;
  long long int p, q, b;
  scanf("%I64d", &n);
  while (n--) {
    scanf("%I64d%I64d%I64d", &p, &q, &b);
    if (isFinite(p, q, b))
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
