#include <bits/stdc++.h>
using namespace std;
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() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  while (n--) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long g = gcd(p, q);
    p = p / g;
    q = q / g;
    if (q == 1) {
      cout << "Finite" << endl;
    } else {
      long long remaining_q = q;
      while (remaining_q != 1) {
        long long g = gcd(remaining_q, b);
        if (g == 1) {
          break;
        }
        while (remaining_q % g == 0) remaining_q = remaining_q / g;
      }
      if (remaining_q == 1) {
        cout << "Finite" << endl;
      } else {
        cout << "Infinite" << endl;
      }
    }
  }
}
