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, 101)
	}
	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<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int n;
char s[105][105];
int dx[]{ -1,0,1,0 }, dy[]{ 0,1,0,-1 };

void dfs(int idx, int x, int y) {
	if (s[x][y] == '#')return;
	s[x][y] = '#';
	int nx = x + dx[idx], ny = y + dy[idx];
	if (nx < 0 || n <= nx || ny < 0 || n <= ny) {
		idx = (idx + 1) % 4;
		dfs(idx, x + dx[idx], y + dy[idx]);
		return;
	}
	int kx = nx + dx[idx], ky = ny + dy[idx];
	if (!(0 <= kx&&kx < n && 0 <= ky&&ky < n&&s[kx][ky] == '#')) {
		dfs(idx, nx, ny);
		return;
	}
	idx = (idx + 1) % 4;
	nx = x + dx[idx], ny = y + dy[idx];
	kx = nx + dx[idx], ky = ny + dy[idx];
	int jx = nx + dx[(idx + 1) % 4], jy = ny + dy[(idx + 1) % 4];
	if ((!(0 <= kx&&kx < n && 0 <= ky&&ky < n&&s[kx][ky] == '#'))&& (!(0 <= jx&&jx < n && 0 <= jy&&jy < n&&s[jx][jy] == '#'))) {
		dfs(idx, nx, ny);
		return;
	}
}
int main() {
	int d; scanf("%d", &d);
	rep(i, d) {
		scanf("%d", &n);
		rep(j, n)rep(k, n)s[j][k] = ' ';
		dfs(0, n - 1, 0);
		rep(j, n)printf("%s\n", s[j]);
	}
}

C++: #include <iostream>
#include <algorithm>

using namespace std;

main(){
  bool f = true;
  int n;
  char data[100][100];
  int dx[] = {0, 1, 0, -1};
  int dy[] = {-1, 0, 1, 0};
  int t;
  cin >> t;
  for(int tc=0;tc<t;tc++){
    cin >> n;
    if(!f) cout << endl;
    int y = n-1;
    int x = 0;
    fill(data[0], data[100], ' ');
    data[y][x] = '#';
    int d = 0;
    while(1){
      int ty = y + dy[d];
      int tx = x + dx[d];
      if(ty<0 || ty>=n){ d = (d+1)%4; continue; }
      if(tx<0 || tx>=n){ d = (d+1)%4; continue; }
      if(data[ty][tx] == '#') break;
      int tty = ty + dy[d];
      int ttx = tx + dx[d];
      if((tty>=0 || tty<n) && (ttx>=0 || ttx<n)){
	if(data[tty][ttx] == '#'){
	  d = (d+1)%4; 
	  continue; 
	}
      }
      int cnt = 0;
      for(int i=0;i<4;i++){
	int tty = ty + dy[i];
	int ttx = tx + dx[i];
	if(tty<0 || tty>=n) continue;
	if(ttx<0 || ttx>=n) continue;
	if(data[tty][ttx] == '#') cnt++;
      }
      if(cnt > 1) break;
      data[ty][tx] = '#';
      y = ty;
      x = tx;
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	cout << data[i][j];
      }
      cout << endl;
    }
    f = false;
  }
  return 0;
}

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

int main(){
  bool grid[100][100];
  int t,n;

  cin >> t;

  while(t--){
    cin >> n;
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++)grid[i][j] = false;
    }

    bool f[4];
    for(int i=0;i<4;i++)f[i] = true;
    int x = 0, y = n-1;
    grid[y][x] = true;

    while(f[0] || f[1] || f[2] || f[3]){
      for(int i=0;i<4;i++)f[i] = false;

      while(1){
	y--;
	if(y < 0 || (x+1 < n && grid[y][x+1]) || (x-1 >= 0 && grid[y][x-1]) || (y-1 >= 0 && grid[y-1][x]) ){
	  y++;
	  break;
	}
	f[0] = true;
	grid[y][x] = true;
      }

     while(1){
	x++;
	if(x >= n || (y+1 < n && grid[y+1][x]) || (y-1 >= 0 && grid[y-1][x]) || (x+1 < n && grid[y][x+1]) ){
	  x--;
	  break;
	}
	f[1] = true;
	grid[y][x] = true;
      }
  
     while(1){
	y++;
	if(y >= n ||(x-1 >= 0 && grid[y][x-1]) || (x+1 < n && grid[y][x+1]) || (y+1 < n && grid[y+1][x]) ){
	  y--;
	  break;
	}
	f[2] = true;
	grid[y][x] = true;
      }

      while(1){
	x--;
	if(x < 0 || (y+1 < n && grid[y+1][x]) || (y-1 >= 0 && grid[y-1][x]) || (x-1 >= 0 && grid[y][x-1]) ){
	  x++;
	  break;
	}
	f[3] = true;
	grid[y][x] = true;
      }
    }
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	if(grid[i][j])cout << "#";
	else cout << " ";
      }
      cout << endl;
    }
    if(t)cout << endl;
  }
}

C++: #include <iostream>
#include <algorithm>
#include <cstring>

using namespace std;

int dx[4]={-1,0,1,0};
int dy[4]={0,1,0,-1};

int main(void){
  int d;
  bool isfirst = true;
  cin >> d;

  for(int i=0;i<d;i++){
    int n;
    cin >> n;
    bool sharp[n][n];

    memset(sharp,false,sizeof(sharp));

    int x=n-1,y=0;
    int vec=0;
    int len = n-1;
    
    if(!isfirst) cout << endl;

    sharp[x][y] = true;
    while(len > 1){
      for(int j=0;j<len;j++){
	x += dx[vec];
	y += dy[vec];
	sharp[x][y] = true;
      }
      vec = (vec+1)%4;
      for(int j=0;j<len;j++){
	x += dx[vec];
	y += dy[vec];
	sharp[x][y] = true;
      }
      vec = (vec+1)%4;
      if(len == n-1){
	for(int j=0;j<len;j++){
	  x += dx[vec];
	  y += dy[vec];
	  sharp[x][y] = true;
	}
	vec = (vec+1)%4;
      }
      len -= 2;
    }
    if(len == 1){
      x += dx[vec];
      y += dy[vec];
      sharp[x][y] = true;
    }
    for(int p=0;p<n;p++){
      for(int q=0;q<n;q++){
	if(sharp[p][q]){
	  cout << "#";
	}else{
	  cout << " ";
	}
      }
      cout << endl;
    }
    isfirst = false;
  }
}

C++: #include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <deque>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <bitset>
#include <functional>
#include <utility>
#include <algorithm>
#include <numeric>
#include <typeinfo>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <ctime>

using namespace std;

#define dump(n) cout<<"# "<<#n<<"="<<(n)<<endl
#define debug(n) cout<<__FILE__<<","<<__LINE__<<": #"<<#n<<"="<<(n)<<endl
#define repi(i,a,b) for(int i=int(a);i<int(b);i++)
#define rep(i,n) repi(i,0,n)
#define iter(c) __typeof((c).begin())
#define foreach(i,c) for(iter(c) i=(c).begin();i!=(c).end();i++)
#define allof(c) (c).begin(),(c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int,int> pii;

void solve(int n)
{
	if(n==1){
		cout<<"#"<<endl;
		return;
	}
	vs a(n,string(n,'#'));
	int di[]={-1,0,1,0};
	int dj[]={0,1,0,-1};
	int ci=n-1,cj=1,k=0;
	rep(i,10000){
		a[ci][cj]=' ';
		int ti=ci+di[k]*2,tj=cj+dj[k]*2;
		if(ti<0 || n<=ti || tj<0 || n<=tj || a[ti][tj]==' ')
			k=(k+1)%4;
		else{
			ci+=di[k];
			cj+=dj[k];
		}
	}
	rep(i,n)
		cout<<a[i]<<endl;
}

int main()
{
	int d; cin>>d;
	rep(i,d){
		int n; cin>>n;
		solve(n);
	}
	
	return 0;
}