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;
const int MX = 1010;
vector<int> prime;
void sieve() {
  vector<bool> mark(MX, 0);
  int limit = sqrt(MX + 1);
  prime.push_back(2);
  for (int i = 3; i < MX; i += 2) {
    if (!mark[i]) {
      prime.push_back(i);
      if (i <= limit)
        for (int j = i * i; j < MX; j += 2 * i) mark[j] = 1;
    }
  }
}
long long power(long long base, int pow) {
  long long ans = 1LL;
  while (pow > 0) {
    if (pow & 1) ans *= base;
    pow >>= 1;
    base *= base;
  }
  return ans;
}
void input(long long &N, vector<pair<int, int> > &v) {
  N = 1LL;
  long long a;
  int cnt;
  map<int, int> mp;
  for (int i = 0; i < 3; i++) {
    cin >> a;
    N *= a;
    for (int i = 0; i < prime.size() && prime[i] * prime[i] <= a; i++) {
      if (a % prime[i] == 0) {
        cnt = 0;
        while (a % prime[i] == 0) {
          a /= prime[i];
          cnt++;
        }
        mp[prime[i]] += cnt;
      }
    }
    if (a > 1) mp[a]++;
  }
  for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++)
    v.push_back(pair<int, int>(it->first, it->second));
}
int count_div(const long long &N, const long long made, const int idx,
              const vector<pair<int, int> > &v) {
  if (idx >= v.size()) return 1;
  int ret = 0;
  long long pr = 1LL, pow = v[idx].second;
  for (int i = 0; i <= pow && pr <= N / made; i++) {
    ret += count_div(N, made * pr, idx + 1, v);
    pr *= v[idx].first;
  }
  return ret;
}
long long gcd_div(const vector<pair<int, int> > &Sm,
                  const vector<pair<int, int> > &Nm, const long long &M) {
  vector<long long> pdiv;
  int i = 0, j = 0;
  while (i < Sm.size()) {
    while (j < Nm.size() && Nm[j].first < Sm[i].first)
      pdiv.push_back(Nm[j++].first);
    if (j < Nm.size() && Nm[j].first == Sm[i].first) {
      if (Nm[j].second > Sm[i].second)
        pdiv.push_back(power(Nm[j].first, Sm[i].second + 1));
      j++;
    }
    i++;
  }
  while (j < Nm.size()) pdiv.push_back(Nm[j++].first);
  long long ans = M, vag;
  int limit = (1 << pdiv.size());
  for (int mask = 1, cnt; mask < limit; mask++) {
    cnt = 0;
    vag = 1LL;
    for (int pos = 0; pos < pdiv.size(); pos++) {
      if (mask & (1 << pos)) {
        cnt++;
        vag *= pdiv[pos];
      }
    }
    if (cnt & 1)
      ans -= M / vag;
    else
      ans += M / vag;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  sieve();
  int T;
  cin >> T;
  while (T--) {
    long long N, M = 1LL, S, a;
    vector<pair<int, int> > Nm, Sm;
    input(N, Nm);
    for (int i = 0; i < 3; i++) {
      cin >> a;
      M *= a;
    }
    input(S, Sm);
    S <<= 1;
    if (Sm[0].first == 2)
      Sm[0].second++;
    else
      Sm.insert(Sm.begin(), pair<int, int>(2, 1));
    cout << count_div(N, 1LL, 0, Sm) + gcd_div(Sm, Nm, M) << '\n';
  }
  return 0;
}