/* Problem URL: https://codeforces.com/contest/2210/problem/C2 */ #include #include #include using namespace std; using namespace __gnu_pbds; template > using ordered_set = tree; #define V vector #define rmin(a, b) a = min(a, b) #define rmax(a, b) a = max(a, b) #define rep(i, lim) for (int i = 0; i < (lim); i++) #define nrep(i, s, lim) for (int i = s; i < (lim); i++) #define repv(i, v) for (auto &i : (v)) #define fillv(v) for (auto &itr_ : (v)) { cin >> itr_; } #define sortv(v) sort(v.begin(), v.end()) #define all(v) (v).begin(), (v).end() using vi = vector; using vvi = vector; using vvvi = vector; using vvvvi = vector; using ll = long long; using vl = vector; using vvl = vector; using vvvl = vector; using vvvvl = vector; template auto operator<<(ostream &os, const vector &vec)->ostream& { os << vec[0]; for (size_t i = 1; i < vec.size(); i++) { os << ' ' << vec[i]; } os << '\n'; return os; } template auto operator>>(istream &is, vector &vec)->istream& { for (auto &i : vec) { is >> i; } return is; } template auto operator<<(ostream &os, const vector> &vec)->ostream& { for (auto &i : vec) { os << i[0]; for (size_t j = 1; j < i.size(); j++) { os << ' ' << i[j]; } os << '\n'; } return os; } template auto operator>>(istream &is, vector> &vec)->istream& { for (auto &i : vec) { for (auto &j : i) { is >> j; } } return is; } const int oo = INT32_MAX >> 1; const ll OO = INT64_MAX >> 1; ll p[] = {1, 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113}; int ps = sizeof(p) / sizeof(ll); void pre() { } #define TEST 1 void solve() { int n; cin >> n; vl a(n); vl b(n); cin >> a >> b; vl act(n); V rem(n); act[0] = gcd(a[0], a[1]); if (act[0] > b[0]) { act[0] = a[0]; } if (act[0] != a[0]) { rem[0] = true; } act[n - 1] = gcd(a[n - 1], a[n - 2]); if (act[n - 1] > b[n - 1]) { act[n - 1] = a[n - 1]; } if (act[n - 1] != a[n - 1]) { rem[n - 1] = true; } nrep(i, 1, n - 1) { act[i] = lcm(gcd(a[i], a[i - 1]), gcd(a[i], a[i + 1])); if (act[i] > b[i]) { act[i] = a[i]; continue; } if (act[i] != a[i]) { rem[i] = true; } } vvl dp(n, vl(ps, -OO)); dp[0][0] = rem[0]; if (!rem[0]) { rep(i, ps) { ll cur = p[i] * act[0]; if (cur > b[0]) { break; } dp[0][i] = (cur != a[0]); } } nrep(i, 1, n) { ll bf = act[i - 1] / gcd(act[i - 1], act[i]); ll nw = act[i] / gcd(act[i - 1], act[i]); if (rem[i]) { dp[i][0] = dp[i - 1][0] + 1; nrep(k, 1, ps) { if (nw % p[k] != 0) { rmax(dp[i][0], dp[i - 1][k] + 1); } } continue; } dp[i][0] = dp[i - 1][0]; nrep(k, 1, ps) { if (nw % p[k] == 0) { continue; } rmax(dp[i][0], dp[i - 1][k]); } nrep(j, 1, ps) { if (act[i] * p[j] > b[i]) { break; } if (bf % p[j] == 0) { continue; } rmax(dp[i][j], dp[i - 1][0] + (act[i] * p[j] != a[i])); nrep(k, 1, ps) { if (j == k || nw % p[k] == 0) { continue; } rmax(dp[i][j], dp[i - 1][k] + (act[i] * p[j] != a[i])); } } } cout << max(*max_element(all(dp[n - 1])), 0LL) << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); pre(); int t; (TEST && cin >> t) || (t = 1); while (t--) { solve(); } }