#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<long long> a(n);
  for (long long i = 0; i < n; ++i) cin >> a[i];
  long long mx = -1e18;
  for (long long i = max(0ll, n - 2 * k); i < n; ++i)
    for (long long j = i + 1; j < n; ++j) {
      long long curr = (i + 1) * (j + 1) - k * (a[i] | a[j]);
      mx = max(curr, mx);
    }
  cout << mx << "\n";
}
int32_t main() {
  long long t = 1;
  cin >> t;
  while (t--) solve();
}
