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