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