//I write this program, but keep on getting "runtime error".
//Thus, I ask Chan Man Hon for help, and find that
//the "rank" can be very large, and using "int" 
//leads to error.

#include <iostream>
#include <string>
using namespace std;

//The following line is copied from Chan Man Hon.
//It defines the type "ull".
typedef unsigned long long ull;

unsigned long long G[7]; //the value of G(i)
int pow2[7];

void init(){
  G[0] = 1;
  G[1] = 1;
  G[2] = 2 * G[1]*G[1];
  G[3] = 3 * G[2]*G[2];
  G[4] = 4 * G[3]*G[3];
  G[5] = 5 * G[4]*G[4];
  G[6] = 6 * G[5]*G[5];
  
  pow2[0] = 1;
  pow2[1] = 2;
  pow2[2] = 4;
  pow2[3] = 8;
  pow2[4] = 16;
  pow2[5] = 32;
  pow2[6] = 64;
}

void print( int no_bit, unsigned long long rank, bool* output[] ){
  if( no_bit==1 ){
    output[0][1] = !output[0][0];
    return;
  }
  
  //set the fixed_bit
  int fixed_bit = rank/(G[no_bit-1]*G[no_bit-1]);
  for( int i=1; i<pow2[no_bit-1]; i++ ){
    output[fixed_bit][i] = output[fixed_bit][0];
  }
  for( int i=pow2[no_bit-1]; i<pow2[no_bit]; i++ ){
    output[fixed_bit][i] = !output[fixed_bit][0];
  }
  
  //Prepare the remaining columns
  bool* next_output[6];
  int temp = 0;
  for( int i=0; i<no_bit; i++ ){
    if( i==fixed_bit ){
      ;
    }else{
      next_output[temp] = output[i];
      temp++;
    }
  }
  //print the upper half of the columns
  ull next_rank = rank%(G[no_bit-1]*G[no_bit-1]);

  ull upper_rank = next_rank / G[no_bit-1];
  print( no_bit-1, upper_rank, next_output );
  
  //print the lower half of the columns
  for( int i=0; i<no_bit-1; i++ ){
    next_output[i][ pow2[no_bit-1] ] = next_output[i][ pow2[no_bit-1]-1 ];
    next_output[i] = next_output[i]+ pow2[no_bit-1];    
  }
  ull lower_rank = next_rank % G[no_bit-1];
  print( no_bit-1, lower_rank, next_output );
}

int main(){
  int no_case;
  cin >> no_case;
  init();
  
  for( int i=0; i<no_case; i++ ){
    //get a clean table to store the output
    bool table[6][64];
    for( int i=0; i<6; i++ ){
      for( int j=0; j<64; j++ ){
        table[i][j] = 0;
      }
    }
    bool* output[6];
    for( int i=0; i<6; i++ ){
      output[i] = &(table[i][0]);
    }
    
    //read the input line
    char line[100];
    cin >> line;
    int no_bit = strlen( line );
    for( int i=0; i<no_bit; i++ ){
      output[i][0] = (line[i]=='1');
    }

    //This is the most tricky part! The rank
    //can be very large, and using "int" will fail!
    unsigned long long rank;
    cin >> rank;
    
    rank--;
    print( no_bit, rank, output );
    
    //print the ouput
    for( int j=0; j<pow2[no_bit]; j++ ){
      for( int i=0; i<no_bit; i++ ){
        cout << output[i][j];
      }
      cout << endl;
    }
    if( i<no_case-1) cout << endl;
  }
  return 1;
}


