Go: package main

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

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		scanner.Scan()
		input := strings.Split(scanner.Text(), " ")
		s, _ := strconv.Atoi(input[0])
		e, _ := strconv.Atoi(input[1])
		a := make([]int, 0)
		if s <= 5 {
			if s > e {
				for j := s; j > e-1; j-- {
					a = append(a, j)
				}
			} else {
				for j := s; j < e+1; j++ {
					a = append(a, j)
				}
			}
		} else if s < e {
			for j := s; j < 10; j++ {
				a = append(a, j)
			}
		} else {
			for j := s; j < 10; j++ {
				a = append(a, j)
			}
			if e <= 5 {
				for j := 5; j > e-1; j-- {
					a = append(a, j)
				}
			} else {
				for j := 5; j > 0; j-- {
					a = append(a, j)
				}
				for j := 0; j < e+1; j++ {
					a = append(a, j)
				}
			}
		}
		for i, v := range a {
			if i != len(a)-1 {
				fmt.Printf("%d ", v)
			} else {
				fmt.Println(v)
			}
		}
	}
}

C++: 
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <cctype>
#include <sstream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <set>
#include <stack>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <iterator>
#include <bitset>
#include <complex>
#include <fstream>
using namespace std;
typedef long long ll;
const double EPS = 1e-9;
typedef vector<int> vint;
typedef pair<int, int> pint;
#define rep(i, n) REP(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define MSG(a) cout << #a << " " << a << endl;
#define REP(i, x, n) for(int i = x; i < n; i++)
template<class T> T RoundOff(T a){ return int(a+.5-(a<0)); }
template<class T, class C> void chmax(T& a, C b){ if(a < b) a = b; }
template<class T, class C> void chmin(T& a, C b){ if(b < a) a = b; }
template<class T, class C> pair<T, C> mp(T a, C b){ return make_pair(a, b); }

string toString(int x)
{
	stringstream ss; ss << x;
	return ss.str();
}

string rootString(int f, int t)
{
	int b = f < t ? 1 : -1;
	
	string res;
	
	for(int i = f; i != t; i += b)
		res += toString(i);
	
	return res + toString(t);
}

void print(string s)
{
	rep(i, s.size())
		cout << s[i] << (i == s.size() - 1 ? "\n" : " ");
}

int main()
{
	int n;
	cin >> n;
	
	while(n--)
	{
		int f, t;
		cin >> f >> t;
		
		if(f < t)	print(rootString(f, t));
		else
		{
			if(f <= 5)	print(rootString(f, t));
			else if(t < 5) print(rootString(f, 9) + toString(5) + rootString(4, t));
			else if(t == 5) print(rootString(f, 9) + toString(5));
			else print(rootString(f, 9) + rootString(5, 0) + rootString(1, t));
		}
	}
}

C++: #include<stdio.h>

int main(){

int n,s,f;
int w;

//while(1){
scanf("%d",&n);

for(int i=0;i<n;i++){
scanf("%d %d",&s,&f);

if(s<=5){
 if(s<f){
for(int i=s;i<=f;i++)
{
if(i!=s)printf(" ");
printf("%d",i);
}
     }
else if(s>f){
for(int i=s;i>=f;i--)
{
if(i!=s)printf(" ");
printf("%d",i);
}
}
}
if(s>5){
 if(f>s){
for(int i=s;i<=f;i++)
{
if(i!=s)printf(" ");
printf("%d",i);
}
}
 else if(5<f<s){
	 for(int i=s;i<=9;i++)
{
if(i!=s)printf(" ");
printf("%d",i);
}

for(int i=5;i>=0;i--)
{
printf(" ");
printf("%d",i);
}

for(int i=1;i<=f;i++)
{
printf(" ");
printf("%d",i);
}

}
 else if(f<=5){
for(int i=s;i<=9;i++)
{
if(i!=s)printf(" ");
printf("%d",i);
}

for(int i=5;i>=f;i--)
{
printf(" ");
printf("%d",i);
}

}
}printf("\n");
}

return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

int main() {
  
  int Tc; cin >> Tc;
  
  while(Tc--) {
    int a, b; cin >> a >> b;
    int const S = a;
    int const T = b;
    
    if(S < T && S < 5) {
      for(int i=S; i<=T; i++) {
	if(i>S) cout << " ";
	cout << i;
      }
      cout << endl;
    }
    else if(S > T && S < 5) {
      for(int i=S; i>=T; i--) {
	if(i<S) cout << " ";
	cout << i;
      }
      cout << endl;
    }
    else if(S == 5) {
      if(0<=T && T<5) {
	for(int i=S; i>=T; i--) {
	  if(i<S) cout << " ";
	  cout << i;
	}
	cout << endl;
      }
      else {
	for(int i=S; i<=T; i++) {
	  if(i>S) cout << " ";
	  cout << i;
	}
	cout << endl; goto EXIT;
      }
    }
    else {
      assert(S > 5);
      if(5 < T && T < S) {
	for(int i=S; i<10; i++) {
	  if(i>S) cout << " ";
	  cout << i;
	}
	for(int i=5; i>=0; i--) {
	  cout << " "; cout << i;
	}
	for(int i=1; i<=T; i++) {
	  cout << " "; cout << i;
	}
	cout << endl;
	
      }
      else if(S < T) {
	for(int i=S; i<=T; i++) {
	  if(i>S) cout << " ";
	  cout << i;
	}
	cout << endl;
      }
      else {
	for(int i=S; i<10; i++) {
	  if(i>S) cout << " ";
	  cout << i;
	}
	for(int i=5; i>=T; i--) {
	  cout << " " << i;
	}
	cout << endl;
      }
    }
    
  EXIT:;
  }
  
  return 0;
}

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

int main() {
	int n, src, dir = 1, dst, spc;
	cin >> n;
	for(int i=0;i<n;i++){
		cin >> src >> dst;
		spc = 0;
		if(src<dst || (src>dst && src>=6))dir = 1;
		else dir = -1;
		int j=0;
		do{
			if(src+j>9){
				j=5-src;
				dir = -1;
			}
			if(spc++ != 0)cout << " ";
			cout << src+j;
		}while(src+(dir>0 ? j++ : j--)!=dst);
		cout << endl;
	}
	// your code goes here
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <algorithm>
#include <math.h>
#include <map>
#include <functional>
#include <iomanip>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <typeinfo>
#define PI 3.14159265359
#define INF 99999999
#define rep(i, n) for(int i=0; i<n; i++)
#define REP(n) rep(i, n)
#define pb push_back
#define mp make_pair
typedef long long ll;
using namespace std;
typedef pair<int, int> P;

int n, s, t;
int stops[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2, 1, 0};
vector<int> ss1, ss2;
bool flag1, flag2;

int main()
{
	cin >> n;

	REP(n)
	{
		cin >> s >> t;
		
		ss1.clear();
		ss2.clear();

		flag1 = flag2 = false;

		//printf("s = %d, t = %d\n", s, t);

		if (0 <= s && s <= 5)
		{
			for (int j=s; j<16; j++)
			{
				ss1.pb(stops[j]);
				if (stops[j] == t)
				{
					flag1 = true;
					break;
				}
			}
			for (int j=15-s; j<16; j++)
			{
				ss2.pb(stops[j]);
				if (stops[j] == t)
				{
					flag2 = true;
					break;
				}
			}
		} else {
			for (int j=s; j<16; j++)
			{
				ss1.pb(stops[j]);
				if (stops[j] == t)
				{
					flag1 = true;
					break;
				}
			}
		}

		//printf("ss1.size = %d, ss2.size = %d\n", ss1.size(), ss2.size());

		if (flag1 && flag2 && ss1.size() != 0 && ss2.size() != 0 && ss1.size() < ss2.size())
		{
			rep(j, ss1.size())
			{
				cout << ss1[j];
				cout << ((j != ss1.size() - 1) ? " " : "\n");
			}
		}
		if (flag1 && flag2 && ss1.size() != 0 && ss2.size() != 0 && ss2.size() < ss1.size())
		{
			rep(j, ss2.size())
			{
				cout << ss2[j];
				cout << ((j != ss2.size() - 1) ? " " : "\n");
			}
		}
		if (flag1 && ss1.size() != 0 && ss2.size() == 0)
		{
			rep(j, ss1.size())
			{
				cout << ss1[j];
				cout << ((j != ss1.size() - 1) ? " " : "\n");
			}
		}
		if (flag2 && ss1.size() == 0 && ss2.size() != 0)
		{
			rep(j, ss2.size())
			{
				cout << ss2[j];
				cout << ((j != ss2.size() - 1) ? " " : "\n");
			}
		}
		if (flag1 && ss1.size() == ss2.size())
		{
			rep(j, ss1.size())
			{
				cout << ss1[j];
				cout << ((j != ss1.size() - 1) ? " " : "\n");
			}
		}
		if (flag2 && ss1.size() == ss2.size())
		{
			rep(j, ss2.size())
			{
				cout << ss2[j];
				cout << ((j != ss2.size() - 1) ? " " : "\n");
			}
		}
	}
	
	return 0;
}