#include <bits/stdc++.h>
using namespace std;
const int M = 1e5 + 10;
int n;
long long p, q, b;
long long gcd(long long x, long long y) { return y ? gcd(y, x % y) : x; }
int main() {
  while (~scanf("%d", &n)) {
    for (int i = 0; i < n; ++i) {
      scanf("%lld%lld%lld", &p, &q, &b);
      if (p % q == 0) {
        printf("Finite\n");
      } else {
        long long yue = gcd(p, q);
        p /= yue, q /= yue;
        long long ans = gcd(q, b);
        while (ans > 1) {
          while (q % ans == 0) q /= ans;
          ans = gcd(q, b);
        }
        if (q == 1) {
          printf("Finite\n");
        } else {
          printf("Infinite\n");
        }
      }
    }
  }
}
