#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
int main() {
  ios_base::sync_with_stdio(false);
  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 g = gcd(p, q);
    p /= g;
    q /= g;
    long long gg2 = gcd(q, b);
    if (p == 0) {
      cout << "Finite\n";
      continue;
    }
    if (q == 1) {
      cout << "Finite\n";
    } else if ((q == gg2) && gg2 != 1) {
      cout << "Finite\n";
    } else {
      while (true) {
        while (q % gg2 == 0 && gg2 != 1) {
          q /= gg2;
        }
        gg2 = gcd(q, gg2);
        if (gg2 == 1) {
          break;
        }
      }
      if (q == 1) {
        cout << "Finite\n";
      } else {
        cout << "Infinite\n";
      }
    }
  }
  return 0;
}
