Problem: Mister B has a house in the middle of a giant plain field, which attracted aliens life. For convenience, aliens specified the Cartesian coordinate system on the field in such a way that Mister B's house has coordinates (0, 0). After that they sent three beacons to the field, but something went wrong. One beacon was completely destroyed, while the other two landed in positions with coordinates (m, 0) and (0, n), respectively, but shut down.

Mister B was interested in this devices, so he decided to take them home. He came to the first beacon, placed at (m, 0), lifted it up and carried the beacon home choosing the shortest path. After that he came to the other beacon, placed at (0, n), and also carried it home choosing the shortest path. When first beacon was lifted up, the navigation system of the beacons was activated.

Partially destroyed navigation system started to work in following way.

At time moments when both survived beacons are at points with integer coordinates the system tries to find a location for the third beacon. It succeeds if and only if there is a point with integer coordinates such that the area of the triangle formed by the two survived beacons and this point is equal to s. In this case the system sends a packet of information with beacon positions to aliens, otherwise it doesn't.

Compute how many packets of information system sent while Mister B was moving the beacons.

Input

The first line contains one integer t (1 ≤ t ≤ 1000) — the number of test cases. The next 3·t lines describe t test cases. 

Every test case is described in three lines as follows. Note that each parameter is given as a product of three factors.

The first line of a test case contains three space-separated integers: n1, n2, n3 (1 ≤ ni ≤ 106) such that n = n1·n2·n3.

The second line contains three space-separated integers: m1, m2, m3 (1 ≤ mi ≤ 106) such that m = m1·m2·m3.

The third line contains three space-separated integers: s1, s2, s3 (1 ≤ si ≤ 106) such that s = s1·s2·s3.

Note that for hacks only tests with t = 1 allowed.

Output

Print t integers one per line — the answers for each test.

Example

Input

3
2 1 1
2 1 1
1 1 3
1 5 1
2 2 1
1 1 2
10 6 18
2 103 2
13 1 13


Output

4
7
171

Note

First test case contains the following beacon positions: (2, 0) and (0, 2), s = 3. The following packets could be sent: ((2, 0), (0, 2), ( - 1, 0)), ((1, 0), (0, 2), (4, 0)), ((0, 0), (0, 2), (3, 1)), ((0, 0), (0, 1), ( - 6, 0)), where (b1, b2, p) has next description: b1 — first beacon position, b2 — second beacon position, p — some generated point.

Second test case contains the following beacon initial positions: (4, 0) and (0, 5), s = 2. The following packets could be sent: ((4, 0), (0, 5), (0, 4)), ((3, 0), (0, 5), (2, 3)), ((2, 0), (0, 5), (2, 2)), ((1, 0), (0, 5), (1, 4)), ((0, 0), (0, 4), (0, - 1)), ((0, 0), (0, 2), (2, 0)), ((0, 0), (0, 1), (4, 0)).

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
template <typename T>
inline bool chkmin(T &a, const T &b) {
  return a > b ? a = b, 1 : 0;
}
template <typename T>
inline bool chkmax(T &a, const T &b) {
  return a < b ? a = b, 1 : 0;
}
template <typename T>
inline bool smin(T &a, const T &b) {
  return a > b ? a = b : a;
}
template <typename T>
inline bool smax(T &a, const T &b) {
  return a < b ? a = b : a;
}
const long long N = 1e6 + 6, mod = (long long)0;
long long get_num(long long x0, long long x1, long long x2) {
  return x0 * x1 * x2;
}
set<long long> get_pr(long long first) {
  set<long long> res;
  for (long long j = 2; j * j <= first; ++j) {
    while (first % j == 0) {
      res.insert(j);
      first /= j;
    }
  }
  if (first != 1) res.insert(first);
  return res;
}
set<long long> get_pr(long long x0, long long x1, long long x2) {
  set<long long> a = get_pr(x0), b = get_pr(x1), c = get_pr(x2);
  for (long long first : b) a.insert(first);
  for (long long first : c) a.insert(first);
  return a;
}
long long divisors[N * 10];
long long get_div(long long s1, long long s2, long long s3, long long range) {
  set<long long> pr = get_pr(s1, s2, s3);
  long long s = get_num(s1, s2, s3);
  long long res = 0, sz = 1;
  divisors[0] = 1;
  for (long long first : pr) {
    long long cnt = 0;
    while (s % first == 0) {
      ++cnt;
      s /= first;
    }
    long long tsz = sz * (cnt + 1);
    for (long long j = sz; j < tsz; ++j) divisors[j] = divisors[j - sz] * first;
    sz = tsz;
  }
  ++range;
  for (long long j = 0; j < sz; ++j)
    if (divisors[j] <= range) ++res;
  return res;
}
long long a[N], b[N];
long long gcd(long long a, long long b) {
  return min(a, b) == 0 ? max(a, b) : gcd(b, a % b);
}
long long solve(set<long long> ns, long long range, long long s, long long n) {
  long long sz = 0;
  for (long long first : ns) {
    long long cs = 0;
    long long cn = 0;
    long long ts = s;
    long long tn = n;
    while (ts % first == 0) ts /= first, ++cs;
    while (tn % first == 0) tn /= first, ++cn;
    if (cn > cs) {
      a[sz] = first;
      for (long long j = 0; j < cs; ++j) a[sz] *= first;
      ++sz;
    }
  }
  long long g = gcd(s, n);
  long long res = 0;
  for (long long mask = 0; mask < (1 << sz); ++mask) {
    long long val = 1;
    for (long long j = 0; j < sz; ++j)
      if (mask >> j & 1) val *= a[j];
    long long cnt = range / val;
    if (__builtin_popcount(mask) & 1) {
      res -= cnt;
    } else {
      res += cnt;
    }
  }
  return res;
}
int32_t main() {
  long long tc;
  cin >> tc;
  while (tc--) {
    long long n1, n2, n3;
    long long m1, m2, m3;
    long long s1, s2, s3;
    cin >> n1 >> n2 >> n3;
    cin >> m1 >> m2 >> m3;
    cin >> s1 >> s2 >> s3;
    s1 *= 2;
    cout << solve(get_pr(n1, n2, n3), get_num(m1, m2, m3), get_num(s1, s2, s3),
                  get_num(n1, n2, n3)) +
                get_div(s1, s2, s3, get_num(n1, n2, n3) - 1)
         << '\n';
  }
}