#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    int n, k;
    cin >> n >> k;
    long long arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    long long ans = INT_MIN;
    long long start = max((long long)0, (long long)(n - 200));
    for (long long i = start; i < n; i++) {
      long long currval = INT_MIN;
      for (long long j = i + 1; j < n; j++) {
        currval = max(currval, ((i + 1) * (j + 1)) - (k * (arr[i] | arr[j])));
      }
      ans = max(ans, currval);
    }
    cout << ans << endl;
  }
}
