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, typename U>
inline void smin(T &a, U b) {
  if (a > b) a = b;
}
template <typename T, typename U>
inline void smax(T &a, U b) {
  if (a < b) a = b;
}
inline void getint(int &first) {
  char c;
  while (c = getchar(), c > '9' || c < '0')
    ;
  for (first = 0; c >= '0' && c <= '9'; c = getchar())
    first = (first << 1) + (first << 3) + c - '0';
}
inline void print(int first) {
  if (first < 10) {
    putchar('0' + first);
    return;
  }
  print(first / 10);
  putchar(first % 10 + '0');
}
int power(int a, int b, int m, int ans = 1) {
  for (; b; b >>= 1, a = 1LL * a * a % m)
    if (b & 1) ans = 1LL * ans * a % m;
  return ans;
}
vector<pair<long long, long long> > fac[5];
long long q[11111111];
int cmp(pair<long long, long long> &a, pair<long long, long long> b) {
  if (a.first != b.first) return 0;
  a.second += b.second;
  return 1;
}
vector<pair<long long, long long> > calc1(
    vector<pair<long long, long long> > &a,
    vector<pair<long long, long long> > &b) {
  int i = 0, n = a.size(), j = 0, m = b.size();
  vector<pair<long long, long long> > tmp;
  while (i < n && j < m) {
    if (a[i].first == b[j].first) {
      if (a[i].second < b[j].second)
        tmp.push_back(pair<long long, long long>(b[j].first, a[i].second + 1));
      i++, j++;
    } else if (a[i].first > b[j].first)
      tmp.push_back(pair<long long, long long>(b[j].first, 1)), j++;
    else
      i++;
  }
  while (j < m) tmp.push_back(pair<long long, long long>(b[j].first, 1)), j++;
  return tmp;
}
vector<pair<long long, long long> > c;
long long calc(long long second, long long first) {
  long long ans = 0;
  for (int i = 0; i < c.size(); i++) {
    if (first % c[i].first == 0) {
      if (c[i].second & 1LL)
        ans -= second / c[i].first;
      else
        ans += second / c[i].first;
    }
  }
  return ans;
}
int main() {
  int T, qb;
  for (scanf("%d", &T); T--;) {
    long long first = 1, second = 1, z = 2;
    for (int i = 0; i < 3; i++) {
      fac[i].clear();
      if (i == 2) fac[i].push_back(pair<long long, long long>(2, 1));
      for (int j = 0; j < 3; j++) {
        int u;
        scanf("%d", &u);
        if (i == 0) first *= u;
        if (i == 1) second *= u;
        if (i == 2) z *= u;
        for (int v = 2; v * v <= u; v++)
          if (u % v == 0) {
            int cnt = 0;
            while (u % v == 0) u /= v, cnt++;
            fac[i].push_back(pair<long long, long long>(v, cnt));
          }
        if (u > 1) fac[i].push_back(pair<long long, long long>(u, 1));
      }
      if (!fac[i].size()) continue;
      sort(fac[i].begin(), fac[i].end());
      fac[i].erase(unique(fac[i].begin(), fac[i].end(), cmp), fac[i].end());
    }
    qb = 0;
    q[qb++] = 1;
    fac[3] = calc1(fac[2], fac[0]);
    long long mx = (long long)(sqrtl(z) + 1);
    for (int i = 0; i < fac[2].size(); i++) {
      long long u = fac[2][i].first, v = fac[2][i].second;
      for (int z = qb, j = 0; j < z; j++) {
        long long mid = 1;
        for (int k = 0; k < v; k++) {
          mid *= u;
          if (mid > mx) break;
          if (q[j] * mid > mx) break;
          q[qb++] = q[j] * mid;
        }
      }
    }
    long long ans = 0;
    for (int i = 0; i < qb; i++) {
      if (z / q[i] < q[i]) continue;
      if (q[i] <= first) ans++;
      if (q[i] * q[i] == z) continue;
      if (z / q[i] <= first) ans++;
    }
    qb = 0, q[qb++] = 1;
    q[qb++] = 0;
    for (int i = 0; i < fac[3].size(); i++) {
      long long u = fac[3][i].first, v = fac[3][i].second;
      for (int z = qb, j = 0; j < z; j += 2) {
        long long mid = 1;
        for (int k = 0; k < v; k++) mid *= u;
        if (q[j] > second / mid) continue;
        q[qb++] = q[j] * mid;
        q[qb++] = q[j + 1] + 1;
      }
    }
    for (int i = 0; i < qb; i += 2) {
      if (q[i + 1] & 1LL)
        ans -= second / q[i];
      else
        ans += second / q[i];
    }
    printf("%I64d\n", ans);
  }
  return 0;
}