#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:160777216")
using namespace std;
int i, j, m, k, n, l, h, p;
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  if (b == 0) return a;
  return gcd(b, a % b);
}
int main() {
  std::ios::sync_with_stdio(false);
  cin >> n;
  while (n--) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    while (1) {
      long long t = gcd(q, b);
      if (t == 1) break;
      while (q % t == 0) q /= t;
    }
    if (q == 1) {
      puts("Finite");
    } else
      puts("Infinite");
  }
  return 0;
}
