#include <bits/stdc++.h>
using namespace std;
long long p, q, d, w;
long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }
int T;
int main() {
  for (scanf("%d", &T); T--;) {
    scanf("%I64d%I64d%I64d", &p, &q, &d);
    w = gcd(p, q);
    p /= w;
    q /= w;
    w = 2;
    while (q > 1 && w > 1) {
      w = gcd(q, d);
      if (w > 1)
        while (!(q % w)) q /= w;
    }
    if (q == 1) {
      puts("Finite");
      continue;
    }
    if (gcd(q, d) == 1)
      puts("Infinite");
    else
      puts("Finite");
  }
  return 0;
}
