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 <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <utility>
#include <functional>
#include <iterator>
using namespace std;

const int INF = (int)1e9;
typedef pair<int, int> P;

struct Edge {
    int to, cost;
    Edge(int t, int c) : to(t), cost(c) { }
};

pair<int, vector<int> > dijkstra_with_route(const vector<vector<Edge> > &edges, const int from, const int to) {
    vector<int> dist(edges.size(), INF);
    vector<int> prev(edges.size(), -1);

    priority_queue<P, vector<P>, greater<P> > Q;
    Q.push(P(0, from));
    dist[from] = 0;

    while(!Q.empty()) {
        P p = Q.top(); Q.pop();
        int v = p.second;
        int d = p.first;
        if(v == to) break;
        if(d > dist[v]) continue;
        for(int i = 0; i < (int)edges[v].size(); i++) {
            Edge e = edges[v][i];
            int c = d + e.cost;
            if(c < dist[e.to]) {
                dist[e.to] = c;
                Q.push(P(c, e.to));
                prev[e.to] = v;
            }
        }
    }

    vector<int> res;
    int u = to;
    while(u != from) {
        res.push_back(u);
        u = prev[u];
    }
    res.push_back(from);
    reverse(res.begin(), res.end());
    return make_pair(dist[to], res);
}

int main() {
    vector<vector<Edge> > edges(10);
    edges[0].push_back(Edge(1, 1));
    edges[1].push_back(Edge(0, 1));
    edges[1].push_back(Edge(2, 1));
    edges[2].push_back(Edge(1, 1));
    edges[2].push_back(Edge(3, 1));
    edges[3].push_back(Edge(2, 1));
    edges[3].push_back(Edge(4, 1));
    edges[4].push_back(Edge(3, 1));
    edges[4].push_back(Edge(5, 1));
    edges[5].push_back(Edge(4, 1));
    edges[5].push_back(Edge(6, 1));
    edges[6].push_back(Edge(7, 1));
    edges[7].push_back(Edge(8, 1));
    edges[8].push_back(Edge(9, 1));
    edges[9].push_back(Edge(0, 1));

    int N; cin >> N;
    while(N--) {
        int from, to; cin >> from >> to;
        auto res = dijkstra_with_route(edges, from, to).second;
        for(int i = 0; i < (int)res.size() - 1; i++) {
            cout << res[i] << ' ';
        }
        cout << *res.rbegin() << endl;
    }
}

C++: #include<iostream>
using namespace std;
void p(int x,int y){
 
  cout<<x;
  for(int i=x+1;i<=y;i++){
    cout<<' '<<i;
  }
  
}
void pb(int x,int y){
  cout<<x;
  for(int i=x-1;i>=y;i--){
    cout<<' '<<i;
  }

}
int main(){
  int n,a,b;
  cin>>n;
  while(n--){
    cin>>a>>b;
    if(a<=5&&b<=5){
      if(a<b)p(a,b);
      else {
	
	pb(a,b);
      }
      cout<<endl;
    }else if(a<=5&&b>=6){

      if(a<=4){p(a,4);cout<<' ';}
      p(5,b);
      cout<<endl;

    }else if(a>=6&&b<=5){

      p(a,9);cout<<' ';
      pb(5,b);cout<<endl;

    }else{
      
      if(a<=b){
	p(a,b);cout<<endl;
      }else{
	p(a,9);cout<<' ';
	pb(5,0);cout<<' ';
	p(1,4);cout<<' ';
	p(5,b);cout<<endl;
      }

    }
  }
  
}

C++: #define _USE_MATH_DEFINES
#include "bits/stdc++.h"
using namespace std;

//#define int long long
#define DBG 1
#define dump(o) if(DBG){cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) if(DBG){cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define each(it,c) for(auto it=(c).begin();it!=(c).end();it++)
#define all(c) c.begin(),c.end()
const int INF = sizeof(int) == sizeof(long long) ? 0x3f3f3f3f3f3f3f3fLL : 0x3f3f3f3f;
const int MOD = (int)(1e9 + 7);
void up(int x,bool f) {
	bool dir = f;
	if (dir) {
		x++;
		if (x == 10) {
			x = 5;
		}
	}
}
signed main() {
	int n; cin >> n;
	while (n--) {
		int a, b; cin >> a >> b;
		int x = a;
		vector<int>v,w;
		bool dir = true;
		while (x!=b) {
			v.push_back(x);
			if (dir) {
				if (x == 9) {
					x = 5;
					dir = false;
				}
				else x++;
			}
			else {
				if (x == 0) {
					dir = true;
				}
				else x--;
			}
		}
		v.push_back(x);
		dir = false;
		x = a;
		while (x != b) {
			w.push_back(x);
			if (6 <= x&&x <= 9)dir = true;
			if (dir) {
				if (x == 9) {
					x = 5;
					dir = false;
				}
				else x++;
			}
			else {
				if (x == 0) {
					dir = true;
				}
				else x--;
			}
		}
		w.push_back(x);

		if (v.size() < w.size()) {
			rep(i, 0, v.size()) {
				if (i)cout << " ";
				cout << v[i];
			}
			cout << endl;
		}
		else {
			rep(i, 0, w.size()) {
				if (i)cout << " ";
				cout << w[i];
			}
			cout << endl;
		}

	}
	return 0;
}

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

using namespace std;

int line[15];

void init() {
    for (int i = 0; i < 14; i++) {
        line[i] = i+1;
    }
    line[14] = 0;
}

int main() {
    init();
    int n; cin >> n;
    for (int i = 0; i < n; i++) {
        int s, g; cin >> s >> g;
        vector<int> path;
        int as = s;
        int ag = g;
        if (1 <= s && s <= 5) as = 15 - s;
        if (1 <= g && g <= 5) ag = 15 - g;

        int c = s;
        while (c != g && c != ag) {
            path.push_back(c);
            c = line[c];
        }
        path.push_back(g);

        if (as != s) {
            vector<int> opath;
            c = as;
            while (c != g && c != ag) {
                opath.push_back(c);
                c = line[c];
            }
            opath.push_back(g);
            if (path.size() > opath.size()) {
                path.resize(opath.size());
                copy(opath.begin(), opath.end(), path.begin());
            }
        }

        if (path[0] >= 10) {
            cout << 15 - path[0];
        } else {
            cout << path[0];
        }
        for (int j = 1; j < path.size(); j++) {
            if (path[j] >= 10) {
                cout << ' ' << 15 - path[j];
            } else {
                cout << ' ' << path[j];
            }   
        }
        cout << endl;
    }
    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);
		if(i!=n-1)cout << endl;
	}
	return 0;
}

C++: #include<cstdio>
int n,m,l;void solve(int p,int d){if(p==m){printf("%d\n",p);return;}printf("%d ",p);if(p==9)solve(5,0);else if(!p||d)solve(p+1,1);else solve(p-1,0);}main(){scanf("%d",&l);while(l--){scanf("%d%d",&n,&m);if(n==9||(n<6&&n>m))solve(n,0);else solve(n,1);}}