Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
	)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	out := make([][]string, 100)
	for i := range out {
		out[i] = make([]string, 100)
	}
	dx := []int{0, 1, 0, -1}
	dy := []int{-1, 0, 1, 0}
	scanner.Scan()
	a, _ := strconv.Atoi(scanner.Text())
	for a > 0 {
		a--
		scanner.Scan()
		b, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				out[i][j] = " "
			}
			out[i][b] = "0"
		}
		if b == 1 {
			fmt.Println("#")
			if a != 0 {
				fmt.Println()
			}
			continue
		}
		if b == 2 {
			fmt.Println("##")
			if a != 0 {
				fmt.Println()
				continue
			}
		}
		row := b -1
		col := 0
		dir := 0
		l := 0
		out[row][col] = "#"
		for {
			nrow := row+dy[dir%4]
			ncol := col+dx[dir%4]
			if dir%4 == 0 && nrow == 0 || dir%4==3&&ncol==0 || dir%4==2&&nrow==b-1 || dir%4==1&&ncol==b-1 {
				row=nrow
				col=ncol
				out[row][col]="#"
				dir++
				l = 1
			} else if out[nrow+dy[dir%4]][ncol+dx[dir%4]]=="#" {
				if l < 3 {
					break
				}
				dir++
				l = 1
			} else {
				row=nrow
				col=ncol
				out[row][col]="#"
				l++
			}
		}
		for i := 0; i < b; i++ {
			for j := 0; j < b; j++ {
				fmt.Printf("%s", out[i][j])
			}
			fmt.Println()
		}
		if a != 0 {
			fmt.Println()
		}
	}
}

C++: #include<iostream>
using namespace std;
#define MAX_D 101


bool dx[MAX_D][MAX_D];

void resetDx();
void makeDx();
void dispDx();
bool checkField(int n);
void make2();
int d;

int main(){
    int n;
    cin >>n;
    for(int i = 0; i < n; i++){
        resetDx();
        cin >>d;
        if(d == 2){
            make2();
        } else{
        makeDx();
        }
        /*for(int i = 0; i < d; i++){
            for(int j = 0; j < d; j++){
                cout <<dx[i][j];
            }
            cout <<endl;
        }*/
    }
    return 0;
}

void resetDx(){
    for(int i = 0; i < MAX_D; i++){
        for(int j = 0; j < MAX_D; j++){
            dx[i][j] = false;
        }
    }
}

void dispDx(){
    for(int i = 0; i < d; i++){
        for(int j = 0; j < d; j++){
            if(dx[i][j] == true) cout <<'#';
            else cout <<' ';
        }
        cout <<endl;
    }
}

void makeDx(){
    for(int i = 0; i < d; i++){
        dx[i][0] = true;
        dx[0][i] = true;
        dx[i][d - 1] = true;
    }
    int x = d - 1, y = d - 1, mx[4] = {-1, 0, 1, 0}, my[4] = {0, -1, 0, 1}, i = 0;
    bool moveFlg = false;
    while(true){
        while(checkField(x + mx[i]) && checkField(y + my[i]) && !dx[y + my[i] + my[i]][x + mx[i] + mx[i]]){
            if(!moveFlg) moveFlg = true;
            dx[y + my[i]][x + mx[i]] = true;
            x += mx[i]; y += my[i];
        }
        if(!moveFlg) break;
        //cout <<"now = " <<y <<", " <<x <<" iBefore = " <<i <<endl;
        i = (i + 1) % 4;
        int conti = (i + 2) % 4;
        int ny = y + my[i], nx = x + mx[i];
        bool endFlg = false;
        //cout <<"ny = " <<y <<"+" <<my[i] <<"=" <<ny <<", nx = " <<x <<"+" <<mx[i] <<"=" <<nx <<endl;
        for(int j = 0; j < 4; j++){
            if(j == conti) continue;
            else{
                if(checkField(ny + my[j]) && checkField(nx + mx[j])){
                    //cout <<"j = " <<j <<" ny + my[j] = " <<ny + my[j] <<", nx + mx[j] = " <<nx + mx[j];
                    if(dx[ny + my[j]][nx + mx[j]]){
                        //cout <<" true" <<endl;
                        endFlg = true;
                        break;
                    } else{
                        //cout <<" false" <<endl;
                    }
                }
            }
        }
        if(endFlg){
            break;
        }
        //dispDx();
    }
    dispDx();
}

bool checkField(int n){
    bool ret = true;
    if(n < 0 || n >= d) ret = false;
    return ret;
}

void make2(){
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2; j++){
            if(i == 1 && j == 1){
                dx[i][j] = false;
            } else{
                dx[i][j] = true;
            }
        }
    }
    dispDx();
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>
#include <math.h>
#include <string.h>

/*
 
 #####
 #   #
 # # #
 # # #
 # ###
 
 ######
 #    #
 # ## #
 # #  #
 # #  #
 # ####
 
 */
using namespace std;


void f(char guruguru[100][100], int l, int d, int x, int y){
    while(1){
        if(l<=0)return;
        
        if(d==0){//右下
            for(int i=0; i<l; i++){
                guruguru[y][x+i]='#';
            }
            x=x+(l-1);
            
            if(l==2)return;
            
            for(int i=0; i<l; i++){
                guruguru[y+i][x]='#';
            }
            y=y+(l-1);
            l=l-2;
            d=1;
            //f(guruguru,l,d,x,y);
        }
        
        if(d==1){//左上
            
            
            for(int i=0; i<l; i++){
                guruguru[y][x-i]='#';
            }
            x=x-(l-1);
            
            if(l<=2)return;
            
            for(int i=0; i<l; i++){
                guruguru[y-i][x]='#';
            }
            y=y-(l-1);
            
            l=l-2;
            d=0;
            //f(guruguru,l,d,x,y);
        }
    }
}

void f0(char guruguru[100][100], int l, int d, int x, int y){
    for(int i=0; i<l; i++){
        guruguru[i][0]='#';
    }
    
    f(guruguru,l,0,0,0);
}

/*
 int main(){
 
 for(int i=0; i<100; i++){
 for(int j=0; j<100; j++){
 guruguru_0[i][j]=' ';
 guruguru_1[i][j]=' ';
 guruguru_2[i][j]=' ';
 guruguru_3[i][j]=' ';
 }
 }
 
 
 //f0(guruguru_0, 100, 0, 0, 0);
 //f0(guruguru_3, 99, 0, 0, 0);
 //f0(guruguru_2, 98, 0, 0, 0);
 //f0(guruguru_1, 97, 0, 0, 0);
 
 
 
 int n;
 cin>>n;
 for(int roop=0; roop<n; roop++){
 int l;
 cin>>l;
 if(l%4==0){
 int x=50-l/2;
 int y=50-l/2;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_0[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 }else if(l%4==1){
 int x=50-l/2-2;
 int y=50-l/2-2;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_1[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 } else if(l%4==2){
 int x=50-l/2-1;
 int y=50-l/2-1;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_2[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 }else if(l%4==3){
 int x=50-l/2-1;
 int y=50-l/2-1;
 for(int i=y; i<y+l; i++){
 for(int j=x; j<x+l; j++){
 cout<<guruguru_3[i][j];
 }
 cout<<endl;
 }
 cout<<endl;
 }
 }
 
 }
 */
int main(){
    
    int n;
    cin>>n;
    for(int roop=0; roop<n; roop++){
        int l;
        cin>>l;
        char guruguru[100][100];
        //memcpy(guruguru,init,sizeof(init));
        for(int i=0; i<l; i++){
            for(int j=0; j<l; j++){
                guruguru[i][j]=' ';
            }
        }
        
        f0(guruguru,l,0,0,0);
        
        for(int i=0; i<l; i++){
            for(int j=0; j<l; j++){
                cout<<guruguru[i][j];
            }
            cout<<endl;
        }
        cout<<endl;
    }
}