#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 5;
long long gcd(long long a, long long b) {
  if (b == 0LL) return a;
  return gcd(b, a % b);
}
bool check(long long q, long long b) {
  while (1) {
    long long t = gcd(q, b);
    if (t == 1LL) {
      if (q == 1LL)
        return true;
      else
        return false;
    } else
      q /= t;
    if (q == 1LL) return true;
    b = t;
  }
}
int main() {
  int n;
  long long p, q, b;
  scanf("%d", &n);
  while (n--) {
    scanf("%lld%lld%lld", &p, &q, &b);
    long long t = gcd(p, q);
    p /= t, q /= t;
    if (check(q, b))
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
