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<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

#define rep(i, n)for((i) = 0;(i) < (n);++(i))

char f[100][100];
int dh[4] = {-1, 0, 1, 0};
int dw[4] = {0, 1, 0, -1};
int main(void){
	int d, n, i, j;
	scanf("%d", &d);
	for(;d--;){
		scanf("%d", &n);
		rep(i, n)rep(j, n){
			f[i][j] = ' ';
			if(i == 0 || j == 0 || i == n - 1 || j == n - 1)f[i][j] = '#';
		}
		
		int w = 2, h = n - 1, k = 0;
		f[h][w - 1] = ' ';		
		for(int times = 200000;times--;){
			//printf("%d %d\n", h, w);
			f[h][w] = '#';
			if(f[h + dh[k] + dh[k]][w + dw[k] + dw[k]] == '#')k = (k + 1) % 4;
			else if(f[h + dh[k] + dh[(k + 1) % 4]][w + dw[k] + dw[(k + 1) % 4]] == '#')k = (k + 1) % 4;
			else if(f[h + dh[k] + dh[(k + 3) % 4]][w + dw[k] + dw[(k + 3) % 4]] == '#')k = (k + 1) % 4;
			else{ h += dh[k]; w += dw[k]; }
		}

		rep(i, n){
			rep(j, n)printf("%c",f[i][j]);
			puts("");
		}
		if(d >= 0)puts("");
	}
	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <iostream>
#include <complex>
#include <algorithm>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <cmath>
#include <math.h>
#include <numeric>
#include <list>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <climits>
#include <set>
#include <memory.h>
#include <memory>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cassert>
#include <map>
#include <cassert>
#include <time.h>
#include <ctime>

using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int , P> PP;
typedef pair<int, string> Ps;
typedef vector<int> vec;
typedef vector<vec> mat;
const int INF= 1 << 30;
const double EPS = 1e-9;
const double PI = 3.1415926535897932384626433832795;
const int CLK = CLOCKS_PER_SEC;

#define rep(i, n) for(int i = 0; i < n; i++)

double cross(const xy_t &a, const xy_t &b){
	return imag(conj(a) * b);
}

double dot(const xy_t &a, const xy_t &b){
	return real(conj(a) * b);
}

xy_t projection(const line &l, const xy_t &p){
	double t = dot(p - l.first, l.first - l.second) / norm(l.first - l.second);
	return l.first + t * (l.first - l.second);
}

bool intersectSP(const line &l, const xy_t &p){
	return abs(l.first- p) + abs(l.second - p) - abs(l.first - l.second) < EPS;
}

double distanceSP(line s, xy_t p){
	const xy_t r = projection(s, p);
	if(intersectSP(s, r)) return abs(r - p);
	return min(abs(s.first - p), abs(s.second - p));
}

int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
char field[200][200];

int main(){
	int n, d;
	cin >> d;
	rep(t, d){
		cin >> n;
		fill(&field[0][0], &field[199][199] + 1, '#');
		rep(i, n + 2){
			rep(j, n + 2) field[i+1][j+1] = ' ';
		}
		int x = 2;
		int y = 2 + n - 1;
		int cnt = 0;
		int dire = 0;
		while(true){
			cnt = 0;
			while(field[y + 2 * dy[dire]][x + 2 * dx[dire]] != '#'){
				cnt++;
				field[y][x] = '#';
				y += dy[dire];
				x += dx[dire];
			}
			field[y][x] = '#';
			if(cnt < 2) break;
			dire = (dire + 1) % 4;
		}
		rep(i, n){
			rep(j, n) cout << field[i+2][j+2];
			cout << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
using namespace std;

typedef istringstream ISS;
typedef ostringstream OSS;
typedef vector<string> VS;
typedef int INT;
typedef vector<INT> VI;
typedef vector<VI> VVI;
typedef pair <INT, INT> II;
typedef vector <II> VII;

template<class T> ostream& operator << ( ostream& os, vector<T> v ) {
    for ( typename vector<T>::iterator it_i = v.begin(); it_i != v.end(); ++it_i ) {
        os << *it_i << ", ";
    }
    return os;
}


void solve( int d ) {
    bool T[d][d];
    int r = d - 1, c = 0;
    const int dr[4] = { -1, 0, 1, 0 };
    const int dc[4] = { 0, 1, 0, -1 };
    int k = 0;
    fill( (bool*)T, (bool*)T+d*d, false );

    for ( int lpc = 0; lpc < d * d; ++ lpc ) {
        T[r][c] = true;
        bool flag = false;
        for ( int i = 0; i < 2; ++ i ) {
            int nr = r + dr[k];
            int nc = c + dc[k];
            if ( nr < 0 || nr >= d || nc < 0 || nc >= d ) {
                k = ( k + 1 ) % 4;
                continue;
            }
            int nnr = r + dr[k] * 2;
            int nnc = c + dc[k] * 2;
            if ( nnr < 0 || nnr >= d || nnc < 0 || nnc >= d ) {
                r = nr;
                c = nc;
                flag = true;
                break;
            }
            if ( T[nnr][nnc] ) {
                k = ( k + 1 ) % 4;
                continue;
            }
            r = nr;
            c = nc;
            flag = true;
            break;
        }
        int cnt = 0;
        for ( int i = 0; i < 4; ++ i ) {
            int nr = r + dr[i];
            int nc = c + dc[i];
            if ( nr < 0 || nr >= d || nc < 0 || nc >= d ) continue;
            if ( T[nr][nc] ) cnt ++;
        }
        if ( cnt > 1 ) break;
    }

    for ( int i = 0; i < d; ++ i ) {
        for ( int j = 0; j < d; ++ j ) {
            cout << ( T[i][j] ? '#' : ' ' );
        }
        cout << endl;
    }
}

int main() {
    int n;
    cin >> n;
    for ( int i = 0; i < n; ++ i ) {
        int d;
        cin >> d;
        solve( d );
        if ( i + 1 < n ) cout << endl;
    }
    return 0;
}

C++: #include<stdio.h>
int main()
{
	const int N = 127;
	char a[N][N];

	int n,d,caseCount=0,i,j,x,y,dd[]={-1,0,1,0},angle,c;
	scanf("%d",&d);
	while(d--)
	{
		scanf("%d",&n);
		for(i=0;i<n;++i)for(j=0;j<n;++j)a[i][j]=' ';

		x=0;y=n-1;
		angle=0;
		for(c=0;c<3;++c)
		{
			for(;x>=0&&y>=0&&x<n&&y<n; x+=dd[(angle+1)%4],y+=dd[angle])
			{
				a[y][x]='#';
			}
			x-=dd[(angle+1)%4];
			y-=dd[angle];
			angle=(angle+1)%4;;
		}
		for(;;)
		{
c=0;
			for(; a[y+2*dd[angle]][x+2*dd[(angle+1)%4]]!='#'; x+=dd[(angle+1)%4],y+=dd[angle])
				a[y][x]='#',c++;
			if(c<2) break;
			angle=(angle+1)%4;;
		}
		a[y][x]='#';

		printf("%s",(caseCount++?"\n":""));
		for(i=0;i<n;++i)
		{
			for(j=0;j<n;++j)
				putchar(a[i][j]);
			putchar('\n');
		}
	}
	return 0;
}