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>
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;
using namespace std;
template <class TH>
void _dbg(const char* sdbg, TH h) {
  cerr << sdbg << '=' << h << endl;
}
template <class TH, class... TA>
void _dbg(const char* sdbg, TH h, TA... a) {
  while (*sdbg != ',') cerr << *sdbg++;
  cerr << '=' << h << ',';
  _dbg(sdbg + 1, a...);
}
const int N = 1e6;
long long nums[N + 1];
vector<pair<long long, long long> > rozklad[N + 1];
long long int_pow(long long base, long long exp) {
  long long res = 1;
  for (long long i = (long long)0; i < (long long)exp; i++) {
    res *= base;
  }
  return res;
}
vector<unsigned long long> znajdz_dzielniki(
    vector<pair<unsigned long long, int> >& V) {
  vector<unsigned long long> odp{1};
  for (auto& p : V) {
    int sz = (int)((odp).size());
    unsigned long long mnoz = 1;
    for (int i = 0; i < p.second; i++) {
      mnoz *= p.first;
      for (int j = 0; j < sz; j++) odp.push_back(odp[j] * mnoz);
    }
  }
  return odp;
}
void solve() {
  long long n1, n2, n3;
  cin >> n1 >> n2 >> n3;
  long long m1, m2, m3;
  cin >> m1 >> m2 >> m3;
  long long s1, s2, s3;
  cin >> s1 >> s2 >> s3;
  long long n = n1 * n2 * n3, m = m1 * m2 * m3, s = s1 * s2 * s3;
  map<long long, long long> rozklad_n, rozklad_m, rozklad_s;
  for (auto ni : {n1, n2, n3}) {
    ;
    ;
    for (auto& p : rozklad[ni]) {
      rozklad_n[p.first] += p.second;
    }
  }
  for (auto mi : {m1, m2, m3}) {
    for (auto& p : rozklad[mi]) {
      rozklad_m[p.first] += p.second;
    }
  }
  for (auto si : {s1, s2, s3}) {
    for (auto& p : rozklad[si]) {
      rozklad_s[p.first] += p.second;
    }
  }
  rozklad_s[2]++;
  vector<long long> exps;
  for (auto& p : rozklad_n) {
    ;
    ;
    if (rozklad_s[p.first] < p.second) {
      exps.push_back(int_pow(p.first, rozklad_s[p.first] + 1));
    }
  }
  int cnt = (int)((exps).size());
  vector<long long> prods(1 << cnt);
  long long res = 0;
  for (long long bmask = (long long)0; bmask < (long long)1 << cnt; bmask++) {
    if (bmask) {
      prods[bmask] = prods[bmask ^ (1 << __builtin_ctz(bmask))] *
                     exps[__builtin_ctz(bmask)];
    } else {
      prods[bmask] = 1;
    };
    ;
    res += (1 - 2 * (__builtin_popcount(bmask) & 1)) * m / prods[bmask];
  }
  vector<pair<unsigned long long, int> > roz_s;
  for (auto& p : rozklad_s) {
    roz_s.push_back({p.first, p.second});
  };
  ;
  auto div = znajdz_dzielniki(roz_s);
  for (auto& d : div) {
    if (d <= n) {
      res++;
    }
  }
  cout << res << "\n";
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  for (long long i = (long long)1; i < (long long)N + 1; i++) {
    nums[i] = i;
  }
  for (long long i = (long long)2; i < (long long)N + 1; i++) {
    if (nums[i] == i) {
      for (long long k = 1; k * i <= N; k++) {
        long long exp = 0;
        while (nums[k * i] % i == 0) {
          exp++;
          nums[k * i] /= i;
        }
        rozklad[k * i].push_back({i, exp});
      }
    }
  }
  int T;
  cin >> T;
  for (long long _ = (long long)0; _ < (long long)T; _++) {
    solve();
  }
  return 0;
}