#include <bits/stdc++.h>
using namespace std;
const int INF = numeric_limits<int>::max();
long long foo;
long long gcd(long long a, long long b) {
  if (a < b) {
    foo = a;
    a = b;
    b = foo;
  }
  while (b != 0) {
    a %= b;
    foo = a;
    a = b;
    b = foo;
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    long long p, q, b;
    cin >> p >> q >> b;
    long long s = p, t = q;
    if (s < t) {
      foo = s;
      s = t;
      t = foo;
    }
    while (t != 0) {
      s %= t;
      foo = s;
      s = t;
      t = foo;
    }
    q /= s;
    while (true) {
      s = b, t = q;
      if (s < t) {
        foo = s;
        s = t;
        t = foo;
      }
      while (t != 0) {
        s %= t;
        foo = s;
        s = t;
        t = foo;
      }
      if (s == 1) break;
      while (q % s == 0) q /= s;
    }
    if (q == 1)
      cout << "Finite\n";
    else
      cout << "Infinite\n";
  }
}
