#include <bits/stdc++.h>
using namespace std;
const int N = 1e6 + 100;
long long gcd(long long a, long long b) {
  while (b > 0) {
    long long aux = a;
    a = b;
    b = aux % b;
  }
  return a;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int tc;
  scanf("%d", &tc);
  for (; tc; tc--) {
    long long p, q, b;
    scanf("%lld %lld %lld", &p, &q, &b);
    if (p == 0 || q == 1) {
      printf("Finite\n");
      continue;
    }
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    while (q > 1) {
      long long g = gcd(b, q);
      if (g == 1) {
        break;
      }
      while (q % g == 0) q /= g;
    }
    if (q == 1) {
      printf("Finite\n");
    } else {
      printf("Infinite\n");
    }
  }
  return 0;
}
