#include <bits/stdc++.h>
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX = 1e5 + 10;
const long long INF = 0x7FFFFFFFFFFFFFFFLL;
const int inf = 0x3f3f3f3f;
const int mod = 1e9 + 7;
using namespace std;
long long gcd(long long a, long long b) {
  while (b ^= a ^= b ^= a %= b)
    ;
  return a;
}
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long p, q, b;
    scanf("%I64d %I64d %I64d", &p, &q, &b);
    long long GCD = b;
    q /= gcd(p, q);
    while (q != 1) {
      GCD = gcd(GCD, q);
      if (GCD == 1) break;
      q /= GCD;
    }
    if (q == 1)
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
