Go: package main

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

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<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<vector>
#include<list>
#include<stack>
#include<queue>
#include<deque>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
#include<cstdlib>
#include<climits>
#include<utility>
#include<cctype>
using namespace std;

#define ull unsigned long long
#define stt(it,order) sort(it.begin(),it.end(),order)
#define st(it) sort(it.begin(),it.end())
#define ll long long
#define rep(i,j) for(int i=0;i<j;++i)
#define loop(i,a,j) for(int i=a;i<j;++i)
#define mpi(it,table,T,E) for(map<T,E>::iterator it = table.begin();it!=table.end();++it)
#define PI 3.141592654
#define pb(v,e) v.push_back(e)

string sint(int i){
   stringstream buf;
   buf<<i;
   return buf.str();
}

/*
   ifstream fin("input.txt");
   ofstream fout("output.txt");
#define cin fin
#define cout fout
*/

int graph[10][10] = { 
   {0,1,0,0,0,0,0,0,0,0},
   {1,0,1,0,0,0,0,0,0,0},
   {0,1,0,1,0,0,0,0,0,0},
   {0,0,1,0,1,0,0,0,0,0},
   {0,0,0,1,0,1,0,0,0,0},
   {0,0,0,0,1,0,1,0,0,0},
   {0,0,0,0,0,0,0,1,0,0},
   {0,0,0,0,0,0,0,0,1,0},
   {0,0,0,0,0,0,0,0,0,1},
   {0,0,0,0,0,1,0,0,0,0}
};

int sol[10];

void print(int i,int e){
   if( i == -1)return;
   print( sol[i] , 0);
   if( e ){
      printf("%d\n",i);
   }else{
      printf("%d ",i);
   }
}

int main(){
   int u;
   scanf("%d",&u);
   rep(i,u){
   int j,k;
      scanf("%d %d",&j,&k);
      queue<int> next;
      bool vis[10];
      memset(vis,0,sizeof(vis));
      memset(sol,-1,sizeof(sol));

      next.push(j);
      vis[j] = true;
      sol[j] = -1;
      int nx;
      while( !next.empty() ){
         nx = next.front();
         next.pop();
         if(nx == k)break;
         rep(i,10){
            if( graph[nx][i] && !vis[i]){
               vis[i] = true;
               sol[i] = nx;
               next.push(i);
            }
         }
      }

      print(k,1);
   }
   return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin>>n;
    int num[]={0,1,2,3,4,5,6,7,8,9,5,4,3,2,1,0};
    for(int i=0,x,y;i<n;i++){
        cin>>x>>y;
        if(x<6&&y<6){
            int sum=y-x;
            if(sum>0){
                cout<<num[x];
                for(int j=x+1;j<=y;j++)cout<<" "<<num[j];
                cout<<endl;
            }
            else {
                cout<<num[15-x];
                for(int j=16-x;j<16-y;j++)cout<<" "<<num[j];
                cout<<endl;
            }
        }
        else{
            cout<<num[x];
            for(int j=x+1;num[j-1]!=y;j++)cout<<" "<<num[j];
            cout<<endl;
        }
    }
}

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

using namespace std;

int main()
{
    int n;
    
    cin >> n;
    for (int i = 0; i < n; i++) {
        int s, e;

        cin >> s >> e;

        if (s <= 5) {
            if (s < e) {
                while (s != e) 
                    printf("%d ", s++);
                printf("%d\n", e);
            }
            else {
                while (s != e)
                    printf("%d ", s--);
                printf("%d\n", e);
            }
        }
        else {
            while (s != e) {
                printf("%d ", s);
                s = (s + 1) - ((s + 1) / 10) * 5;
            }
            printf("%d\n", e);
        }
    }

    return 0;
}

C++: #include<bits/stdc++.h>
#define rep(i,n)for(int i=0;i<n;i++)
using namespace std;

int bus[]{ 0,1,2,3,4,5,6,7,8,9,5,4,3,2,1,0,1,2,3,4,5,6,7,8,9,5,4,3,2,1,0 };
int main() {
	int n; scanf("%d", &n);
	rep(t, n) {
		int s, g; scanf("%d%d", &s, &g);
		int x = 0, y = 31;
		for (int i = 0; i < 31; i++) {
			for (int j = i + 1; j < 31; j++) {
				if (bus[i] == s&&bus[j] == g&&j - i < y - x) {
					x = i; y = j;
				}
			}
		}
		for (int i = x; i <= y; i++) {
			printf("%d", bus[i]);
			if (i == y)printf("\n");
			else printf(" ");
		}
	}
}

C++: #include<iostream>
#include<queue>
using namespace std;
int main()
{
	int n;

	cin >> n;
	for(int i = 0; i < n; ++i){
		int st, ed;
		queue<int> q;
		cin>>st>>ed;

		if( ed <= 5 ){
			if( st > 5 ){
				for(int i = st; i <= 9; ++i) q.push( i ); 
				for(int i = 5; i >= ed; --i) q.push( i );
			}else{
				if( st > ed )
					for(int i = st; i >= ed; --i) q.push( i );
				else
					for(int i = st; i <= ed; ++i) q.push( i );
			}
		}else{
			if( st < ed ){
				for(int i = st; i <= ed; ++i) q.push( i );
			}else{
				for(int i = st; i <= 9; ++i) q.push( i );
				for(int i = 5; i <= ed; ++i) q.push( i );
			}
		}
		cout << q.front();
		q.pop();
		while( !q.empty() ){
			cout << ' ' << q.front();
			q.pop();
		}
		cout << '\n';
	}
	return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <fstream>
#include <complex>
#include <stack>
#include <queue>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
static const double EPS = 1e-5;

#define FOR(i,k,n) for (int i=(k); i<(int)(n); ++i)
#define REP(i,n) FOR(i,0,n)

int main(void){
  int n;
  cin>>n;
  while(n--){
    int a,b;
    vector<int> ans;
    cin>>a>>b;
    bool rev=false;
    if(a>b){
      swap(a,b);
      rev = true;
    }
    if(a<5){
      for(int i=a; i<5&&i<=b;i++){
        ans.push_back(i);
      }
    }
    if(b>=5){
      if(b<=7){
        for(int i=5; i<=b; i++){
          ans.push_back(i);
        }
      }else{
        ans.push_back(5);
        for(int i=9; i>=b; i--){
          ans.push_back(i);
        }
      }
    }
    if(rev) reverse(ans.begin(),ans.end());
    REP(i,ans.size()){
      if(i) cout<<" ";
      cout<<ans[i];
    }
    cout<<endl;
  }
  return 0;
}