#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() {
  int q;
  scanf("%d", &q);
  while (q--) {
    long long x, y, b;
    scanf("%lld %lld %lld", &x, &y, &b);
    long long fpb = gcd(x, y);
    x /= fpb;
    y /= fpb;
    long long now = gcd(y, b);
    while (now != 1) {
      while (y % now == 0) y /= now;
      now = gcd(b, y);
    }
    if (y == 1)
      printf("Finite\n");
    else
      printf("Infinite\n");
  }
}
