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