#include <bits/stdc++.h>
using namespace std;
const int INF = 1e18;
const int N = 1e6 + 10;
long long a1[N], a2[N];
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
void solve() {
  long long p, q, b;
  scanf("%lld%lld%lld", &p, &q, &b);
  long long c = gcd(p, q);
  q = q / c;
  c = gcd(q, b);
  while (c != 1) {
    while (q % c == 0) q /= c;
    c = gcd(q, b);
  }
  if (q != 1) {
    printf("Infinite\n");
  } else
    printf("Finite\n");
}
int main() {
  int t;
  scanf("%d", &t);
  for (int i = 1; i <= t; i++) solve();
  return 0;
}
