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 inf = 1 << 30;
const int md = 1e9 + 7;
int n, m;
int T;
const int maxn = 1e6 + 10;
bool b[maxn];
int p[maxn];
long long mpow(long long x, int k) {
  long long re = 1;
  while (k) {
    if (k & 1) re = re * x;
    x = x * x;
    k /= 2;
  }
  return re;
}
struct point {
  long long val;
  vector<int> v;
  vector<int> c;
  point() { val = 1; }
  void work(long long x) {
    val = x;
    v.clear();
    c.clear();
    int now = 0;
    while (x != 1) {
      if (x % p[now] == 0) {
        v.push_back(p[now]);
        c.push_back(0);
        while (x % p[now] == 0) {
          x /= p[now];
          c.back()++;
        }
      }
      now++;
    }
  }
};
point operator+(point a, point b) {
  point re;
  re.work(a.val * b.val);
  return re;
}
long long save[maxn];
int mp[maxn];
int cal[maxn];
int pn;
void pre() {
  for (int i = 0; (1 << i) < maxn; i++) mp[1 << i] = i;
  for (int i = 1; i < maxn; i++) cal[i] = cal[i ^ (i & -i)] + 1;
  for (int i = 2; i < maxn; i++) {
    if (b[i]) continue;
    p[pn++] = i;
    for (int j = i << 1; j < maxn; j += i) b[j] = 1;
  }
}
long long work(point n, point m, point s) {
  long long vs = s.val;
  long long vm = m.val;
  long long re = vm;
  int l = 0, r = 0;
  vector<long long> ve;
  for (; l < n.v.size(); l++) {
    while (r < s.v.size() && s.v[r] < n.v[l]) r++;
    if (r >= s.v.size() || s.v[r] != n.v[l]) {
      ve.push_back(n.v[l]);
    } else if (s.c[r] < n.c[l]) {
      ve.push_back(mpow(s.v[r], s.c[r] + 1));
    }
  }
  save[0] = 1;
  for (int st = 1; st < (1 << ve.size()); st++) {
    save[st] = save[st ^ (st & -st)] * ve[mp[st & -st]];
    if (cal[st] & 1)
      re -= vm / save[st];
    else
      re += vm / save[st];
  }
  return re;
}
long long dfs(int dep, long long now, long long& n, point& s) {
  if (dep == s.v.size()) return 1;
  long long re = 0;
  for (int i = 0; i <= s.c[dep]; i++) {
    if (now < 0 || now > n) return re;
    re += dfs(dep + 1, now, n, s);
    now *= s.v[dep];
  }
  return re;
}
long long solve(point& n, point& m, point& s) {
  long long re = 0;
  long long vn = n.val;
  re = dfs(0, 1, vn, s);
  re += work(n, m, s);
  return re;
}
int main() {
  pre();
  scanf("%d", &T);
  while (T--) {
    point n, m, s;
    point t;
    int x;
    for (int i = 0; i < 3; i++) {
      scanf("%d", &x);
      t.work(x);
      n = n + t;
    }
    for (int i = 0; i < 3; i++) {
      scanf("%d", &x);
      t.work(x);
      m = m + t;
    }
    for (int i = 0; i < 3; i++) {
      scanf("%d", &x);
      t.work(x);
      s = s + t;
    }
    t.work(2);
    s = s + t;
    long long ans = solve(n, m, s);
    printf("%I64d\n", ans);
  }
  return 0;
}