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>
#pragma GCC optimize("Ofast")
#pragma GCC target( \
    "sse,sse2,sse3,ssse3,sse4,sse4.1,sse4.2,popcnt,abm,mmx,avx,tune=native")
using namespace std;
const int MOD = 1000000007;
const int UNDEF = -1;
const long long INF = 1e18;
template <typename T>
inline bool chkmax(T &aa, T bb) {
  return aa < bb ? aa = bb, true : false;
}
template <typename T>
inline bool chkmin(T &aa, T bb) {
  return aa > bb ? aa = bb, true : false;
}
int rint();
char rch();
long long rlong();
const int MAXP = 1e6 + 1;
bool sieve_array[MAXP + 1];
int ap[MAXP + 1];
vector<int> gpv;
void sieve() {
  for (int i = 0; i <= MAXP; i++) {
    sieve_array[i] = true;
    ap[i] = 1 << 30;
  }
  sieve_array[0] = false;
  sieve_array[1] = false;
  int lim = (int)(ceil(sqrt(MAXP))) + 2;
  for (int i = 2; i <= lim; i++) {
    if (sieve_array[i]) {
      for (int j = i * i; j <= MAXP; j += i) {
        sieve_array[j] = false;
        ap[j] = i;
      }
    }
  }
  for (int i = 2; i <= MAXP; i++) {
    if (sieve_array[i]) {
      gpv.push_back(i);
      ap[i] = i;
    }
  }
}
int factorize_tmp[128];
void factorize(int *inp, vector<pair<int, int> > &factorize_dest) {
  const int power = 1;
  int tsz = 0;
  for (int i = 0; i < 3; i++) {
    int x = inp[i];
    while (x >= MAXP) {
      bool found = false;
      for (auto &p : gpv) {
        if (p * (long long)p > x) break;
        while ((x % p) == 0) {
          x /= p;
          factorize_tmp[tsz++] = p;
          found = true;
        }
      }
      if (x < MAXP) break;
      if (!found) break;
    }
    if (x < MAXP) {
      while (x > 1) {
        int p = ap[x];
        x /= p;
        factorize_tmp[tsz++] = p;
      }
    }
    if (x > 1) factorize_tmp[tsz++] = x;
  }
  sort(factorize_tmp, factorize_tmp + tsz);
  int didx = 0;
  factorize_dest.resize(128);
  for (int tidx = 0; tidx < tsz; tidx++) {
    long long p = factorize_tmp[tidx];
    if (tidx == 0 || p != factorize_tmp[tidx - 1]) {
      factorize_dest[didx++] = make_pair(p, power);
    } else
      factorize_dest[didx - 1].second += power;
  }
  factorize_dest.resize(didx);
}
vector<pair<int, int> > gcd(const vector<pair<int, int> > &af,
                            const vector<pair<int, int> > &bf) {
  vector<pair<int, int> > ans;
  int bsz = bf.size();
  int bi = 0;
  for (auto &aw : af) {
    int p = aw.first;
    while (bi < bf.size() && bf[bi].first < p) {
      bi++;
    }
    int e = 0;
    if (bi < bf.size() && p == bf[bi].first) {
      e = min(aw.second, bf[bi].second);
    }
    ans.push_back(make_pair(p, e));
  }
  assert(ans.size() == af.size());
  return ans;
}
long long divisors_dest[500000];
int countdivisors(const vector<pair<int, int> > &factorize_dest,
                  const long long lim) {
  int numprimes = factorize_dest.size();
  int sz = 1;
  divisors_dest[0] = 1;
  for (int pi = 0; pi < numprimes; pi++) {
    long long p = factorize_dest[pi].first;
    int elim = factorize_dest[pi].second;
    long long pp = 1;
    int prevsz = sz;
    for (int e = 1; e <= elim; e++) {
      pp *= p;
      for (int i = 0; i < prevsz; i++) {
        long long res = divisors_dest[i] * pp;
        if (res <= lim) {
          divisors_dest[sz++] = divisors_dest[i] * pp;
        }
      }
    }
  }
  return sz;
}
vector<pair<int, int> > af, bf, sf;
int va[3], vb[3], vs[3];
long long solve() {
  af.clear();
  bf.clear();
  sf.clear();
  vs[0] *= 2;
  long long a = va[0] * (long long)va[1] * (long long)va[2];
  long long b = vb[0] * (long long)vb[1] * (long long)vb[2];
  factorize(va, af);
  factorize(vs, sf);
  vector<pair<int, int> > gcdsa = gcd(af, sf);
  vector<long long> res;
  for (int i = 0; i < af.size(); i++) {
    int p = af[i].first;
    int ae = af[i].second, ge = gcdsa[i].second;
    if (ae > ge) {
      long long pp = 1;
      for (int j = 0; j < ge + 1; j++) pp *= p;
      res.push_back(pp);
    }
  }
  int k = res.size();
  int zlim = 1 << k;
  long long ans1 = 0;
  for (int z = 0; z < zlim; z++) {
    long long base = 1;
    for (int i = 0; i < k; i++) {
      if (z & (1 << i)) {
        base *= -res[i];
      }
    }
    ans1 += b / base;
  }
  int ans2 = countdivisors(sf, a);
  long long final = ans1 + ans2;
  return final;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  sieve();
  int t = rint();
  for (int ti = 0; ti < t; ti++) {
    for (int i = 0; i < 3; i++) va[i] = rint();
    for (int i = 0; i < 3; i++) vb[i] = rint();
    for (int i = 0; i < 3; i++) vs[i] = rint();
    long long ans = solve();
    printf("%lld\n", ans);
  }
}
static char stdinBuffer[1024];
static char *stdinDataEnd = stdinBuffer + sizeof(stdinBuffer);
static const char *stdinPos = stdinDataEnd;
void readAhead(size_t amount) {
  size_t remaining = stdinDataEnd - stdinPos;
  if (remaining < amount) {
    memmove(stdinBuffer, stdinPos, remaining);
    size_t sz = fread(stdinBuffer + remaining, 1,
                      sizeof(stdinBuffer) - remaining, stdin);
    stdinPos = stdinBuffer;
    stdinDataEnd = stdinBuffer + remaining + sz;
    if (stdinDataEnd != stdinBuffer + sizeof(stdinBuffer)) *stdinDataEnd = 0;
  }
}
int rint() {
  readAhead(16);
  int x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}
char rch() {
  readAhead(16);
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  char ans = *stdinPos;
  ++stdinPos;
  return ans;
}
long long rlong() {
  readAhead(32);
  long long x = 0;
  bool neg = false;
  while (*stdinPos == ' ' || *stdinPos == '\n') ++stdinPos;
  if (*stdinPos == '-') {
    ++stdinPos;
    neg = true;
  }
  while (*stdinPos >= '0' && *stdinPos <= '9') {
    x *= 10;
    x += *stdinPos - '0';
    ++stdinPos;
  }
  return neg ? -x : x;
}