#include <bits/stdc++.h>
using namespace std;
long long int n = 0, m = 0, ans = 0, sum = 0, cnt = 0, tmp = 0, ma = 0, mi = 0;
string s;
bool flag, ok;
long long int dx[4] = {1, -1, 0, 0}, dy[4] = {0, 0, 1, -1};
long long int ddx[9] = {1, -1, 0, 0, 1, 1, -1, -1, 0},
              ddy[9] = {0, 0, 1, -1, 1, -1, 1, -1, 0};
long long int gcd(long long int x, long long int y) {
  if (x < y) swap(x, y);
  return y ? gcd(y, x % y) : x;
}
int main(void) {
  cin >> n;
  long long int p, q, b;
  for (long long int i = (long long int)(0); i < (long long int)(n); i++) {
    scanf("%lld%lld%lld", &p, &q, &b);
    q /= gcd(p, q);
    tmp = gcd(b, q);
    while (q != 1) {
      if (tmp == 1) break;
      while (q % tmp == 0) {
        q /= tmp;
      }
      tmp = gcd(q, tmp);
    }
    if (q == 1) {
      ok = true;
    } else {
      ok = false;
    }
    if (ok) {
      printf("Finite\n");
    } else {
      printf("Infinite\n");
    }
  }
  return 0;
}
