#include <bits/stdc++.h>
using namespace std;
const int N = 5010;
const int MOD = 998244353;
long long gcd(long long a, long long b) {
  if (!b) return a;
  return gcd(b, a % b);
}
int main() {
  int n;
  cin >> n;
  long long p, q, b;
  while (n--) {
    cin >> p >> q >> b;
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    g = gcd(b, q);
    while (g != 1) {
      q /= g;
      g = gcd(q, g);
    }
    if (q == 1)
      cout << "Finite";
    else
      cout << "Infinite";
    printf("\n");
  }
  return 0;
}
