#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1.0);
const int N = 10010;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
long long a[N], b[N];
int main(void) {
  int n;
  scanf("%d", &n);
  while (n--) {
    long long p, q, b;
    scanf("%I64d%I64d%I64d", &p, &q, &b);
    long long g = gcd(p, q);
    q = q / g;
    while (q > 1 && (g = gcd(q, b)) > 1) {
      p = g;
      while (q % p == 0) q /= p;
    }
    puts(q == 1 ? "Finite" : "Infinite");
  }
  return 0;
}
