1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef long long ll;
  5. double pi = acos(-1);
  6. #define _time_ 1.0 * clock() / CLOCKS_PER_SEC
  7. #define fi first
  8. #define se second
  9. #define mp make_pair
  10. #define pb push_back
  11. #define all(a) a.begin(),a.end()
  12. mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
  13.  
  14. const int maxn=1e5+5;
  15.  
  16. void solve(){
  17. int n;
  18. cin >> n;
  19. vector<ll> a(n),b(n);
  20. for(int i=0;i<n;i++) cin >> a[i];
  21. for(int i=0;i<n;i++) cin >> b[i];
  22. vector<ll> sum(20),cnt(20);
  23. vector<vector<ll>> st(20,vector<ll>(20,0));
  24. ll tot=0;
  25. for(int i=0;i<n;i++){
  26. tot += a[i];
  27. int k = -1;
  28. for(int j=0;j<20;j++){
  29. if(a[i]&(1<<j)){
  30. k = j;
  31. sum[j]++;
  32. }
  33. }
  34. if(k!=-1){
  35. cnt[k]++;
  36. for(int j=0;j<20;j++){
  37. if(a[i]&(1<<j)){
  38. st[k][j]++;
  39. }
  40. }
  41. }
  42. }
  43. ll ans=0;
  44. for(int i=0;i<n;i++){
  45. int k = -1;
  46. for(int j=0;j<20;j++){
  47. if(b[i]&(1<<j)){
  48. k = j;
  49. }
  50. }
  51. if(k==-1){
  52. ans += tot;
  53. continue;
  54. }
  55. for(int j=0;j<20;j++){
  56. if(b[i]&(1<<j)){
  57. ans += (1<<j)*st[k][j];
  58. ans += (1<<j)*(n-cnt[k]-(sum[j]-st[k][j]));
  59. }else{
  60. ans += (1<<j)*(sum[j]-st[k][j]);
  61. }
  62. }
  63. }
  64. cout << ans;
  65. }
  66.  
  67. int main(){
  68. ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  69. //freopen("input.txt", "r", stdin);
  70. //freopen("output.txt", "w", stdout);
  71. #ifdef SIEVE
  72. sieve();
  73. #endif
  74. #ifdef NCR
  75. init();
  76. #endif
  77. int t;
  78. cin >> t;
  79. while(t--){
  80. solve();
  81. cout << "\n";
  82. }
  83. return 0;
  84. }