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;
vector<long long> primes;
void sieve() {
  int ar[1500] = {0};
  for (int ctr1 = 2; ctr1 < 1500; ++ctr1)
    if (!ar[ctr1]) {
      primes.push_back(ctr1);
      for (int ctr3 = 2 * ctr1; ctr3 < 1500; ctr3 += ctr1) ar[ctr3] = 0;
    }
}
enum val { n_idx = 0, m_idx = 1, s_idx = 2 };
void add_divisors(long long x, map<int, int> &divs) {
  for (int ctr1 = 0; primes[ctr1] * primes[ctr1] <= x; ++ctr1) {
    if (x % primes[ctr1] == 0) {
      int r = 0;
      while (x % primes[ctr1] == 0) ++r, x /= primes[ctr1];
      divs[primes[ctr1]] += r;
    }
  }
  if (x > 1) divs[x] += 1;
}
long long dfs(long long cur, long long lim, map<int, int>::iterator it,
              map<int, int>::iterator last) {
  if (it == last) {
    return cur <= lim;
  }
  long long p = 1, rez = 0;
  for (int ctr1 = 0; ctr1 <= it->second; ++ctr1) {
    rez += dfs(cur * p, lim, ++it, last);
    --it;
    p *= it->first;
  }
  return rez;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  sieve();
  int t;
  cin >> t;
  while (t--) {
    long long v[3][3], ml[3];
    for (int ctr0 = 0; ctr0 < 3; ++ctr0) {
      ml[ctr0] = 1;
      for (int ctr1 = 0; ctr1 < 3; ++ctr1)
        cin >> v[ctr0][ctr1], ml[ctr0] *= v[ctr0][ctr1];
    }
    map<int, int> v_divs[3];
    add_divisors(2, v_divs[s_idx]);
    for (int ctr0 = 0; ctr0 < 3; ++ctr0)
      for (int ctr1 = 0; ctr1 < 3; ++ctr1)
        add_divisors(v[ctr0][ctr1], v_divs[ctr0]);
    long long rez =
        dfs(1, ml[n_idx], v_divs[s_idx].begin(), v_divs[s_idx].end());
    vector<long long> bad_primes;
    auto it_n = v_divs[n_idx].begin();
    for (auto it_s = v_divs[s_idx].begin();
         it_n != v_divs[n_idx].end() && it_s != v_divs[s_idx].end();) {
      if (it_n->first > it_s->first) {
        ++it_s;
      } else if (it_n->first == it_s->first) {
        if (it_n->second > it_s->second) {
          long long cur_p = 1;
          for (int ctr1 = 0; ctr1 <= it_s->second; ++ctr1) cur_p *= it_s->first;
          bad_primes.push_back(cur_p);
        }
        ++it_s, ++it_n;
      } else {
        bad_primes.push_back(it_n->first);
        ++it_n;
      }
    }
    while (it_n != v_divs[n_idx].end()) {
      bad_primes.push_back(it_n->first);
      ++it_n;
    }
    for (int ctr1 = 0; ctr1 < (1 << bad_primes.size()); ++ctr1) {
      long long cur = 1;
      int sign = 0;
      for (uint32_t ctr2 = 0; ctr2 < bad_primes.size(); ++ctr2)
        if (ctr1 & (1 << ctr2)) cur *= bad_primes[ctr2], ++sign;
      rez += (sign % 2 ? -1 : 1) * (ml[m_idx] / cur);
    }
    cout << rez << '\n';
  }
  return 0;
}