#include <bits/stdc++.h>
using namespace std;
void Solve() {
  int n, k;
  cin >> n >> k;
  vector<long long> a(n + 1);
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  long long ans = -1e9;
  for (long long i = n; i > 0; i--) {
    for (long long j = i - 1; j > 0; j--) {
      long long cur = i * j;
      if (cur <= ans) break;
      cur -= k * (a[i] | a[j]);
      ans = max(ans, cur);
    }
  }
  cout << ans << '\n';
}
int main() {
  cin.tie(0)->sync_with_stdio(0);
  int t;
  cin >> t;
  while (t--) {
    Solve();
  }
  return 0;
}
