#include <bits/stdc++.h>
using namespace std;
int n;
long long p, q, b;
long long gcd(long long a, long long b) { return a == 0 ? b : gcd(b % a, a); }
long long read() {
  long long num = 0, w = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    num = num * 10 + ch - 48;
    ch = getchar();
  }
  return num * w;
}
int main() {
  n = read();
  for (int i = 0; i < n; i++) {
    p = read(), q = read(), b = read();
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    long long orq = -1;
    while (orq != q) {
      orq = q;
      g = gcd(q, b);
      while (q % g == 0) {
        if (g == 1) goto out;
        q /= g;
      }
    }
  out:
    if (q == 1)
      cout << "Finite\n";
    else
      cout << "Infinite\n";
  }
}
