#include <bits/stdc++.h>
using namespace std;
int i, j, m, n;
long long p, q, b, temp;
long long gcd(long long a, long long b) {
  long long t;
  if (a > b) {
    t = a;
    a = b;
    b = t;
  }
  if ((b % a) == 0) return a;
  return gcd(b % a, a);
}
int main() {
  scanf("%d", &n);
  for (i = 1; i <= n; i++) {
    scanf("%lld%lld%lld", &p, &q, &b);
    if (p == 0) {
      printf("Finite\n");
      continue;
    }
    temp = gcd(q, p);
    q = q / temp;
    temp = b;
    while (true) {
      temp = gcd(q, temp);
      if (temp == 1) {
        if (q == 1)
          printf("Finite\n");
        else
          printf("Infinite\n");
        break;
      }
      q /= temp;
    }
  }
  return 0;
}
