#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC target("sse4")
const double PI = acos(-1.0);
const double EPS = 1e-6;
const long long INF = 1e9;
const long long LINF = 1e18;
const long long mod = 1e9 + 7;
const long long MAX = 1e5 + 47;
long long gcd(long long x, long long y) {
  long long x1, y1;
  while (y) {
    x1 = y;
    y1 = x % y;
    x = x1;
    y = y1;
  }
  return x;
}
string solve() {
  long long p, q, b;
  cin >> p >> q >> b;
  p %= q;
  if (p == 0) return "Finite";
  long long g = gcd(p, q);
  p /= g;
  q /= g;
  g = gcd(q, b);
  while (g != 1) {
    while (q % g == 0) q /= g;
    g = gcd(q, b);
  }
  if (q == 1)
    return "Finite";
  else
    return "Infinite";
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t;
  cin >> t;
  while (t--) cout << solve() << endl;
  return 0;
}
