#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int B = 31;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int irandom_range(int l, int r) { return (l + rng() % (r-l+1)); }
struct Query {
int op;
int p, y;
void input() {
cin >> op;
if (op == 1) cin >> p >> y;
}
};
void brute_update(vector<int>& a, int p, int q) {
int n = (int)a.size();
for (int i = 0; i < n; ++i) {
if (__builtin_popcount(a[i]) == p) {
a[i] |= q;
break;
}
}
}
ll brute_query(vector<int>& a) {
int n = (int)a.size();
ll ret = 0;
for (int l = 0; l < n; ++l) for (int r = l; r < n; ++r) {
int mx = 0;
for (int i = l; i <= r; ++i) {
mx = max(mx, __builtin_popcount(a[i]));
}
ret += (ll)(r-l+1) * mx;
}
return ret;
}
ll get_sum(ll l, ll r) {
ll ret = r * (r + 1) / 2;
if (l > 0) ret -= l * (l - 1) / 2;
return ret;
}
ll get_val(ll i, ll l, ll r) {
ll ld = i - l + 1;
ll rd = r - i + 1;
ll ret = (ld + rd) * ld * rd / 2;
return ret;
}
void solve_case(){
int n, q;
n = 100;
q = 100;
// cin >> n >> q;
vector<int> a(n);
for (int i = 0; i < n; ++i) a[i] = irandom_range(1, 100);
// for (int i = 0; i < n; ++i) cin >> a[i];
vector<Query> queries(q);
for (int i = 0; i < q; ++i) {
queries[i].op = 1;
queries[i].p = irandom_range(1, 4);
queries[i].y = irandom_range(1, 100);
// queries[i].input();
}
vector<int> P(n);
for (int i = 0; i < n; ++i) {
P[i] = __builtin_popcount(a[i]);
}
vector<int> L(n);
{
stack<int> st;
for (int i = 0; i < n; ++i) {
while (!st.empty() && (P[st.top()] < P[i])) st.pop();
if (st.empty()) L[i] = 0;
else L[i] = st.top() + 1;
st.push(i);
}
}
vector<int> R(n);
{
stack<int> st;
for (int i = n-1; i >= 0; --i) {
while (!st.empty() && (P[st.top()] <= P[i])) st.pop();
if (st.empty()) R[i] = n-1;
else R[i] = st.top() - 1;
st.push(i);
}
}
ll ans = 0;
for (int i = 0; i < n; ++i) {
ans += get_val(i, L[i], R[i]) * P[i];
}
array<set<pair<int,int>>,B> pos;
for (int i = 0; i < n; ++i) pos[P[i]].emplace(i, a[i]);
auto debug_print = [&]() -> void {
cout << "\tP: ";
for (int i = 0; i < n; ++i) cout << P[i] << " ";
cout << "\n";
cout << "\tL: ";
for (int i = 0; i < n; ++i) cout << L[i] << " ";
cout << "\n";
cout << "\tR: ";
for (int i = 0; i < n; ++i) cout << R[i] << " ";
cout << "\n";
};
auto update_L = [&](int i, int new_i) -> void {
ans -= get_val(i, L[i], R[i]) * P[i];
L[i] = new_i;
ans += get_val(i, L[i], R[i]) * P[i];
};
auto update_R = [&](int i, int new_i) -> void {
ans -= get_val(i, L[i], R[i]) * P[i];
R[i] = new_i;
ans += get_val(i, L[i], R[i]) * P[i];
};
vector<int> b = a;
for (int qi = 0; qi < q; ++qi) {
// queries[qi].input();
int op = queries[qi].op;
int p = queries[qi].p;
int y = queries[qi].y;
if (op == 1) {
if (pos[p].empty()) continue;
auto it = pos[p].begin();
int i = it->first;
int x = it->second;
pos[p].erase(pos[p].begin());
int xto = x | y;
if (xto == x) continue;
int xto_p = __builtin_popcount(xto);
ans -= get_val(i, L[i], R[i]) * P[i];
// update R
R[i] = n;
for (int b = xto_p + 1; b < B; ++b) {
if (pos[b].empty()) continue;
auto it = pos[b].lower_bound({i, -1});
if (it != pos[b].end()) {
R[i] = min(R[i], it->first - 1);
}
}
if (!pos[xto_p].empty()) {
auto it = pos[xto_p].lower_bound({i, -1});
if (it != pos[xto_p].end()) {
if (it->first < R[i]) {
R[i] = R[it->first];
update_L(it->first, i+1);
}
}
}
R[i] = min(R[i], n-1);
// update L
L[i] = -1;
for (int b = xto_p + 1; b < B; ++b) {
if (pos[b].empty()) continue;
auto it = pos[b].lower_bound({i, -1});
if (it != pos[b].begin()) {
--it;
L[i] = max(L[i], it->first + 1);
}
}
if (!pos[xto_p].empty()) {
auto it = pos[xto_p].lower_bound({i, -1});
while (it != pos[xto_p].begin()) {
--it;
if (it->first < L[i]) break;
if (it->first > L[i]) {
L[i] = it->first+1;
}
update_R(it->first, R[i]);
}
}
L[i] = max(L[i], 0);
int curr = P[i];
for (int j = i+1; j < n; ++j) {
if (P[j] >= xto_p) break;
if (P[j] > curr) {
update_L(j, i+1);
curr = P[j];
}
}
for (int j = i-1; j >= 0; --j) {
if (P[j] > xto_p) break;
if (P[j] >= x) {
update_R(j, i-1);
}
}
a[i] = xto;
P[i] = xto_p;
pos[xto_p].emplace(i, a[i]);
ans += get_val(i, L[i], R[i]) * P[i];
brute_update(b, p, y);
int bans = brute_query(b);
if (ans != bans) {
cout << ans << " : " << bans << "\n";
debug_print();
break;
}
} else {
cout << "\t" << ans << " " << brute_query(b) << endl;
}
}
}
int main(){
// ios_base::sync_with_stdio(false); cin.tie(NULL);
int tc = 1, ti;
cin >> tc;
for (ti = 1; ti <= tc; ++ti) {
solve_case();
}
}
/*
1
10 123123
1 2 3 4 5 6 7 8 9 10
1 1 10000
1 1 100000000
1 2 10000
1
10 123123
1 2 3 4 5 6 7 8 9 10
1 1 10000
1 1 100000000
1 2 10000
1 5 213123
1 5 23123
1 4 123123
1 6 123123
1 10 123123
1 3 123123
8
3 5
2 1 3
2
1 2 4
2
1 1 1
2
9 6
8 3 4 6 1 7 3 8 2
2
1 2 5
2
1 2 6
1 3 8
2
9 6
2 8 3 7 1 6 4 3 8
2
1 2 5
2
1 2 6
1 3 8
2
7 1
1 1 1 1 1 1 1
2
7 1
1 1 1 3 1 1 1
2
7 1
7 7 7 3 7 7 7
2
7 1
7 7 7 7 7 7 7
2
7 1
7 7 7 15 7 7 7
2
*/