/*
The darkest places in the hell are reserved for those who maintain thier neutrality in time of moral crisis- Bertrand Zobrist.
Religion and GOD are the only shit between India and Development
*/
#include<stdio.h>
#include<iostream>
#include<bitset>
#include<math.h>
#include<string.h>
using namespace std;
int array[20005], solution[20005] ;
bitset <31> bin[20005] ;
bitset <31> sol[20005] ;
int main(){
int T, N, K, ans ;
scanf("%d", &T) ;
while(T--){
ios_base::sync_with_stdio(false); cin.tie(0) ;
scanf("%d %d", &N, &K) ;
for(int i = 0; i < N; i++){
scanf("%d", &array[i]) ;
for(int j = 0; j < 31; j++)
bin[i][j] = array[i] & 1 << j ;
}
K = min(K, N / 2) ;
int zeroes ;
for(int i = 0; i < 31; i++){
zeroes = 0;
for(int j = 0; j < 2*K+1; j++){
if (bin[j][i] == 0)
zeroes++;
}
for (int k = 0; k < N; k++){
if (!zeroes)
sol[(K+k)%N][i] = 1 ;
else
sol[(K+k)%N][i] = 0 ;
if (bin[k][i] == 0)
zeroes--;
if(bin[(2*K+1+k)%N][i] == 0)
zeroes++;
}
}
for (int i = 0; i < N; i++){
ans = 0 ;
for(int j = 0; j < 31; j++){
ans += sol[i][j] * (pow(2,j)) ;
}
printf("%d ", ans);
}
printf("\n");
}
return 0;
}
LyoKVGhlIGRhcmtlc3QgcGxhY2VzIGluIHRoZSBoZWxsIGFyZSByZXNlcnZlZCBmb3IgdGhvc2Ugd2hvIG1haW50YWluIHRoaWVyIG5ldXRyYWxpdHkgaW4gdGltZSBvZiBtb3JhbCBjcmlzaXMtIEJlcnRyYW5kIFpvYnJpc3QuClJlbGlnaW9uIGFuZCBHT0QgYXJlIHRoZSBvbmx5IHNoaXQgYmV0d2VlbiBJbmRpYSBhbmQgRGV2ZWxvcG1lbnQKKi8KI2luY2x1ZGU8c3RkaW8uaD4KI2luY2x1ZGU8aW9zdHJlYW0+CiNpbmNsdWRlPGJpdHNldD4KI2luY2x1ZGU8bWF0aC5oPgojaW5jbHVkZTxzdHJpbmcuaD4KdXNpbmcgbmFtZXNwYWNlIHN0ZDsKaW50IGFycmF5WzIwMDA1XSwgc29sdXRpb25bMjAwMDVdICAgOwpiaXRzZXQgPDMxPiBiaW5bMjAwMDVdICA7CmJpdHNldCA8MzE+IHNvbFsyMDAwNV0gICA7CmludCBtYWluKCl7CiAgICBpbnQgVCwgTiwgSywgYW5zIDsKICAgIHNjYW5mKCIlZCIsICZUKSAgICA7CiAgICB3aGlsZShULS0pewogICAgICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oZmFsc2UpOyAgIGNpbi50aWUoMCkgIDsKICAgICAgICBzY2FuZigiJWQgJWQiLCAmTiwgJkspICAgOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCBOOyBpKyspewogICAgICAgICAgICBzY2FuZigiJWQiLCAmYXJyYXlbaV0pIDsKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IDMxOyBqKyspCiAgICAgICAgICAgICAgICBiaW5baV1bal0gPSBhcnJheVtpXSAmIDEgPDwgaiAgIDsKICAgICAgICAgICAgfQogICAgICAgIEsgPSBtaW4oSywgTiAvIDIpICAgOwogICAgICAgIGludCB6ZXJvZXMgICAgOwogICAgICAgIGZvcihpbnQgaSA9IDA7IGkgPCAzMTsgaSsrKXsKICAgICAgICAgICAgemVyb2VzID0gMDsKICAgICAgICAgICAgZm9yKGludCBqID0gMDsgaiA8IDIqSysxOyBqKyspewogICAgICAgICAgICAgICAgaWYgKGJpbltqXVtpXSA9PSAwKQogICAgICAgICAgICAgICAgemVyb2VzKys7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgZm9yIChpbnQgayA9IDA7IGsgPCBOOyBrKyspewogICAgICAgICAgICAgICAgaWYgKCF6ZXJvZXMpCiAgICAgICAgICAgICAgICAgICAgc29sWyhLK2spJU5dW2ldID0gMSAgOwogICAgICAgICAgICAgICAgZWxzZQogICAgICAgICAgICAgICAgICAgIHNvbFsoSytrKSVOXVtpXSA9IDAgIDsKCiAgICAgICAgICAgICAgICBpZiAoYmluW2tdW2ldID09IDApCiAgICAgICAgICAgICAgICAgICAgemVyb2VzLS07CgogICAgICAgICAgICAgICAgaWYoYmluWygyKksrMStrKSVOXVtpXSA9PSAwKQogICAgICAgICAgICAgICAgICAgIHplcm9lcysrOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgTjsgaSsrKXsKICAgICAgICAgICAgYW5zID0gMCA7CiAgICAgICAgICAgIGZvcihpbnQgaiA9IDA7IGogPCAzMTsgaisrKXsKICAgICAgICAgICAgICAgIGFucyArPSBzb2xbaV1bal0gKiAocG93KDIsaikpIDsKICAgICAgICAgICAgfQogICAgICAgICAgICBwcmludGYoIiVkICIsIGFucyk7CiAgICAgICAgfQogICAgICAgIHByaW50ZigiXG4iKTsKICAgIH0KICAgIHJldHVybiAgMDsKfQo=