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>
inline bool chkmax(T &x, const T &y) {
  return x < y ? x = y, 1 : 0;
}
template <typename T>
inline bool chkmin(T &x, const T &y) {
  return x > y ? x = y, 1 : 0;
}
struct IO {
  static const int maxn = (1 << 25) + 10;
  char a[maxn], *s, b[maxn], *t;
  void INPUT() {
    s = a;
    t = b;
    a[fread(a, 1, sizeof a, stdin)] = 0;
  }
  void OUTPUT() { fwrite(b, 1, t - b, stdout); }
  operator int() {
    int x = 0;
    while (*s != '-' && (*s < '0' || *s > '9')) {
      ++s;
    }
    bool f = 0;
    if (*s == '-') {
      f = 1;
      ++s;
    }
    while (*s >= '0' && *s <= '9') {
      (x *= 10) += *s - '0';
      ++s;
    }
    if (f) {
      x = -x;
    }
    return x;
  }
  operator long long() {
    long long x = 0;
    while (*s != '-' && (*s < '0' || *s > '9')) {
      ++s;
    }
    bool f = 0;
    if (*s == '-') {
      f = 1;
      ++s;
    }
    while (*s >= '0' && *s <= '9') {
      (x *= 10) += *s - '0';
      ++s;
    }
    if (f) {
      x = -x;
    }
    return x;
  }
  operator char() {
    while (*s <= 32) {
      ++s;
    }
    char ret = *s;
    ++s;
    return ret;
  }
  inline void out(int x) {
    if (!x) {
      *t++ = '0';
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      int y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    return;
  }
  inline void out(int x, char C) {
    if (!x) {
      *t++ = '0';
      *t++ = C;
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      int y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    *t++ = C;
    return;
  }
  inline void out(long long x) {
    if (!x) {
      *t++ = '0';
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      long long y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    return;
  }
  inline void out(long long x, char C) {
    if (!x) {
      *t++ = '0';
      *t++ = C;
      return;
    }
    if (x < 0) {
      *t++ = '-';
      x = -x;
    }
    static char c[20], *i;
    i = c;
    while (x) {
      long long y = x / 10;
      *i++ = x - y * 10 + '0';
      x = y;
    }
    while (i != c) {
      *t++ = *--i;
    }
    *t++ = C;
    return;
  }
  inline void out(char c) {
    *t++ = c;
    return;
  }
  inline void out(char *s) {
    while (*s >= ' ') {
      *t++ = *s++;
    }
    return;
  }
} io;
void Main();
int main(int argc, char *argv[]) {
  io.INPUT();
  Main();
  io.OUTPUT();
  return 0;
}
map<int, int> n, m, s;
long long N, M, S, ans;
vector<pair<int, int> > all;
void input(map<int, int> &a, long long &A) {
  a.clear();
  A = 1;
  for (int i = 0, i_end_ = (3); i < i_end_; ++i) {
    int x = io;
    A *= (long long)x;
    for (int y = 2; y * y <= x; ++y) {
      while (x % y == 0) {
        ++a[y];
        x /= y;
      }
    }
    if (x > 1) {
      ++a[x];
    }
  }
  return;
}
void dfs(long long x = 1, int it = 0) {
  if (x > N) {
    return;
  }
  if (it == (int)((all).size())) {
    ans += (x <= N);
    return;
  }
  dfs(x, it + 1);
  for (int i = 0, i_end_ = (all[it].second); i < i_end_; ++i) {
    dfs((x *= (long long)all[it].first), it + 1);
  }
  return;
}
void solve() {
  input(n, N);
  input(m, M);
  input(s, S);
  ++s[2];
  S *= 2;
  ans = 0;
  vector<long long> v;
  for (auto it : n) {
    int x = s.count(it.first) ? s[it.first] + 1 : 1, y = it.first;
    if (x <= it.second) {
      long long z = 1;
      while (x--) {
        z *= (long long)y;
      }
      v.push_back(z);
    }
  }
  for (int i = 0, i_end_ = (1 << (int)((v).size())); i < i_end_; ++i) {
    long long z = 1;
    for (int j = 0, j_end_ = ((int)((v).size())); j < j_end_; ++j) {
      if ((i >> j) & 1) {
        z *= (long long)v[j];
      }
    }
    ans += (__builtin_parity(i) ? -1 : 1) * (M / z);
  }
  all.clear();
  for (auto it : s) {
    all.push_back(make_pair(it.first, it.second));
  }
  dfs();
  io.out(ans, '\n');
  return;
}
void Main() {
  int T = io;
  while (T--) {
    solve();
  }
  return;
}