#include <bits/stdc++.h>
using namespace std;
int ar[100];
vector<pair<pair<int, int>, pair<int, int> > > v;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int check(long long a, long long b) {
  while (b != 1) {
    while (a % b == 0) a /= b;
    b = gcd(a, b);
  }
  if (a == 1) return 1;
  return 0;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n;
  cin >> n;
  while (n--) {
    long long p, q, b;
    cin >> p >> q >> b;
    if (p == 0) {
      cout << "Finite\n";
      continue;
    }
    long long hcf = gcd(p, q);
    p /= hcf;
    q /= hcf;
    int flg = check(q, b);
    if (!flg)
      cout << "Infinite\n";
    else
      cout << "Finite\n";
  }
}
