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;
using ll = long long;
using pii = pair<int, int>;

const vector<int> bs = {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, 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() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(10);

  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    int m = bs.size(), l = 0, r = 0;
    vector<int> p, q;
    while (l < m && bs[l] != a) l++, r++;
    while (r < m && bs[r] != b) p.emplace_back(bs[r++]);
    r = ++l;
    while (l < m && bs[l] != a) l++, r++;
    while (r < m && bs[r] != b) q.emplace_back(bs[r++]);
    if (int(p.size()) > int(q.size())) swap(p, q);
    for (int j = 0; j < int(p.size()); j++) {
      cout << p[j] << " ";
    }
    cout << b << endl;
  }  
  
  return 0;
}

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;
};
void show(std::vector<int> &route) {
	std::cout << route.at(0);
	for (auto i = 1; i < route.size(); ++i) {
		std::cout << ' ' << route.at(i);
	}
	std::cout << std::endl;
}
int main() {
	std::vector<std::shared_ptr<Bus_stop>> stops(15, nullptr);
	for (auto i = 0; i < 15; ++i) {
		stops.at(i) = std::make_shared<Loop_stop>(Loop_stop((i < 10) ? i : (15 - i)));
	}
	for (auto i = 0; i < 15; ++i) {
		stops.at(i)->set_next(*stops.at((i + 1) % 15));
	}
	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 route1 = stops.at(0)->get_route(from, to);
		auto route2 = stops.at(9)->get_route(from, to);
		if (route1.size() < route2.size()) {
			show(route1);
		}
		else {
			show(route2);
		}
	}
	return 0;
}

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>

using namespace std;

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

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

int main()
{
	const int stations[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 3, 2, 1, 0};
	int n;
	scanf("%d", &n);
	rep(i, n){
		int s, g;
		scanf("%d%d", &s, &g);
		
		for(s = s<g ? s : 15-s; stations[s] != stations[g]; ++s)
			printf("%d ", stations[s]);
		printf("%d\n", stations[g]);
	}
	return 0;
}

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 || 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));
	}
}