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<bits/stdc++.h>
using namespace std;
int main(){
	string ans[10][10]={
	{
		"0",
		"0 1",
		"0 1 2",
		"0 1 2 3",
		"0 1 2 3 4",
		"0 1 2 3 4 5",
		"0 1 2 3 4 5 6",
		"0 1 2 3 4 5 6 7",
		"0 1 2 3 4 5 6 7 8",
		"0 1 2 3 4 5 6 7 8 9"
	},
	{
		"1 0",
		"1",
		"1 2",
		"1 2 3",
		"1 2 3 4",
		"1 2 3 4 5",
		"1 2 3 4 5 6",
		"1 2 3 4 5 6 7",
		"1 2 3 4 5 6 7 8",
		"1 2 3 4 5 6 7 8 9"
	},
	{
		"2 1 0",
		"2 1",
		"2",
		"2 3",
		"2 3 4",
		"2 3 4 5",
		"2 3 4 5 6",
		"2 3 4 5 6 7",
		"2 3 4 5 6 7 8",
		"2 3 4 5 6 7 8 9"
	},
	{
		"3 2 1 0",
		"3 2 1",
		"3 2",
		"3",
		"3 4",
		"3 4 5",
		"3 4 5 6",
		"3 4 5 6 7",
		"3 4 5 6 7 8",
		"3 4 5 6 7 8 9"
	},
	{
		"4 3 2 1 0",
		"4 3 2 1",
		"4 3 2",
		"4 3",
		"4",
		"4 5",
		"4 5 6",
		"4 5 6 7",
		"4 5 6 7 8",
		"4 5 6 7 8 9"
	},
	{
		"5 4 3 2 1 0",
		"5 4 3 2 1",
		"5 4 3 2",
		"5 4 3",
		"5 4",
		"5",
		"5 6",
		"5 6 7",
		"5 6 7 8",
		"5 6 7 8 9"
	},
	{
		"6 7 8 9 5 4 3 2 1 0",
		"6 7 8 9 5 4 3 2 1",
		"6 7 8 9 5 4 3 2",
		"6 7 8 9 5 4 3",
		"6 7 8 9 5 4",
		"6 7 8 9 5",
		"6",
		"6 7",
		"6 7 8",
		"6 7 8 9"
	},
	{
		"7 8 9 5 4 3 2 1 0",
		"7 8 9 5 4 3 2 1",
		"7 8 9 5 4 3 2",
		"7 8 9 5 4 3",
		"7 8 9 5 4",
		"7 8 9 5",
		"7 8 9 5 4 3 2 1 0 1 2 3 4 5 6",
		"7",
		"7 8",
		"7 8 9"
	},
	{
		"8 9 5 4 3 2 1 0",
		"8 9 5 4 3 2 1",
		"8 9 5 4 3 2",
		"8 9 5 4 3",
		"8 9 5 4",
		"8 9 5",
		"8 9 5 4 3 2 1 0 1 2 3 4 5 6",
		"8 9 5 4 3 2 1 0 1 2 3 4 5 6 7",
		"8",
		"8 9"
	},
	{
		"9 5 4 3 2 1 0",
		"9 5 4 3 2 1",
		"9 5 4 3 2",
		"9 5 4 3",
		"9 5 4",
		"9 5",
		"9 5 4 3 2 1 0 1 2 3 4 5 6",
		"9 5 4 3 2 1 0 1 2 3 4 5 6 7",
		"9 5 4 3 2 1 0 1 2 3 4 5 6 7 8",
		"9"
	}
};
	int n,a,b;
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a>>b;
		cout<<ans[a][b]<<endl;
	}
	return 0;
}

C++: #include <iostream>
using namespace std;
int main(){
    int n;
    cin>>n;
    for (int j=0; j<n; j++) {
        int on,off;
        cin>>on>>off;
        if(on>off){
            if(on>=6&&off>=6){
                for(int i=on;i<10;i++)cout<<i<<" ";
                for(int i=5;i>0;i--)cout<<i<<" ";
                for(int i=0;i<off;i++)cout<<i<<" ";
            }else{
                int cnt=on;
                while (1) {
                    if(cnt==off)break;
                    cout<<cnt<<" ";
                    cnt--;
                }
            }
        }else{
            for(int i=on;i<off;i++)cout<<i<<" ";
        }
        cout<<off<<endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <climits>
#include <memory>
class Bus_stop {
public:
	Bus_stop(const int &i) :id{ i }, mins(10, INT_MAX - 1), prevs(10, nullptr){mins.at(i) = 0; };
	//?¬?????????????????¨????
	void set_next(Bus_stop &other) {
		//this ?????? other ????????????????????????
		sub_next(other);	//other ???????????????????¨???????????£????????????????????????????
	}
	//??¨??????????????¢?´¢
	void search_route() {
		call_sub_search(mins);	//private???????????????mins???virtual??¢??°?????±??§???????????????????????????
	};
	std::vector<int> get_route(const int &from, const int &to) const {
		return move(to)->sub_route(from);
	}
protected:
	//Bus_stop???ID
	int id;
	//?????????????????´??°??¨????¬??????¢?´¢???????§?????????????
	void sub_search(Bus_stop *next, const std::vector<int> &ms) {
		if (next->renew(ms, this)) {//?????????????????´??°????????£??????
			next->search_route(); //?¬??????¢?´¢???????§?
		}
	}
private:
	//???????????????????°??????????
	std::vector<int> mins;
	//????°???????????????¨???"???????????????"??????????????????
	std::vector<Bus_stop*> prevs;
	//mins??¨prevs?????´??°?????´??°???????????°true???????????°false
	bool renew(const std::vector<int> &prev, Bus_stop *from) {
		bool res = false;
		for (auto i = 0; i < prev.size(); ++i) {
			if (prev.at(i) + 1 < mins.at(i)) {
				mins.at(i) = prev.at(i) + 1;
				prevs.at(i) = from;
				res = true;
			}
		}
		return res;
	}
	//?????????????§????
	const Bus_stop *move(const int &to) const {
		if (to == id) {
			return this;
		}
		else {
			return prevs.at(to)->move(to);
		}
	}
	std::vector<int> sub_route(const int &from) const {
		if (from == id) {
			return std::vector<int>{from};
		}
		else {
			auto res = prevs.at(from)->sub_route(from);
			res.push_back(id);
			return res;
		}
	}
	//?¬???????????????????????????¢?´¢
	virtual void call_sub_search(const std::vector<int> &) = 0;
	//?¬?????????????????¨????
	virtual void sub_next(Bus_stop&) = 0;
};
class Loop_stop : public Bus_stop {
public:
	Loop_stop(const int &i) :Bus_stop(i), next{ nullptr } {};
private:
	void sub_next(Bus_stop &other) override {
		next = &other;
	}
	void call_sub_search(const std::vector<int> &mins) override {
		sub_search(next, mins);
	}
	Bus_stop *next;
};
class Line_stop : public Bus_stop {
public:
	Line_stop(const int &i) : Bus_stop(i), next1{ nullptr }, next2{ nullptr } {};
private:
	void sub_next(Bus_stop &other) override {
		if (next1) {
			next2 = &other;
		}
		else {
			next1 = &other;
		}
	}
	void call_sub_search(const std::vector<int> &mins) override {
		sub_search(next1, mins);
		sub_search(next2, mins);
	}
	Bus_stop *next1, *next2;
};
int main() {
	std::vector<std::shared_ptr<Bus_stop>> stops(10, nullptr);
	for (auto i = 1; i < 6; ++i) {
		stops.at(i) = std::make_shared<Line_stop>(Line_stop(i));
	}
	for (auto i = 6; i <= 10; ++i) {
		stops.at(i % 10) = std::make_shared<Loop_stop>(Loop_stop(i % 10));
	}
	for (auto i = 0; i < 9; ++i) {
		stops.at(i)->set_next(*stops.at(i + 1));
	}
	stops.at(9)->set_next(*stops.at(5));
	for (auto i = 5; i > 0; --i) {
		stops.at(i)->set_next(*stops.at(i - 1));
	}
	stops.at(0)->search_route();
	int n;
	std::cin >> n;
	for (auto i = 0; i < n; ++i) {
		int from, to;
		std::cin >> from >> to;
		auto route = stops.at(0)->get_route(from, to);
		std::cout << route.at(0);
		for (auto i = 1; i < route.size(); ++i) {
			std::cout << ' ' << route.at(i);
		}
		std::cout << std::endl;
	}
	return 0;
}

C++: #include <iostream>

using namespace std;

int main(void){

  int n;
  cin >> n;

  while(n--){

    int from,too;
    cin >> from >> too;

    if( from < too ){
      for( int i = from ; i <= too ; i++ ){
	if( i == too ) cout << i << endl;
	else cout << i << " ";
      }
    }
    else if( ( 0 <= from && from <= 5 ) && ( 0 <= too && too <= 5 ) ){
      for( int i = from ; i >= too ; i-- ){
	if( i == too ) cout << i << endl;
	else cout << i << " ";
      }
    }

    else if( 6 <= from && from <= 9 ){

      for( int i = from ; i <= 9 ; i++ )
	cout << i << " ";

      for( int i = 5 ; i >= too ; i-- ){
	if( i == too ) cout << i << endl;
	else cout << i << " ";
      }

      if( 6 <= too && too <= 9 ){
	cout << "5 4 3 2 1 0 1 2 3 4 5 ";   
	for( int i = 6 ; i <= too ; i++ ){
	  if( i == too ) cout << i << endl;
	  else cout << i << " ";
	}
      }

    }
    
  }
  return 0;
}