#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
int main() {
  int T;
  cin >> T;
  while (T--) {
    long long p, q, b;
    scanf("%lld%lld%lld", &p, &q, &b);
    long long g = gcd(p, q);
    p /= g;
    q /= g;
    bool flag = 1;
    while (q != 1) {
      g = gcd(q, b);
      if (g == 1) {
        flag = 0;
        break;
      }
      while (q % g == 0) q /= g;
    }
    if (flag)
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
  return 0;
}
