- #include <bits/stdc++.h>
- using namespace std;
-
- typedef long long ll;
- double pi = acos(-1);
- #define _time_ 1.0 * clock() / CLOCKS_PER_SEC
- #define fi first
- #define se second
- #define mp make_pair
- #define pb push_back
- #define all(a) a.begin(),a.end()
- mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
-
- const int maxn=1e5+5;
-
- void solve(){
- int n;
- cin >> n;
- vector<ll> a(n),b(n);
- for(int i=0;i<n;i++) cin >> a[i];
- for(int i=0;i<n;i++) cin >> b[i];
- vector<ll> sum(20),cnt(20);
- vector<vector<ll>> st(20,vector<ll>(20,0));
- ll tot=0;
- for(int i=0;i<n;i++){
- tot += a[i];
- int k = -1;
- for(int j=0;j<20;j++){
- if(a[i]&(1<<j)){
- k = j;
- sum[j]++;
- }
- }
- if(k!=-1){
- cnt[k]++;
- for(int j=0;j<20;j++){
- if(a[i]&(1<<j)){
- st[k][j]++;
- }
- }
- }
- }
- ll ans=0;
- for(int i=0;i<n;i++){
- int k = -1;
- for(int j=0;j<20;j++){
- if(b[i]&(1<<j)){
- k = j;
- }
- }
- if(k==-1){
- ans += tot;
- continue;
- }
- for(int j=0;j<20;j++){
- if(b[i]&(1<<j)){
- ans += (1<<j)*st[k][j];
- ans += (1<<j)*(n-cnt[k]-(sum[j]-st[k][j]));
- }else{
- ans += (1<<j)*(sum[j]-st[k][j]);
- }
- }
- }
- cout << ans;
- }
-
- int main(){
- ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- #ifdef SIEVE
- sieve();
- #endif
- #ifdef NCR
- init();
- #endif
- int t;
- cin >> t;
- while(t--){
- solve();
- cout << "\n";
- }
- return 0;
- }