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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;

int dy[] = {-1, 0, 1, 0};
int dx[] = {0, 1, 0, -1};
int n;
string s[110];

bool is_valid(int y, int x, int d) {
  if (y < 0 || y >= n || x < 0 || x >= n) return false;
  for (int i = 0; i < 4; i++) {
    int ny = y + dy[i], nx = x + dx[i];
    if (i == (d + 2) % 4) continue;
    if (ny >= 0 && ny < n && nx >= 0 && nx < n && s[ny][nx] == '#') return false;
  }
  return true;
}

void solve() {
  cin >> n;
  fill_n((string*)s, 110, string(n, ' '));
  int y = n - 1, x = 0, d = 0;
  while (true) {
    s[y][x] = '#';
    if (!is_valid(y + dy[d], x + dx[d], d)) (d += 1) %= 4;
    if (!is_valid(y + dy[d], x + dx[d], d)) break;
    y += dy[d], x += dx[d];
  }
  for (int i = 0; i < n; i++) {
    cout << s[i] << endl;
  }
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);
  
  int d;
  cin >> d;
  for (int i = 0; i < d; i++) {
    if (i) cout << endl;
    solve();
  }  

  return 0;
}

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

char ptn[104][105];
const int dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
int dir;

bool check( int x, int y, int dir )
{
	int nx = x + dx[dir], ny = y + dy[dir];

	for( int i = 0; i != 4; ++i )
	{
		int nnx = nx + dx[i], nny = ny + dy[i];

		if( nnx == x && nny == y )
			continue;

		if( ptn[nny][nnx] == '#' )
			return false;
	}

	return true;
}

int main()
{
	int d;
	std::cin >> d;
	while( d-- )
	{
		int n;
		std::cin >> n;

		for( int i = 0; i != n + 4; ++i )
			for( int j = 0; j != n + 4; ++j )
				ptn[i][j] = !i || !j || i == n + 3 || j == n + 3 ? '#' : ' ';

		int x = 2, y = n + 1;
		bool fl = false;
		dir = 0;

		while( true )
		{
			ptn[y][x] = '#';

			if( !check( x, y, dir ) )
			{
				if( !fl )
				{
					dir = ( dir + 1 ) & 3;
					fl = true;
					continue;
				}
				else
					goto end;
			}
			else
				fl = false;

			x += dx[dir];
			y += dy[dir];
		}

end:;

		for( int i = 2; i != n + 2; ++i )
		{
			for( int j = 2; j != n + 2; ++j )
				std::cout << ptn[i][j];

			std::cout << std::endl;
		}
	}

	return 0;
}

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){
    
    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(){
    /*
    char init[100][100];
    for(int i=0; i<100; i++){
        for(int j=0; j<100; j++){
            init[i][j]=' ';
        }
    }
     */
    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;
    }
}

C++: // clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

main {
  int d;
  cin >> d;
  while (d--) {
    int n;
    cin >> n;
    vs v(n, string(n, ' '));
    vi di = {-1, 0, 1, 0}, dj = {0, 1, 0, -1};
    auto canPut = [&](int i, int j) {
      if (i < 0 || j < 0 || i >= n || j >= n) return false;
      int c = 0;
      c += i - 1 >= 0 && v[i - 1][j] == '#';
      c += j - 1 >= 0 && v[i][j - 1] == '#';
      c += i + 1 < n && v[i + 1][j] == '#';
      c += j + 1 < n && v[i][j + 1] == '#';
      return c <= 1;
    };
    int i = n, j = 0, t = 0;
    forever {
      int ni = i + di[t], nj = j + dj[t];
      if (canPut(ni, nj)) {
        i = ni;
        j = nj;
        v[i][j] = '#';
        continue;
      }
      t = (t + 1) % 4;
      ni = i + di[t], nj = j + dj[t];
      if (canPut(ni, nj)) {
        i = ni;
        j = nj;
        v[i][j] = '#';
        continue;
      }
      break;
    }
    for (auto &x : v) cout << x << endl;
    if (d) cout << endl;
  }
}