#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
int main() {
  long long qq;
  scanf("%I64d", &qq);
  while (qq--) {
    long long p, q, k;
    scanf("%I64d%I64d%I64d", &p, &q, &k);
    long long gcdd = gcd(p, q);
    p = p / gcdd, q = q / gcdd;
    int flag = 0;
    while (1) {
      long long tmp = gcd(k, q);
      if (tmp == 1) {
        if (q == 1) flag = 1;
        break;
      }
      while (q % tmp == 0) {
        q /= tmp;
      }
    }
    if (flag)
      puts("Finite");
    else
      puts("Infinite");
  }
  return 0;
}
