#include<iostream>
#include<cmath>
using namespace std;
using ll = long long;
const int N = 1e5 + 9;
ll a[N],w[N];
void slove()
{
int n; cin >> n;
for (int i = 1; i <= n; ++i)cin >> a[i];
for (int i = 1; i <= n; ++i)cin >> w[i];
ll ess = 0;
for (int i = 1; i <= n; ++i)ess += a[i]*w[i];
ll mx = 0, fix = 0;//fix表示最大子段和
for (int i = 1; i <= n; ++i)
{
mx = max(0ll, mx + (a[i] ? -1 : 1)*w[i]);
fix = max(mx, fix);
}
cout << ess + fix << '\n';
}
int main()
{
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t; cin >> t;
while (t--)
{
slove();
}
return 0;
}