#include <bits/stdc++.h>
using namespace std;

template<class T>
class vec: public vector<T> {
    public:
    void operator+= (vec<T> &b) {
        this->insert(this->end(), b.begin(), b.end());
    }

    vec<T> operator+ (vec<T> &b) {
        vec<T> c;
        for(T x: *this) c.push_back(x);
        for(T x: b) c.push_back(x);
        return c;
    }

    void operator+= (T &x) {
        for(int i=0; i<this->size(); i++) {
            *this[i]+=x;
        }
    }

    vec<T> operator+ (T &x) {
        vec<T> c;
        for(auto y: *this) {
            c.push_back(x+y);
        }
        return c;
    }

    void operator-= (vec<T> &b) {
        for(auto x: b) {
            this->pop_back();
        }
    }

    void operator- (vec<T> &b) {
        vec<T> a = *this;
        for(auto x: b) {
            a.pop_back();
        }
        return a;
    }
};

#define vector vec

struct rll {
    long long MAX = LLONG_MAX - 7;
    long long val;

    rll(long long x=0): val(x) {}

    rll operator+ (rll b) {
        if(val >= MAX - b.val) {
            return rll(MAX);
        }
        return rll(val+b.val);
    }

    rll operator* (rll b) {
        if(val >= MAX/b.val) {
            return rll(MAX);
        }
        return rll(val*b.val);
    }

    operator long long() {
        return val;
    }

    bool operator== (int x) {return val==x;}
    bool operator== (long long x) {return val==x;}
    bool operator== (rll b) {return val==b.val;}
};

rll dp[55], dpfact[55];

rll fact(long long n) {
    if(dpfact[n]==-1) {
        if(n<=1) return 1;
        dpfact[n] = n*fact(n-1);
    }
    return dpfact[n];
}

rll combinations(long long n) {
    if(n<3) return fact(1);
    return fact(n + (-2) );
}

vector<vector<long long> > allGoodPermutations;
long long rv[55];

void trans(long long num, long long n, int cnt=0) {

    if(cnt==n) {
        vector<long long> goodPermutation;
        for(int i=1; i<=n; i++) goodPermutation.push_back(rv[i]);
        allGoodPermutations.push_back(goodPermutation);
        return;
    }

    if(cnt==0) {
        rv[1] = num;
        trans(1, n, cnt+1);
        rv[1] = 0;
        return;
    }

    if(cnt==n-1) {
        rv[n] = num;
        trans(n, n, cnt+1);
        rv[n] = 0;
        return;
    }

    for(int i=2; i<n; i++) {
        if(rv[i]==0) {
            rv[i] = num;
            trans(i, n, cnt+1);
            rv[i] = 0;
        }
    }
}

rll rec(long long n) {
    if(dp[n]==-1) {
        if(n<=1) {
            dp[n] = 1;
        } else {
            dp[n] = combinations(n);
            for(long long i=1; i<n; i++) {
                dp[n] = dp[n] + combinations(i)*rec(n-i);
            }
        }
    }
    return dp[n];
}

vector<pair<long long, long long> > sol;
void analyze(rll n, rll k) {
    if(k==0) {
        assert(n==0); //no more number left
        return;
    }

    if(n==1 && k==1) {
        sol.push_back({1,1});
        return;
    }
    for(int i=1; i<=n; i++) {
        if(rec(n-i)*combinations(i) < k) {
            k = k - rec(n-i)*combinations(i);
        } else {
            long long restCombinations = rec(n-i);
            long long cycleNumber = (long long) k / restCombinations;
            if((restCombinations*cycleNumber)<k) {
                k = k - restCombinations*cycleNumber;
                cycleNumber++;
            } else {
                k = k - restCombinations*(cycleNumber-1);
            }

            sol.push_back({i, cycleNumber});
            analyze(n-i, k);

            break;
        }
    }
}

long long arr[55], mark[55];
vector<long long> solution;

bool isvalid(int n, int position=1, int cnt=0) {
    if(position==1) {
        if(cnt==n) return true;
        if(cnt!=0) return false;
    }
    if(cnt==n) return false;

    return isvalid(n, arr[position], cnt+1);
}

void kth_trans(long long n, long long k, long long start=1) {
    if(n==1) {
        solution.push_back(start);
        return;
    }
    if(n==2) {
        solution.push_back(start+1);
        solution.push_back(start);
        return;
    }
    if(n==3) {
        solution.push_back(start+2);
        solution.push_back(start);
        solution.push_back(start+1);
        return;
    }

    arr[1] = n;
    mark[n] = 1;
    solution.push_back(n+start-1);

    for(int i=2; i<=n-3; i++) {
        rll left = n-i-1;
        rll possibleCombinationsforEachNumber = fact(left);

        long long whichNumber = k/possibleCombinationsforEachNumber;
        if(whichNumber*possibleCombinationsforEachNumber < k) whichNumber++;

        k = k%possibleCombinationsforEachNumber;
        if(k==0) k = possibleCombinationsforEachNumber;

        for(int j=1; j<=n; j++) {
            if(mark[j]) continue;
            if(i==j) continue;
            whichNumber--;
            if(whichNumber==0) {
                arr[i] = j;
                mark[j] = 1;
                break;
            }
        }
        solution.push_back(start+arr[i]-1);
    }

    vector<long long> unmarked;
    for(int i=1; i<=n; i++) {
        if(mark[i]) continue;
        unmarked.push_back(i);
    }

    for(int i=0; i<3; i++) {
        for(int j=0; j<3; j++) {
            if(i==j) continue;
            for(int k2=0; k2<3; k2++) {
                if(j==k2) continue;
                arr[n-2] = unmarked[i];
                arr[n-1] = unmarked[j];
                arr[n] = unmarked[k2];

                // cout << "bleh" << start << " " <<  k << endl;

                if(isvalid(n)) k--;
                if(k==0) {
                    solution.push_back(start+arr[n-2]-1);
                    solution.push_back(start+arr[n-1]-1);
                    solution.push_back(start+arr[n]-1);
                    return;
                }
            }
        }
    }
}

int main() {
    for(int i=0; i<55; i++) {
        dp[i] = -1;
        dpfact[i] = -1;
    }

    // int n;
    // cin >> n;
    // allGoodPermutations.clear();
    // trans(n, n);
    // sort(allGoodPermutations.begin(), allGoodPermutations.end());

    // for(auto goodPermutation: allGoodPermutations) {
    //     for(auto x: goodPermutation) {
    //         cout << x << " ";
    //     }
    //     cout << endl;
    // }
    // // cout << "Good transformation count: " << rec(n) << endl;
    // cout << "Cycle Count: " << allGoodPermutations.size() << endl;
    // if(!allGoodPermutations.empty()) return 0;

    int t;
    cin >> t;

    while(t--) {
        long long n, k;
        cin >> n >> k;

        long long total_possible = rec(n);
        // cout << total_possible << endl;
        if(total_possible<k) {
            cout << -1 << endl;
            continue;
        }

        sol.clear();
        analyze(n, k);

        // for(auto s: sol) {
        //     cout << s.first << "," << s.second << " ---- ";
        // }
        // cout << endl;

        solution.clear();
        long long start = 1;
        for(auto s: sol) {
            memset(arr, 0, sizeof arr);
            memset(mark, 0, sizeof mark);

            kth_trans(s.first, s.second, start);
            start+=s.first;
        }

        for(auto x: solution) {
            cout << x << " ";
        }
        cout << endl;
    }
    return 0;
}


#include <iostream>
#define ll long long
using namespace std;
const ll MX = 2e18;
ll dp[52][52]={},fkt[51]={},rs[51]={},ac[51]={},us[51]={};
int main()
{
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	fkt[0] = 1;
	for (ll i = 1;i < 20;i++) {fkt[i] = fkt[i-1] * i;}
	for (ll i = 20;i <= 50;i++) {fkt[i] = MX;}
	int t;cin>>t;
	while (t--)
	{
		ll n,k;cin>>n>>k;
		dp[n+1][0] = 1;
		for (int i=0;i<=n;i++)
		{
			rs[i] = 0;ac[i] = 0;us[i] = 0;
			for (int j=0;j<=n;j++)
			{
				dp[i][j] = 0;
			}
		}
		for (int i=n;i>0;i--)
		{
			for (int j=1;j<=n-i+1;j++) // Run DP Prefix Sum, to speed up backtracking
			{
				if ((double)fkt[max(0,j-2)] * dp[i+j][n-(i+j)+1] + dp[i][j-1] >= MX) {dp[i][j] = 2e18;}
				else {dp[i][j] = fkt[max(0,j-2)] * dp[i+j][n-(i+j)+1] + dp[i][j-1];}
			}
		}
		k--;
		if (dp[1][n] > k)
		{
			for (int i=1;i<=n;i++)
			{
				for (int j=1;j<=n-i+1;j++) // Determine which "de-permutation", as the next j numbers would be some derangement of i to i+j-1, in which the rs[i] = i+j-1.
				{
					if (dp[i][j] > k)
					{
						k-=dp[i][j-1];
						rs[i] = i + j - 1;
						us[i+j-1] = i;
						ll tp = k / dp[i+j][n-(i+j)+1];
						for (int kk = j-2; kk > 0; kk--) // Figure out the order, there are (n-2)! possibilities, where n is the size
						{
							int to = 0,ig = i+j-1-kk;
							while (us[ig]) {ig = us[ig];} // Find which number should be ignored, which is the number that can cause loops, which isn't connected to the first loop.
							for (int l = i;l <= i+j-1;l++)
							{
								if (l == ig) {continue;}	// Ignore special number
								to += !us[l];
								if (to > tp / fkt[kk-1])	// Insert which number at the current pos.
								{
									us[l] = i + j - 1 - kk;
									rs[i + j - 1 - kk] = l;
									tp %= fkt[kk-1];
									break;
								}
							}
						}
						for (int kk = i;kk <= i+j-1;kk++)	// Find the number that isn't used, as the last number
						{
							if (!us[kk])
							{
								rs[i+j-1] = kk;
								us[kk]=i+j-1;
								break;
							}
						}
						k%=dp[i+j][n-(i+j)+1];
						i+=j-1;
						break;
					}
				}
			}
			for (int i=1;i<=n;i++)
			{
				cout<<rs[i];
				if (i<n) {cout<<" ";}
			}
			cout<<"\n";
		}
		else {cout<<"-1\n";}

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define long unsigned long long
typedef pair<int,int> ii;

const long BIG = 4e18;
long mul(long a, long b) { return (BIG/a <= b)? BIG : a*b; }
long add(long a, long b) { return min(a+b, BIG); }
long dp[51], fac[51];

void count_dp()
{
	fac[0] = 1;
	fac[1] = 1;
	dp[0] = 1;
	dp[1] = 1;
	for(int i = 2; i <= 50; i++)
	{
		fac[i] = mul(i, fac[i-1]);
		dp[i] = add(dp[i], dp[i-1]);
		for(int j = 0; j <= i-2; j++)
		{
			//printf("%d %d %lld %lld\n", j, i-2-j, fac[j], dp[i-2-j]); fflush(stdout);
			dp[i] = add(dp[i], mul(fac[j], dp[i-2-j]));
		}
	}
	// for(int i = 1; i <= 50; i++) printf("%d %lld\n", i, dp[i]);
}

void print(vector<int> a) { for(int i = 0; i < a.size(); i++) printf("%d%c", a[i], (i == a.size()-1)? '\n' : ' '); }
vector<int> erase(vector<int> &a, int b)
{
	vector<int> ans;
	for(int i = 0; i < a.size(); i++)
		if(i != b)
			ans.push_back(a[i]);
	return ans;
}
vector<int> gabung(vector<int> a, vector<int> b)
{
	// printf("		GABUNG 	: "); print(a);
	// printf("				: "); print(b); fflush(stdout);
	for(int x : b) a.push_back(x);
	return a;
}
vector<int> seq(int n)
{
	vector<int> ans;
	for(int i = 1; i <= n; i++) ans.push_back(i);
	return ans;
}
vector<int> find_perm(int n, long k, vector<int> vec)
{
	// printf("		find_perm : %d %d\n", n, k); fflush(stdout);
	if(n == 0) return {};
	if(n == 1 && k == 1) return vec;
	int num = (k+fac[n-1]-1)/fac[n-1];
	return gabung({vec[num-1]}, find_perm(n-1, k-(num-1)*fac[n-1], erase(vec, num-1)));
}

vector<int> convert(int n, vector<int> vec)
{
	// printf("		convert : %d\n", n);
	// printf("				:"); print(vec); fflush(stdout);
	vector<int> ans = {n+2};
	vector<int> can = seq(n+1);

	struct xxx
	{
		int n;
		vector<int> par;
		vector<bool> used;

		xxx(int n)
		{
			for(int i = 0; i <= n+1; i++)
			{
				used.push_back(0);
				par.push_back(i);
			}
		}
		int parent(int u)
		{
			return par[u] = (u == par[u])? u : parent(par[u]);
		}
		void gab(int u, int v)
		{
			par[u] = parent(par[v]);
		}
	} dsu(n);

	for(int i = 0; i < n; i++)
	{
		int num = 0;
		for(int j = 0; j < can.size(); j++)
		{
			if(dsu.used[can[j]]) continue;
			if(dsu.parent(can[j]) != dsu.parent(i+2))
				num++;

			if(num == vec[i])
			{
				ans.push_back(can[j]);
				dsu.used[can[j]] = 1;
				dsu.gab(i+2, can[j]);
				break;
			}
		}
		for(int j = i; j < n; j++)
			if(vec[j] > vec[i])
				vec[j]--;
	}
	for(auto x : can)
		if(!dsu.used[x])
			ans.push_back(x);
	return ans;
}

vector<int> add(vector<int> vec, int num)
{
	for(auto &x : vec) x += num;
	return vec;
}

vector<int> solve(int n, long k, int buffer)
{
	// printf("	%d %d %d\n", n, k, buffer); fflush(stdout);
	vector<int> ans;
	if(dp[n] < k) return {-1};
	else if(n == 0) return {};
	else if(n == 1) return {1+buffer};
	else
	{
		if(k <= dp[n-1])
			return gabung({1+buffer}, solve(n-1, k, buffer+1));
		else
		{
			k -= dp[n-1];
			for(int i = 0; i <= n-2; i++)
			{
				// printf("	k : %d %d %d\n", k, i, dp[n-2-i]);
				if(k <= mul(fac[i], dp[n-2-i]))
				{
					long ke = (k+dp[n-2-i]-1)/dp[n-2-i];
					// printf("	ke : %d\n", ke); fflush(stdout);
					return gabung(add(convert(i, find_perm(i, ke, seq(i))), buffer), solve(n-i-2, k-(ke-1)*dp[n-2-i], buffer+i+2));
					break;
				}
				else
					k -= mul(fac[i], dp[n-2-i]);
			}
			return {};
		}
	}
}

int main()
{
	// freopen("input.in", "r", stdin);

	count_dp();
	int t; scanf("%d", &t);
	while(t--)
	{
		int n; long k;
		scanf("%d %lld", &n, &k);
		// printf("%d %d\n", n, k);
		print(solve(n, k, 0));
	}
	// for(int i = 1; i <= 24; i++)
	// 	print(convert(4, find_perm(4, i, seq(4))));
	// for(int i = 1; i <= 500; i++)
	// {
	// 	printf("i : %d\n", i);
	// 	print(solve(9, i, 0));
	// }
}


#include<bits/stdc++.h>
using namespace std;

#define fi first
#define se second
#define long unsigned long long
typedef pair<int,int> ii;

const long BIG = 4e18;
long mul(long a, long b) { return (BIG/a <= b)? BIG : a*b; }
long add(long a, long b) { return min(a+b, BIG); }
long dp[51], fac[51];

void count_dp()
{
	fac[0] = 1;
	fac[1] = 1;
	dp[0] = 1;
	dp[1] = 1;
	for(int i = 2; i <= 50; i++)
	{
		fac[i] = mul(i, fac[i-1]);
		dp[i] = add(dp[i], dp[i-1]);
		for(int j = 0; j <= i-2; j++)
		{
			//printf("%d %d %lld %lld\n", j, i-2-j, fac[j], dp[i-2-j]); fflush(stdout);
			dp[i] = add(dp[i], mul(fac[j], dp[i-2-j]));
		}
	}
	// for(int i = 1; i <= 20; i++)
	// 	printf("%d %lld %lld\n", i, lllll.dp[i], fac[i]);
}

void print(vector<int> a) { for(int i = 0; i < a.size(); i++) printf("%d%c", a[i], (i == a.size()-1)? '\n' : ' '); }
vector<int> erase(vector<int> &a, int b)
{
	vector<int> ans;
	for(int i = 0; i < a.size(); i++)
		if(i != b)
			ans.push_back(a[i]);
	return ans;
}
vector<int> gabung(vector<int> a, vector<int> b)
{
	// printf("		GABUNG 	: "); print(a);
	// printf("				: "); print(b); fflush(stdout);
	for(int x : b) a.push_back(x);
	return a;
}
vector<int> seq(int n)
{
	vector<int> ans;
	for(int i = 1; i <= n; i++) ans.push_back(i);
	return ans;
}
vector<int> find_perm(int n, int k, vector<int> vec)
{
	// printf("		find_perm : %d %d\n", n, k); fflush(stdout);
	if(n == 0) return {};
	if(n == 1 && k == 1) return vec;
	int num = (k+fac[n-1]-1)/fac[n-1];
	return gabung({vec[num-1]}, find_perm(n-1, k-(num-1)*fac[n-1], erase(vec, num-1)));
}

vector<int> convert(int n, vector<int> vec)
{
	// printf("		convert : %d\n", n);
	// printf("				:"); print(vec); fflush(stdout);
	vector<int> ans = {n+2};
	vector<int> can = seq(n+1);

	struct xxx
	{
		int n;
		vector<int> par;
		vector<bool> used;

		xxx(int n)
		{
			for(int i = 0; i <= n+1; i++)
			{
				used.push_back(0);
				par.push_back(i);
			}
		}
		int parent(int u)
		{
			return par[u] = (u == par[u])? u : parent(par[u]);
		}
		void gab(int u, int v)
		{
			par[u] = parent(par[v]);
		}
	} dsu(n);

	for(int i = 0; i < n; i++)
	{
		// printf("%d %u\n", i, vec.size()); fflush(stdout);
		// printf("%d %d\n", i+2, vec[i]);
		// printf("CAN : "); print(can);
		// printf("VEC : "); for(int j  = i; j < n; j++) printf("%d ", vec[j]); printf("\n");
		// fflush(stdout);

		int num = 0;
		for(int j = 0; j < can.size(); j++)
		{
			// printf("%d %d %d\n", can[j], i+2, num); fflush(stdout);
			if(dsu.used[can[j]]) continue;
			if(dsu.parent(can[j]) != dsu.parent(i+2))
				num++;

			// printf("%d\n", i); fflush(stdout);
			// printf("%d %d\n", num, vec[i]); fflush(stdout);
			if(num == vec[i])
			{
				// printf("J 	: %d\n", can[j]); fflush(stdout);
				ans.push_back(can[j]);
				dsu.used[can[j]] = 1;
				dsu.gab(i+2, can[j]);
				break;
			}
		}
		for(int j = i; j < n; j++)
			if(vec[j] > vec[i])
				vec[j]--;
	}
	for(auto x : can)
		if(!dsu.used[x])
			ans.push_back(x);
	return ans;
}

vector<int> add(vector<int> vec, int num)
{
	for(auto &x : vec) x += num;
	return vec;
}

vector<int> solve(int n, int k, int buffer)
{
	// printf("	%d %d %d\n", n, k, buffer); fflush(stdout);
	vector<int> ans;
	if(dp[n] < k) return {-1};
	else if(n == 0) return {};
	else if(n == 1) return {1+buffer};
	else
	{
		if(k <= dp[n-1])
			return gabung({1+buffer}, solve(n-1, k, buffer+1));
		else
		{
			k -= dp[n-1];
			for(int i = 0; i <= n-2; i++)
			{
				// printf("	k : %d %d %d\n", k, i, dp[n-2-i]);
				if(k <= mul(fac[i], dp[n-2-i]))
				{
					int ke = (k+dp[n-2-i]-1)/dp[n-2-i];
					// printf("	ke : %d\n", ke); fflush(stdout);
					return gabung(add(convert(i, find_perm(i, ke, seq(i))), buffer), solve(n-i-2, k-(ke-1)*dp[n-2-i], buffer+i+2));
					break;
				}
				else
					k -= mul(fac[i], dp[n-2-i]);
			}
			return {};
		}
	}
}

int main()
{
	// freopen("input.in", "r", stdin);

	count_dp();
	int t; scanf("%d", &t);
	while(t--)
	{
		int n; long k;
		scanf("%d %lld", &n, &k);
		// printf("%d %d\n", n, k);
		print(solve(n, k, 0));
	}
	// for(int i = 1; i <= 24; i++)
	// 	print(convert(4, find_perm(4, i, seq(4))));
}


//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")

#include <iostream>
#include <stdlib.h>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <assert.h>
#include <memory.h>
#include <time.h>
#include <bitset>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = (l); i < (r); i++)
#define repb(i, r, l) for (int i = (r); i > (l); i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk
#define prev fhsgfhjf
#define plus fsghsf
#define minus ytryr

using namespace std;

inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }

const int N = 50;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const ld eps = 1e-12;
const double pi = atan2(0, -1);
const int ABC = 26;

ll dp[N + 1];
ll fact[N + 1];

ll add(ll x, ll y) {
    if (x + y <= big) {
        return x + y;
    } else {
        return big;
    }
}

ll mul(ll x, ll y) {
    if (y == 0) {
        return 0;
    }
    if (x <= big / y) {
        return x * y;
    } else {
        return big;
    }
}

bool check(int x, int *to) {
    int y = x;
    while (true) {
        if (to[y] == -1) {
            return true;
        } else if (to[y] == x) {
            return false;
        }
        y = to[y];
    }
}

vector<int> restore_perm(int n, ll k) {
    //cout << n << " " << k << endl;
    vector<int> ans;
    bool used[n]; fill(used, used + n, false);
    int to[n]; fill(to, to + n, -1); to[0] = n - 1;
    ans.push_back(n - 1);
    rep(i, 1, n) {
        if (i == n - 1) {
            rep(j, 0, n) {
                if (!used[j]) {
                    used[j] = true;
                    to[i] = j;
                    ans.push_back(j);
                    break;
                }
            }
            return ans;
        }
        ll v = (n - i - 2 >= 0 ? fact[n - i - 2] : 1);
        rep(j, 0, n) {
            if (!used[j]) {
                to[i] = j;
                if (!check(i, to)) {
                    to[i] = -1;
                    continue;
                }
                if (v <= k) {
                    to[i] = -1;
                    k -= v;
                    continue;
                }
                used[j] = true;
                ans.push_back(j);
                break;
            }
        }
    }
    return ans;
}

int main()
{
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.precision(20);
    //ll TL = 10.95 * CLOCKS_PER_SEC;
    //clock_t time = clock();
    fact[0] = 1;
    rep(i, 1, N + 1) {
        fact[i] = mul(fact[i - 1], i);
    }
    dp[0] = 1;
    rep(i, 1, N + 1) {
        rep(j, 1, i + 1) {
            dp[i] = add(dp[i], mul(dp[i - j], (j == 1 ? 1 : fact[j - 2])));
        }
    }
    /*rep(i, 0, 4 + 1) {
        cout << dp[i] << " ";
    }
    cout << endl;*/
    int T;
    cin >> T;
    while (T--) {
        int n; ll k;
        cin >> n >> k; k--;
        if (k >= dp[n]) {
            cout << -1 << "\n";
            continue;
        }
        vector<int> ans;
        int p = 0;
        while (p < n) {
            rep(j, 1, n + 1) {
                ll v = (j == 1 ? 1 : fact[j - 2]);
                ll val = mul(v, dp[n - p - j]);
                if (val <= k) {
                    k -= val;
                } else {
                    vector<int> aaa = restore_perm(j, k / dp[n - p - j]);
                    for (int x : aaa) {
                        ans.push_back((p + 1) + x);
                    }
                    k %= dp[n - p - j];
                    p += j;
                    break;
                }
            }
        }
        for (int i : ans) {
            cout << i << " ";
        }
        cout << "\n";
    }
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 100;
long long fact[MAXN];
long long dp[MAXN][MAXN];
long long tot[MAXN];
long long ans[MAXN];
bool blocked[MAXN];
long long add(long long x,long long y){
    x+=y;
    if(x>(1e18+5)){
        return 1e18+5;
    }
    return x;
}
long long mult(long long x,long long y){
    if(x<=(long long)(1e18+5)/y){
        return 1LL*x*y;
    }
    return 1e18+5;
}
long long calcfact(long long x){
    if(x-2<0){
        return 1;
    }
    return fact[x-2];
}
int main() {
    long long t;
    cin>>t;
    fact[0] = 1;
    for(long long i=1;i<=50;i++){
        fact[i] = mult(fact[i-1],i);
    }
    while(t--){
        long long n,k;
        cin>>n>>k;
        for(long long i=1;i<=50;i++){
            ans[i] = 0;
            for(long long j=1;j<=50;j++){
                dp[i][j] = 0;
                tot[i] = 0;
            }
        }
        tot[n+1] = 1;
        for(long long i=n;i>=1;i--){
            blocked[i] = false;
            for(long long j=i;j<=n;j++){
                if(j-i-1>=0){
                    dp[i][j] = mult(tot[j+1],fact[(j-i-1)]);
                }else{
                    dp[i][j] = tot[j+1];
                }
                tot[i] = add(tot[i],dp[i][j]);
            }

        }

        if(tot[1]<k){
            cout<<-1<<endl;
            continue;
        }

        for(long long i=1;i<=n;i++){
            long long len = 1;
            if(tot[i+len]<k){
                k-=tot[i+len];
                len++;
            }

            while(len-2>=0 && mult(fact[len-2],tot[i+len])<k){
                k-=mult(fact[len-2],tot[i+len]);
                len++;

            }
            ans[i] = i+len-1;
            blocked[i+len-1] = true;
            for(long long j=i+1;j<=i+len-1;j++){

                long long v= i;
                while(true){

                    if(blocked[v]){
                        v++;
                        continue;
                    }

                    if(j == i+len-1){

                        break;
                    }
                    long long t=v;
                    while(ans[t]){
                        t = ans[t];
                    }
                    if(t==j){
                        v++;
                        continue;
                    }
                    if(k>mult(tot[i+len],calcfact(len-(j-i)))){
                        k-=mult(tot[i+len],calcfact(len-(j-i)));
                        v++;
                        continue;
                    }
                    break;
                }
                blocked[v] = true;
                ans[j] = v;

            }
            i+=len;
            i--;
        }
        for(long long i=1;i<=n;i++){
            cout<<ans[i]<<" ";
        }
        cout<<endl;
    }
}


#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
#include <iomanip>
#include <set>
#include <tuple>
#define mkp make_pair
#define mkt make_tuple
#define rep(i,n) for(int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
const ll MOD=1e9+7;

const ll INF=1e18+128;

void add(ll &a,ll b){
    a=min(INF,a+b);
}

void mul(ll &a,ll b){
    if(INF/a<b||INF/b<a) a=INF;
    else a=min(INF,a*b);
}

ll dp[55];
ll fac[55];

vector<int> getCycle(int n,ll k){
    vector<int> res(n,-1);
    res[0]=n-1;
    vector<int> used(n,0);
    used[n-1]=1;
    for(int i=1;i<n;i++){
        for(int j=0;j<n;j++){
            if(used[j]) continue;
            if(i==n-1){
                used[j]=1;
                res[i]=j;
                break;
            }

            // check no cycle
            int pos=j;
            while(pos<i) pos=res[pos];
            if(pos==i) continue;// ng

            ll num=fac[n-i-2];
            if(num<k){
                k-=num;
                continue;
            }
            res[i]=j;
            used[j]=1;
            break;
        }
    }
    return res;
}

vector<int> rec(int n,ll k){
    vector<int> res(n,0);
    if(n==0) return res;
    for(int i=0;i<n;i++){
        ll num=dp[n-(i+1)];
        mul(num,(i==0?1:fac[i-1]));
        if(num<k){
            k-=num;
            continue;
        }

        vector<int> cyc=getCycle(i+1,((k-1)/dp[n-(i+1)])+1);
        for(int j=0;j<i+1;j++) res[j]=cyc[j];
        cyc=rec(n-(i+1),((k-1)%dp[n-(i+1)])+1);
        for(int j=0;j<cyc.size();j++) res[i+1+j]=cyc[j]+(i+1);
        //for(auto r:res) cout<<n<<" "<<k<<" "<<r<<endl;

        return res;
    }
}

void solve(){
  int N;
  ll K;
  cin>>N>>K;

  if(dp[N]<K){
      cout<<-1<<endl;
      return;
  }

  vector<int> ans=rec(N,K);
  rep(i,ans.size()) cout<<ans[i]+1<<" ";
  cout<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  fac[0]=1;
  for(ll i=1;i<=50;i++){
      ll coef=fac[i-1];
      mul(coef,i);
      add(fac[i],coef);
  }

  dp[0]=1;
  for(int i=1;i<=50;i++){
    for(int j=1;j<=i;j++){
      if(j==1) add(dp[i],dp[i-1]);
      else{
          ll coef=dp[i-j];
          mul(coef,fac[j-2]);
          add(dp[i],coef);
      }
    }
    //cout<<i<<" "<<dp[i]<<endl;
  }

  int T;
  cin>>T;
  rep(i,T) solve();

  return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];
int tar[52],vis[52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			FOR(i,N+1) tar[i]=i,vis[i]=0;
			i=1;
			vector<int> V;

			while(i<=N) {
				for(j=i;j<=N;j++) {
					//cout<<i<<j<<" "<<dp[N][j+1]<<" "<<dp[N][j+1]*cycle[j-i+1]<<" "<<K<<endl;
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						V.push_back(j);
						swap(tar[j],tar[V.size()]);
						vis[j]=1;
						while(V.size()<j-1) {
							for(x=i;x<=j;x++) if(vis[x]==0 && tar[x]!=(int)V.size()+1) {
								ll cand=fact[max(2,j-(int)V.size())-2];
								//cout<<x<<"!"<<num<<" "<<cand<<endl;
								if(cand<=num) {
									num-=cand;
								}
								else {
									V.push_back(x);
									int a=tar[V.size()];
									int b=tar[x];
									tar[b]=a;
									tar[a]=b;
									vis[x]=1;
									break;
								}
							}
						}
						for(x=i;x<=j;x++) if(vis[x]==0) V.push_back(x);
						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i-1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			i=1;
			vector<int> V;
			while(i<=N) {
				for(j=i;j<=N;j++) {
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						vector<int> cand;
						V.push_back(j);
						for(x=i;x<j;x++) cand.push_back(x);
						while(cand.size()) {
							int id=num/fact[cand.size()-1];
							num%=fact[cand.size()-1];
							V.push_back(cand[id]);
							cand.erase(cand.begin()+id);
						}

						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];
int tar[52],vis[52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			FOR(i,N+1) tar[i]=i,vis[i]=0;
			i=1;
			vector<int> V;

			while(i<=N) {
				for(j=i;j<=N;j++) {
					//cout<<i<<j<<" "<<dp[N][j+1]<<" "<<dp[N][j+1]*cycle[j-i+1]<<" "<<K<<endl;
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						V.push_back(j);
						tar[V.size()]=j;
						vis[j]=1;
						while(V.size()<j-1) {
							for(x=i;x<=j;x++) if(vis[x]==0 && tar[x]!=(int)V.size()+1) {
								ll cand=fact[max(2,j-(int)V.size())-2];
								//cout<<x<<"!"<<num<<" "<<cand<<endl;
								if(cand<=num) {
									num-=cand;
								}
								else {
									V.push_back(x);
									swap(tar[V.size()],tar[x]);
									vis[x]=1;
									break;
								}
							}
						}
						for(x=i;x<=j;x++) if(vis[x]==0) V.push_back(x);
						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			i=1;
			vector<int> V;
			while(i<=N) {
				for(j=i;j<=N;j++) {
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						vector<int> cand;
						V.push_back(j);
						for(x=i;x<j;x++) cand.push_back(x);
						while(cand.size()) {
							int id=num/fact[max(2,(int)cand.size())-2];
							num%=fact[max(2,(int)cand.size())-2];
							V.push_back(cand[id]);
							cand.erase(cand.begin()+id);
						}

						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];
int tar[52],vis[52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			FOR(i,N+1) tar[i]=i,vis[i]=0;
			i=1;
			vector<int> V;

			while(i<=N) {
				for(j=i;j<=N;j++) {
					//cout<<i<<j<<" "<<dp[N][j+1]<<" "<<dp[N][j+1]*cycle[j-i+1]<<" "<<K<<endl;
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						V.push_back(j);
						swap(tar[j],tar[V.size()]);
						vis[j]=1;
						while(V.size()<j-1) {
							for(x=i;x<=j;x++) if(vis[x]==0 && tar[x]!=(int)V.size()+1) {
								ll cand=fact[max(2,j-(int)V.size())-2];
								//cout<<x<<"!"<<num<<" "<<cand<<endl;
								if(cand<=num) {
									num-=cand;
								}
								else {
									V.push_back(x);
									int a=tar[V.size()];
									int b=tar[x];
									tar[b]=a;
									tar[a]=b;
									vis[x]=1;
									break;
								}
							}
						}
						for(x=i;x<=j;x++) if(vis[x]==0) V.push_back(x);
						i=j+1;
						break;
					}
				}
			}
			cout<<"!!!!"<<T<<endl;
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];
int tar[52],vis[52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			FOR(i,N+1) tar[i]=-1,vis[i]=0;
			i=1;
			vector<int> V;

			while(i<=N) {
				for(j=i;j<=N;j++) {
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						V.push_back(j);
						tar[V.size()]=j;
						vis[j]=1;
						while(V.size()<j-1) {
							for(x=i;x<=j;x++) if(vis[x]==0 && tar[x]!=(int)V.size()+1) {
								ll cand=fact[max(2,j-(int)V.size()+1)-2];
								if(K<=cand) {
									V.push_back(x);
									swap(tar[V.size()],tar[x]);
									vis[x]=1;
								}
								else {
									K-=cand;
								}
							}
						}
						for(x=i;x<=j;x++) if(vis[x]==0) V.push_back(x);
						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			i=1;
			vector<int> V;
			while(i<=N) {
				for(j=i;j<=N;j++) {
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						vector<int> cand;
						V.push_back(j);
						for(x=i;x<j;x++) cand.push_back(x);
						while(cand.size()) {
							int id=num/fact[cand.size()-1];
							num%=fact[cand.size()-1];
							V.push_back(cand[id]);
							cand.erase(cand.begin()+id);
						}

						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef signed long long ll;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
#define FOR(x,to) for(x=0;x<(to);x++)
#define FORR(x,arr) for(auto& x:arr)
#define ITR(x,c) for(__typeof(c.begin()) x=c.begin();x!=c.end();x++)
#define ALL(a) (a.begin()),(a.end())
#define ZERO(a) memset(a,0,sizeof(a))
#define MINUS(a) memset(a,0xff,sizeof(a))
//-------------------------------------------------------

int T;
int N;
ll K;
ll fact[1010],cycle[1010];;
ll dp[52][52];
int tar[52],vis[52];

void solve() {
	int i,j,k,l,r,x,y; string s;

	fact[0]=1;
	for(i=1;i<=1000;i++) {
		if((double)fact[i-1]*i>1e18) fact[i]=5+1e18;
		else fact[i]=fact[i-1]*i;
	}
	FOR(i,51) cycle[i]=(i-2)>=0?fact[i-2]:1;

	for(x=1;x<=50;x++) {
		dp[x][x+1]=1;
		for(i=x;i>=1;i--) {
			for(j=i;j<=x;j++) {
				double add=(double)dp[x][j+1]*cycle[j-i+1];
				if(add+dp[x][i]>1e18) {
					dp[x][i]=5+1e18;
				}
				else {
					dp[x][i]+=dp[x][j+1]*cycle[j-i+1];
				}
			}
		}
	}

	cin>>T;
	while(T--) {
		cin>>N>>K;

		if(dp[N][1]<K) {
			cout<<-1<<endl;
		}
		else {
			K--;
			FOR(i,N+1) tar[i]=i,vis[i]=0;
			i=1;
			vector<int> V;

			while(i<=N) {
				for(j=i;j<=N;j++) {
					//cout<<i<<j<<" "<<dp[N][j+1]<<" "<<dp[N][j+1]*cycle[j-i+1]<<" "<<K<<endl;
					if(dp[N][j+1]*cycle[j-i+1]<=K) {
						K-=dp[N][j+1]*cycle[j-i+1];
					}
					else {
						ll num=K/dp[N][j+1];
						K%=dp[N][j+1];
						V.push_back(j);
						swap(tar[j],tar[V.size()]);
						vis[j]=1;
						while(V.size()<j-1) {
							for(x=i;x<=j;x++) if(vis[x]==0 && tar[x]!=(int)V.size()+1) {
								ll cand=fact[max(2,j-(int)V.size())-2];
								//cout<<x<<"!"<<num<<" "<<cand<<endl;
								if(cand<=num) {
									num-=cand;
								}
								else {
									V.push_back(x);
									swap(tar[V.size()],tar[x]);
									vis[x]=1;
									break;
								}
							}
						}
						for(x=i;x<=j;x++) if(vis[x]==0) V.push_back(x);
						i=j+1;
						break;
					}
				}
			}
			FORR(v,V) cout<<v<<" ";
			cout<<endl;
		}

	}
}

int main(int argc,char** argv){
	string s;int i;
	if(argc==1) ios::sync_with_stdio(false), cin.tie(0);
	FOR(i,argc-1) s+=argv[i+1],s+='\n'; FOR(i,s.size()) ungetc(s[s.size()-1-i],stdin);
	cout.tie(0); solve(); return 0;
}


#include <bits/stdc++.h>
#ifdef TOPOLOGY
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
using namespace std;

long long fact[55];
long long dp[55];

long long safe_mul(long long a, long long b) {
	if (a == -1LL || b == -1LL) return -1LL;
	double d = 1.0L * a * b;
	if (d >= 2e18) return -1LL;
	long long dd = a * b;
	if (dd > 1000000000000000000LL) return -1LL;
	return dd;
}

void safe_add(long long &a, long long b) {
	if (a == -1LL || b == -1LL) a = -1LL;
	else {
		a += b;
		if (a > 1000000000000000000LL) a = -1LL;
	}
}

vector<int> getidx(int n, long long k) {
	vector<int> res;
	for (int i = 0; i < n; i++) {
		long long unit = fact[n - i - 1];
		if (unit == -1LL) {
			res.push_back(0);
			continue;
		}
		long long q = (k - 1) / unit;
		long long r = (k - 1) % unit;
		k = r + 1;
		res.push_back(q);
	}
	return res;
}

vector<int> solve(int n, long long k) {
	if (n == 0) return vector<int>();
	if (dp[n] != -1LL && dp[n] < k) {
		return vector<int>();
	}
	int first = -1;
	for (int i = 1; i <= n; i++) {
		long long tmp = safe_mul(i == 1 ? 1LL : fact[i - 2], dp[n - i]);
		if (tmp == -1LL || tmp >= k) {
			first = i;
			break;
		} else k -= tmp;
	}
	long long unit = dp[n - first];
	vector<int> ans;
	ans.push_back(first);
	if (unit == -1LL) {
		vector<int> bk = solve(n - first, k);
		for (int i = 1; i < first; i++) {
			ans.push_back(i);
		}
		for (auto &c : bk) {
			ans.push_back(c + first);
		}
		return ans;
	}
	long long q = (k - 1) / unit;
	long long r = (k - 1) % unit;
	vector<int> bk = solve(n - first, r + 1);
	if (first == 1) {
		for (auto &c : bk) {
			ans.push_back(c + 1);
		}
		return ans;
	} else if (first == 2) {
		ans.push_back(1);
		for (auto &c : bk) {
			ans.push_back(c + 2);
		}
		return ans;
	}
	vector<int> vi = getidx(first - 2, q + 1);
	set<int> cand;
	for (int i = 1; i < first; i++) {
		cand.insert(i);
	}
	vector<int> nxt(first + 3);
	nxt[1] = first;
	for (int i = 2; i < first; i++) {
		vector<int> nc;
		for (auto &c : cand) {
			nxt[i] = c;
			int cur = i;
			do {
				cur = nxt[cur];
			} while (cur != 0 && cur != i);
			nxt[i] = 0;
			if (cur == 0) nc.push_back(c);
		}
		// assert((int)nc.size() == first - i);
		nxt[i] = nc[vi[i - 2]];
		ans.push_back(nc[vi[i - 2]]);
		cand.erase(nc[vi[i - 2]]);
	}
	ans.push_back(*cand.begin());
	for (auto &c : bk) {
		ans.push_back(c + first);
	}
	return ans;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	int t;
	cin >> t;
	fact[0] = 1;
	for (int i = 1; i <= 50; i++) {
		fact[i] = safe_mul(fact[i - 1], i);
	}
	dp[1] = dp[0] = 1LL;
	for (int i = 2; i <= 50; i++) {
		for (int j = 1; j <= i; j++) {
			long long lhs = j == 1 ? 1LL : fact[j - 2];
			long long rhs = dp[i - j];
			safe_add(dp[i], safe_mul(lhs, rhs));
		}
	}
	while (t--) {
		int n;
		long long k;
		cin >> n >> k;
		vector<int> ans = solve(n, k);
		if (ans.empty()) {
			cout << "-1\n";
			continue;
		}
		for (int i = 0; i < n; i++) {
			if (i) cout << ' ';
			cout << ans[i];
		}
		cout << '\n';
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[200020];
int main()
{
    int n;
    ll sum,val;
    while(~scanf("%d%I64d",&n,&val))
    {
        sum=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%I64d",&a[i]);
            sum+=a[i];
        }
        ll minn,maxn;
        minn=n-1;
        for(int i=1;i<=n;i++)
        {
            maxn=sum-a[i];
            ll l=val-maxn;
            ll r=val-minn;
            ll ans=0;
            ans+=(l>1?l-1:0);
            ans+=(a[i]-r>0?a[i]-r:0);
            printf("%I64d%c",ans,i==n?'\n':' ');
        }
    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, v[N], an[N];
ll a, s;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d%lld", &n, &a);
    set<int>x;
    for(int i = 0; i<n; ++i){
        scanf("%d", v+i);
        x.insert(v[i]);
        s += v[i];
    }
    for(int i = 1; i<=*(--x.end()); ++i){
        int ad = i > a? 1: 0;
        int rem = a - i;
        auto it = x.lower_bound(i);
        s -= *it;
        if(rem > 0){
            if(rem < n-1 || s < rem)ad = 1;
        }
        if(rem == 0 && n > 1)ad = 1;
        s += *it;
        an[i] = an[i-1] + ad;
    }
    for(int i = 0; i<n; ++i){
        printf("%d ", an[v[i]]);
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, v[N];
ll a, s;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d%lld", &n, &a);
    set<int>x;
    for(int i = 0; i<n; ++i){
        scanf("%d", v+i);
        x.insert(v[i]);
        s += v[i];
    }
    for(int i = 0; i<n; ++i){
        s -= v[i];
        int l = 1, r = v[i], md, lf = 0, lr = 0;
        lr = lf;
        l = 1, r = v[i];
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a && md + (n-1) <= a){l = md+1, lr = md;}
            else r = md-1;
        }
        lf = lr;
        l = 1, r = lr;
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a && md + (n-1) <= a){r = md-1, lf = md;}
            else l = md+1;
        }
        printf("%d ", v[i] - (lr - lf + 1));
        s += v[i];
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, v[N];
ll a, s;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d%lld", &n, &a);
    set<int>x;
    for(int i = 0; i<n; ++i){
        scanf("%d", v+i);
        x.insert(v[i]);
        s += v[i];
    }
    for(int i = 0; i<n; ++i){
        s -= v[i];
        int l = 1, r = v[i], md, lf = 0, lr = 0;
        lr = lf;
        l = 1, r = v[i];
        while(l <= r){
            md = (l+r)/2;
            if(md + s >= a && md + (n-1) <= a){l = md+1; lr = md;}
            else {
                if(md + s < a)l = md+1;
                else r = md-1;
            }
        }
        lf = lr;
        l = 1, r = lr;
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a && md + (n-1) <= a){r = md-1, lf = md;}
            else {
                if(md + s < a)l = md+1;
                else r = md-1;
            }
        }
        printf("%d ", v[i] - (lr - lf + 1));
        s += v[i];
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, v[N];
ll a, s;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d%lld", &n, &a);
    set<int>x;
    for(int i = 0; i<n; ++i){
        scanf("%d", v+i);
        x.insert(v[i]);
        s += v[i];
    }
    for(int i = 0; i<n; ++i){
        s -= v[i];
        int l = 1, r = v[i], md, lf = 1, lr = 1;
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a && md + (n-1) <= a)r = md-1, lf = md;
            else l = md+1;
        }
        lr = lf;
        l = 1, r = v[i];
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a && md + (n-1) <= a)l = md+1, lr = md;
            else r = md-1;
        }
        printf("%d ", v[i] - (lr - lf + 1));
        s += v[i];
    }

}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, v[N];
ll a, s;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d%lld", &n, &a);
    for(int i = 0; i<n; ++i){
        scanf("%d", v+i);
        s += v[i];
    }
    for(int i = 0; i<n; ++i){
        int l = 0, r = v[i], md, an = 0;
        while(l <= r){
            md = (l+r)/2;
            if(s - md >= a)l = md+1;
            else r = md-1, an = md;
        }
        printf("%d ", an);
    }
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) x.begin(),x.end()

int const N = 200001;
int n, v[N], an[N];
ll a, s;

int main(){
    #ifndef ONLINE_JUDGE
    freopen("read.txt","r",stdin);
    #endif
    scanf("%d%lld", &n, &a);
    set<int>x;
    for(int i = 0; i<n; ++i){
        scanf("%d", v+i);
        x.insert(v[i]);
        s += v[i];
    }
    for(int i = 0; i<n; ++i){
        s -= v[i];
        int l = 1, r = v[i], md, ad = 0;
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a)r = md-1;
            else l = md+1, ad = md;
        }
        l = 1, r = v[i], md;
        int ad0 = ad;
        while(l <= r){
            md = (l+r)/2;
            if(md+s >= a && md+(n-1)+s <= a)l = md+1, ad0 = md;
            else r = md-1;
        }
        s += v[i];
        int add = a - (n - 1), ann = 0;
        if(ad || ad0)ann += (ad0 - ad + 1);
        if(v[i] > add)ann += (v[i] - add);
        printf("%d ", ann);
    }

}


#include<bits/stdc++.h>
using namespace std;

#define pb 		push_back
#define mp 		make_pair
#define prior   priority_queue
#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define PI 		3.1415926535897932384626433832795
#define ll      long long
#define ld		long double
#define ret     return
#define NUM 	1000001
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

ll d[200011];

void solveTest(){
	ll n,a;
	cin>>n>>a;
	for(int i=0;i<n;i++) cin>>d[i];
	ll sum = accumulate(d,d+n,0LL);
	for(int i=0;i<n;i++){
		ll l = max(1LL,a-sum+d[i]);
		ll r = min(d[i],a-n+1);
		cout<<d[i]-(l<=r? r-l+1:0)<<" ";
	}
    return ;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define pb 		push_back
#define mp 		make_pair
#define prior   priority_queue
#define MOD 	1000000007
#define INF64 	(long long)1e18
#define INF 	(int)1e9
#define PI 		3.1415926535897932384626433832795
#define ll      long long
#define ld		long double
#define ret     return
#define NUM 	1000001
const int SIZE = 2e5+5;
const int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1};
void fastio(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
}

ll d[200011];

void solveTest(){
	ll n,a,dsum=0;
	cin>>n>>a;
	for(int i=0;i<n;i++) {
		cin>>d[i];
		dsum += d[i];
	}
	for(int i=0;i<n;i++){
		ll cant_up = max(0LL, d[i]-(a-(n-1)));
		ll others_sum = dsum-d[i];
		ll cant_low = a-min(others_sum,a-1)-1;
		cout<<cant_low+cant_up<<" ";
	}
    return ;

}

int main(){
	fastio();

	int t=1;
	//cin>>t;
	while(t--){
		solveTest();

	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0,x,y;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (int i = 0; i < n; ++i)
	{
		x=a-s+d[i]-1;y=d[i]-a+n-1;
		if (x>=0&&y>=0)
			cout<<x+y<<" ";
		else if (x<0&&y<0)
			cout<<0<<" ";
		else
			cout<<max(x,y)<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else{
	for (int i = 0; i < n; ++i)
	{if (d[i]>=a)
		cout<<d[i]-1<<" ";

		else if (a-s+d[i]-1>0)
		cout<<a-s+d[i]-1<<" ";
		else
			cout<<0<<" ";
	}}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else{
	for (int i = 0; i < n; ++i)
	{
		if (d[i]>=a)
			cout<<a-n+2<<" ";
		else if (a-s+d[i]-1>0)
		cout<<a-s+d[i]-1<<" ";
		else
			cout<<0<<" ";
	}}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (int i = 0; i < n; ++i)
		if (a-s+d[i]-1)
			cout<<a-s+d[i]-1<<" ";
		else
		cout<<d[i]-a+n-1<<" ";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (int i = 0; i < n; ++i)
		if (max(a-s+d[i]-1,d[i]-a+n-1)>0)
		cout<<max(a-s+d[i]-1,d[i]-a+n-1)<<" ";
		else
			cout<<0<<" ";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (int i = 0; i < n; ++i)
		cout<<max(a-s+d[i]-1,d[i]-a+n-1)<<" ";
}


#include<bits/stdc++.h>
using namespace std;
int  main()
{
	long long n,a,d[200001],s=0,x,y;
	cin>>n>>a;
	for (long long  i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (long long  i = 0; i < n; ++i)
	{
		x=a-s+d[i]-1;y=d[i]-a+n-1;
		if (x>=0&&y>=0)
			cout<<x+y<<" ";
		else if (x<0&&y<0)
			cout<<0<<" ";
		else
			cout<<max(x,y)<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else{
	for (int i = 0; i < n; ++i)
	{if (d[i]>=a)
		cout<<n-1<<" ";

		else if (a-s+d[i]-1>0)
		cout<<a-s+d[i]-1<<" ";
		else
			cout<<0<<" ";
	}}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0,m1=0,m2=0;
	cin>>n>>a;

	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	for (int i = 0; i < n; ++i)
	{
		cout<<max(a-s+d[i]-1,d[i]-a+n-1)<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
int  main()
{
	long n,a,d[200001],s=0,x,y;
	cin>>n>>a;
	for (long  i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (long  i = 0; i < n; ++i)
	{
		x=a-s+d[i]-1;y=d[i]-a+n-1;
		if (x>=0&&y>=0)
			cout<<x+y<<" ";
		else if (x<0&&y<0)
			cout<<0<<" ";
		else
			cout<<max(x,y)<<" ";
	}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else{
	for (int i = 0; i < n; ++i)
	{
		if (d[i]>=a)
			cout<<d[i]-a+n-1<<" ";
		else if (a-s+d[i]-1>0)
		cout<<a-s+d[i]-1<<" ";
		else
			cout<<0<<" ";
	}}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
	long n,a,d[200001],s=0;
	cin>>n>>a;
	for (int i = 0; i < n; ++i)
	{
		cin>>d[i];
		s+=d[i];
	}
	if (n==1)
		cout<<s-1;
	else
	for (int i = 0; i < n; ++i)
		if (max(a-s+d[i]-1,d[i]-a+n-1)>0)
		cout<<max(a-s+d[i]-1,d[i]-a+n)<<" ";
		else
			cout<<0<<" ";
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,s=0,num[200000];
    cin>>n>>a;
    for(int i=0;i<n;i++){cin>>num[i]; s+=num[i];}
    for(int i=0;i<n;i++)
    {
        int M=s-num[i],m=n-1,cnt=0;
        int x=a-m,y=a-M;
        if(x<num[i])cnt+=num[i]-x;
        if(y>1)cnt+=y-1;
        cout<<cnt<<" ";
    }
    return 0;
}


#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    ll n,a,s=0,num[200000];
    cin>>n>>a;
    for(ll i=0;i<n;i++){cin>>num[i]; s+=num[i];}
    for(ll i=0;i<n;i++)
    {
        ll M=s-num[i],m=n-1,cnt=0;
        ll x=a-m,y=a-M;
        if(x<num[i])cnt+=num[i]-x;
        if(y>1)cnt+=y-1;
        cout<<cnt<<" ";
    }
    return 0;
}


#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define vii vector<pii>
#define all(a) (a).begin(),(a).end()
#define X first
#define Y second
using namespace std;
const int N=2e5+5;
int d[N];
void solve()
{
    int n,A; cin>>n>>A;
    int s=0;
    for(int i=0;i<n;i++)
    {
        cin>>d[i];
        s+=d[i];
    }
    for(int i=0;i<n;i++)
    {
        int m=n-1,M=s-d[i];
        int t1=max(A-M,1LL);
        int t2=min(A-m,d[i]);
        cout<<d[i]+t1-t2-1<<" ";
    }
    return;
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int t; t=1;
    while(t--)solve();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,a,s=0,num[200000];
    cin>>n>>a;
    for(int i=0;i<n;i++){cin>>num[i]; s+=num[i];}
    for(int i=0;i<n;i++)
    {
        int M=s-num[i],m=n-1,cnt=0;
        for(int j=1;j<=num[i];j++){if(a-j>=m&&a-j<=M)continue; cnt++;}
        cout<<cnt<<" ";
    }
    return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define sz(v)				((int)((v).size()))
#define clr(v, d)			memset(v, d, sizeof(v))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define pb					push_back
#define MP					make_pair
#define P(x)				cout<<#x<<" = { "<<x<<" }\n"
typedef long long         ll;
typedef long double   	  ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>		  vll;

const ll OO = 1e16;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

void fast(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
}
int n;
ll A;
int a[(int)2e5 + 2];
int main(){
	fast();
	ll sum = 0;
	cin >> n >> A;
	lp(i, n){
		cin >> a[i];
		sum += a[i];
	}
	if (n == 1){
		cout << a[0] - 1 << endl;
		return 0;
	}
	if (sum == A){
		lp(i, n){
			cout << a[i]-1 << " ";
		}
		return 0;
	}
	lp(i, n){
		int f = 0;
		if (a[i] >= A){
			f = a[i] - A + 1;
			cout << f << " ";
		}
		else
			cout << max(0ll ,A - (sum - a[i]) - 1  ) << " ";
	}
	return 0;
}
//mosta7il_


#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define sz(v)				((int)((v).size()))
#define clr(v, d)			memset(v, d, sizeof(v))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define pb					push_back
#define MP					make_pair
#define P(x)				cout<<#x<<" = { "<<x<<" }\n"
typedef long long         ll;
typedef long double   	  ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>		  vll;

const ll OO = 1e16;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

void fast(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
}
int n;
ll A;
int a[(int)2e5 + 2];
int main(){
	fast();
	ll sum = 0;
	cin >> n >> A;
	lp(i, n){
		cin >> a[i];
		sum += a[i];
	}
	if (n == 1){
		cout << a[0] - 1 << endl;
		return 0;
	}
	if (sum == A){
		lp(i, n){
			cout << a[i]-1 << " ";
		}
		return 0;
	}
	lp(i, n){
		cout << max(0, a[i] - A + 1) << " ";
	}
	return 0;
}
//mosta7il_


#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define sz(v)				((int)((v).size()))
#define clr(v, d)			memset(v, d, sizeof(v))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define pb					push_back
#define MP					make_pair
#define P(x)				cout<<#x<<" = { "<<x<<" }\n"
typedef long long         ll;
typedef long double   	  ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>		  vll;

const ll OO = 1e16;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

void fast(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
}
int n;
ll A;
int a[(int)2e5 + 2];
int main(){
	fast();
	ll sum = 0;
	cin >> n >> A;
	lp(i, n){
		cin >> a[i];
		sum += a[i];
	}
	if (n == 1){
		cout << a[0] - 1 << endl;
		return 0;
	}
	if (sum == A){
		lp(i, n){
			cout << a[i]-1 << " ";
		}
		return 0;
	}
	lp(i, n){
		cout << max(0ll, a[i] - A + 1) << " ";
	}
	return 0;
}
//mosta7il_


#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define sz(v)				((int)((v).size()))
#define clr(v, d)			memset(v, d, sizeof(v))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define pb					push_back
#define MP					make_pair
#define P(x)				cout<<#x<<" = { "<<x<<" }\n"
typedef long long         ll;
typedef long double   	  ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>		  vll;

const ll OO = 1e16;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

void fast(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
}

ll n, A, sum;
int d[2 * 100 * 1000 + 1];
int a[2 * 100 * 1000 + 1];

int main(){
	fast();

	cin >> n >> A;

	lp(i, n){
		cin >> d[i]; sum += d[i];
	}
	if (n == A|| sum == A){
		lp(i, n){
			cout << *(d + i) - 1 << " ";
		}
	}
	else if (sum > A){
		lp(i, n){
			if (*(d + i) >= A){
				a[i] += (*(d + i) - A);
				if (n > 1)a[i]++;
			}
		}
		lp(i, n){
			ll res = (sum - *(d + i) - A);
			if (res < 0){
				res *= -1;
				res--;
				a[i] += res;
			}
		}
		lp(i, n)cout << *(a + i) << " ";
	}

	return 0;
}
//mosta7il_


#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define sz(v)				((int)((v).size()))
#define clr(v, d)			memset(v, d, sizeof(v))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define pb					push_back
#define MP					make_pair
#define P(x)				cout<<#x<<" = { "<<x<<" }\n"
typedef long long         ll;
typedef long double   	  ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>		  vll;

const ll OO = 1e16;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

void fast(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
}

ll n, A, sum;
int d[2 * 100 * 1000 + 1];
int a[2 * 100 * 1000 + 1];

int main(){
	fast();

	cin >> n >> A;

	lp(i, n){
		cin >> d[i]; sum += d[i];
	}
	if (sum == A){
		lp(i, n){
			cout << *(d + i) - 1 << " ";
		}
	}
	else if (sum > A){
		lp(i, n){
			if (*(d + i) >= A){
				a[i] += d[i] - (A - (n - 1));
			}
		}
		lp(i, n){
			ll res = (sum - *(d + i) - A);
			if (res < 0){
				res *= -1;
				res--;
				a[i] += res;
			}
		}
		lp(i, n)cout << *(a + i) << " ";
	}

	return 0;
}
//mosta7il_


#define _CRT_SECURE_NO_WARNINGS
#include<set>
#include<map>
#include<list>
#include<iomanip>
#include<cmath>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<complex>
#include<sstream>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<numeric>
#include<utility>
#include<functional>
#include<stdio.h>
#include<assert.h>
#include<memory.h>
#include<bitset>
using namespace std;

#define all(v)				((v).begin()), ((v).end())
#define sz(v)				((int)((v).size()))
#define clr(v, d)			memset(v, d, sizeof(v))
#define rep(i, v)		for(int i=0;i<sz(v);++i)
#define lp(i, n)		for(int i=0;i<(int)(n);++i)
#define lpi(i, j, n)	for(int i=(j);i<(int)(n);++i)
#define lpd(i, j, n)	for(int i=(j);i>=(int)(n);--i)
#define pb					push_back
#define MP					make_pair
#define P(x)				cout<<#x<<" = { "<<x<<" }\n"
typedef long long         ll;
typedef long double   	  ld;
typedef vector<int>       vi;
typedef vector<double>    vd;
typedef vector< vi >      vvi;
typedef vector< vd >      vvd;
typedef vector<string>    vs;
typedef vector<ll>		  vll;

const ll OO = 1e16;

const double EPS = (1e-7);
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }

void fast(){
	std::ios_base::sync_with_stdio(0);
	cin.tie(NULL); cout.tie(NULL);
}

ll n, A, sum;
int d[2 * 100 * 1000 + 1];
int a[2 * 100 * 1000 + 1];

int main(){
	fast();

	cin >> n >> A;

	lp(i, n){
		cin >> d[i]; sum += d[i];
	}
	if (sum == A){
		lp(i, n){
			cout << *(d + i) - 1 << " ";
		}
	}
	else if (sum > A){
		lp(i, n){
				a[i] += max(0ll,d[i] - (A - (n - 1)));
		}
		lp(i, n){
			ll res = (sum - *(d + i) - A);
			if (res < 0){
				res *= -1;
				res--;
				a[i] += res;
			}
		}
		lp(i, n)cout << *(a + i) << " ";
	}

	return 0;
}
//mosta7il_


#include <bits/stdc++.h>

#define L(a) (int)((a).size())
#define sqr(x) ((x) * 1ll * (x))
#define vi vector<int>
#define mp make_pair
#define pub push_back
#define pob pop_back
#define ii pair<int, int>
#define vii vector <ii>
#define all(s) (s).begin(),(s).end()
#define fore(i, l ,r) for(int i = (int)l; i < (int)r; i++)
#define TRACE(x) cerr << #x << " : " << x << endl
#define _ << " - " <<

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ld EPS = 1e-9;
const ld PI = acos(-1);
const int MOD = (int)1e9 + 7;
const int INF32 = (int)1e9;
const int INF64 = (ll)1e19;

using namespace std;

ll d[200005];
ll mini[200005];

int main()
{
	ll sum = 0;
	ll n, a;
	cin >> n >> a;
	fore(i, 0, n)
	{
		cin >> d[i];
		sum += d[i];
	}
	fore(i, 0, n)
	{
		mini[i] = sum - d[i];
		ll res = 0;
		ll k = a - (n - 1);
		res += max(d[i] - k, 0);
		if(mini[i] < a)
		{
			int minimum = a - mini[i];
			res += minimum - 1;
		}
		cout << res << " ";
	}
}


#include <bits/stdc++.h>

#define L(a) (int)((a).size())
#define sqr(x) ((x) * 1ll * (x))
#define vi vector<int>
#define mp make_pair
#define pub push_back
#define pob pop_back
#define ii pair<int, int>
#define vii vector <ii>
#define all(s) (s).begin(),(s).end()
#define fore(i, l ,r) for(int i = (int)l; i < (int)r; i++)
#define TRACE(x) cerr << #x << " : " << x << endl
#define _ << " - " <<

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ld EPS = 1e-9;
const ld PI = acos(-1);
const int MOD = (int)1e9 + 7;
const int INF32 = (int)1e9;
const int INF64 = (ll)1e19;

using namespace std;

int d[200005];
ll mini[200005];

int main()
{
	ll sum = 0;
	int n, a;
	cin >> n >> a;
	fore(i, 0, n)
	{
		cin >> d[i];
		sum += d[i];
	}
	fore(i, 0, n)
	{
		mini[i] = sum - d[i];
		int res = 0;
		int k = a - (n - 1);
		res += max(d[i] - k, 0);
		if(mini[i] < a)
		{
			int minimum = a - mini[i];
			res += minimum - 1;
		}
		cout << res << " ";
	}
}


#include <bits/stdc++.h>

#define L(a) (int)((a).size())
#define sqr(x) ((x) * 1ll * (x))
#define vi vector<int>
#define mp make_pair
#define pub push_back
#define pob pop_back
#define ii pair<int, int>
#define vii vector <ii>
#define all(s) (s).begin(),(s).end()
#define fore(i, l ,r) for(int i = (int)l; i < (int)r; i++)
#define TRACE(x) cerr << #x << " : " << x << endl
#define _ << " - " <<

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;

const ld EPS = 1e-9;
const ld PI = acos(-1);
const int MOD = (int)1e9 + 7;
const int INF32 = (int)1e9;
const int INF64 = (ll)1e19;

using namespace std;

ll d[200005];
ll mini[200005];

int main()
{
	ll sum = 0;
	ll n, a;
	cin >> n >> a;
	fore(i, 0, n)
	{
		cin >> d[i];
		sum += d[i];
	}
	fore(i, 0, n)
	{
		mini[i] = sum - d[i];
		ll res = 0;
		ll k = a - (n - 1);
		res += max(d[i] - k, 0 * 1LL);
		if(mini[i] < a)
		{
			int minimum = a - mini[i];
			res += minimum - 1;
		}
		cout << res << " ";
	}
}


/*
    Author - linpaws07
*/
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define inf (int)1e9
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define scd(t) scanf("%d",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define memst(a, b) memset(a, (b), sizeof(a))
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rp(i,n) for(int i=0;i<(n);i++)
#define fr(i,a,b) for(int i=(a);i<(b);i++)
#define fre(i,a,b) for(int i=(a);i<=(b);i++)
#define frd(i,a,b) for(int i=(a);i>=(b);i--)
inline bool eq(double a, double b) { return fabs(a-b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
auto start = high_resolution_clock::now();
inline void measure();

int d[200002];
int64 sum;
int main(){
    FAST
    //Do awesome things here
    int64 n, a;
    cin >> n >> a;
    rp(i, n){
        cin >> d[i];
        sum += d[i];
    }
    if(n == 1){
        cout << d[0] - 1 << endl;
        return 0;
    }
    rp(i, n){
        int l = (n - 1);
        int r = sum - d[i], ans = 0;
        int val = (a - 1) - r;
        val = val > 0 ? val : 0;
        ans = ans + val;
        val = l - (a - d[i]);
        val = val > 0 ? val : 0;
        ans = ans + val;
        cout << ans << " ";
    }
    cout << endl;
    return 0;
}

inline void measure(){
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
}


/*
    Author - linpaws07
*/
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define inf (int)1e9
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define scd(t) scanf("%d",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define memst(a, b) memset(a, (b), sizeof(a))
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rp(i,n) for(int i=0;i<(n);i++)
#define fr(i,a,b) for(int i=(a);i<(b);i++)
#define fre(i,a,b) for(int i=(a);i<=(b);i++)
#define frd(i,a,b) for(int i=(a);i>=(b);i--)
inline bool eq(double a, double b) { return fabs(a-b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
auto start = high_resolution_clock::now();
inline void measure();

int d[200002];
int main(){
    FAST
    //Do awesome things here
    int64 n, a;
    cin >> n >> a;
    rp(i, n){
        cin >> d[i];
    }
    if(n == 1){
        cout << d[0] - 1 << endl;
        return 0;
    }
    rp(i, n){
        int val = abs(d[i] - (a - (n - 1)));
        cout << val << " ";
    }
    cout << endl;
    return 0;
}

inline void measure(){
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
}


/*
    Author - linpaws07
*/
#include <bits/stdc++.h>
using namespace std;
using namespace std::chrono;
#define mod 1000000007
#define mp make_pair
#define pb push_back
#define inf (int)1e9
#define eps 1e-9
#define PI 3.1415926535897932384626433832795
#define scd(t) scanf("%d",&t)
#define sclld(t) scanf("%lld",&t)
#define scc(t) scanf("%c",&t)
#define scs(t) scanf("%s",t)
#define scf(t) scanf("%f",&t)
#define sclf(t) scanf("%lf",&t)
#define memst(a, b) memset(a, (b), sizeof(a))
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<vi> vvi;
typedef map<int,int> mpii;
typedef set<int> seti;
typedef multiset<int> mseti;
typedef long int int32;
typedef unsigned long int uint32;
typedef long long int int64;
typedef unsigned long long int uint64;
#define FAST ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define rp(i,n) for(int i=0;i<(n);i++)
#define fr(i,a,b) for(int i=(a);i<(b);i++)
#define fre(i,a,b) for(int i=(a);i<=(b);i++)
#define frd(i,a,b) for(int i=(a);i>=(b);i--)
inline bool eq(double a, double b) { return fabs(a-b) < 1e-9; }
inline int two(int n) { return 1 << n; }
inline int test(int n, int b) { return (n>>b)&1; }
inline void set_bit(int & n, int b) { n |= two(b); }
inline void unset_bit(int & n, int b) { n &= ~two(b); }
inline int last_bit(int n) { return n & (-n); }
inline int ones(int n) { int res = 0; while(n && ++res) n-=n&(-n); return res; }
auto start = high_resolution_clock::now();
inline void measure();

int d[200002];
int64 sum;
int main(){
    FAST
    //Do awesome things here
    int64 n, a;
    cin >> n >> a;
    rp(i, n){
        cin >> d[i];
        sum += d[i];
    }
    if(n == 1){
        cout << d[0] - 1 << endl;
        return 0;
    }
    rp(i, n){
        int64 l = (n - 1);
        int64 r = sum - d[i], ans = 0;
        int64 val = (a - 1) - r;
        val = val > 0 ? val : 0;
        ans = ans + val;
        val = l - (a - d[i]);
        val = val > 0 ? val : 0;
        ans = ans + val;
        cout << ans << " ";
    }
    cout << endl;
    return 0;
}

inline void measure(){
    auto stop = high_resolution_clock::now();
    auto duration = duration_cast<milliseconds>(stop - start);
    cout << duration.count() << endl;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef bitset<15>mask;
ll n,x,y,z,a[1000000],k,sol,sum,MOD=1000000007,b[1000000];
vector<ll>v;
pair<ll,ll>p[1000000];
map<ll,ll>m;
string s1,s2,s;
ll fastpow(ll num, ll p) {
  if (p == 0)
    return 1;
  if (p % 2)
    return (num % MOD * fastpow(num, p - 1)) % MOD;
  ll a = fastpow(num, p / 2);
  return (a * a) % MOD;
}
inline ll fixMod(ll a, ll m){
  return (a%m + m)%m;
}
inline ll floor(ll a, ll b){
  return (a - fixMod(a, b))/b;
}

int main()
{
  cin>>n>>k;
  for(int i=0;i<n;i++)
    cin>>a[i];
  if(n==1)
  {
      cout<<a[0]-1<<endl;
      return 0;
  }
  for(int i=0;i<n;i++)if(a[i]>=k)b[i]+=(a[i]-k)+1,a[i]-=b[i];
  for(int i=0;i<n;i++)sum+=a[i];
  sol=sum-k;
  for(int i=0;i<n;i++)
   cout<<((a[i]-sol)-1)+b[i]<<" ";

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef bitset<15>mask;
ll n,x,y,z,a[1000000],k,sol,sum,MOD=1000000007,b[1000000];
vector<ll>v;
pair<ll,ll>p[1000000];
map<ll,ll>m;
string s1,s2,s;
ll fastpow(ll num, ll p) {
  if (p == 0)
    return 1;
  if (p % 2)
    return (num % MOD * fastpow(num, p - 1)) % MOD;
  ll a = fastpow(num, p / 2);
  return (a * a) % MOD;
}
inline ll fixMod(ll a, ll m){
  return (a%m + m)%m;
}
inline ll floor(ll a, ll b){
  return (a - fixMod(a, b))/b;
}

int main()
{
  cin>>n>>k;
  for(int i=0;i<n;i++)
    cin>>a[i];
  if(n==1)
  {
      cout<<a[0]-1<<endl;
      return 0;
  }
  for(int i=0;i<n;i++)if(a[i]>=k)b[i]+=(a[i]-k)+1,a[i]-=b[i];
  for(int i=0;i<n;i++)sum+=a[i];
  sol=sum-k;
  for(int i=0;i<n;i++)
  {
      x=((a[i]-sol)-1)+b[i];
      if(x<0)
        cout<<0<<" ";
      else
   cout<<x<<" ";
  }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef bitset<15>mask;
ll n,x,y,z,a[1000000],k,sol,sum,MOD=1000000007,b[1000000];
vector<ll>v;
pair<ll,ll>p[1000000];
map<ll,ll>m;
string s1,s2,s;
ll fastpow(ll num, ll p) {
  if (p == 0)
    return 1;
  if (p % 2)
    return (num % MOD * fastpow(num, p - 1)) % MOD;
  ll a = fastpow(num, p / 2);
  return (a * a) % MOD;
}
inline ll fixMod(ll a, ll m){
  return (a%m + m)%m;
}
inline ll floor(ll a, ll b){
  return (a - fixMod(a, b))/b;
}

int main()
{
  cin>>n>>k;
  for(int i=0;i<n;i++)
    cin>>a[i];
  if(n==1)
  {
      cout<<a[0]-1<<endl;
      return 0;
  }
   if(n==k)
  {
      for(int i=0;i<n;i++)
      {
          cout<<a[i]-1<<" ";

      }
      return 0;

  }
  for(int i=0;i<n;i++)if(a[i]>=k)b[i]+=(a[i]-k)+1,a[i]-=b[i];
  for(int i=0;i<n;i++)sum+=a[i];
  sol=sum-k;
  for(int i=0;i<n;i++)
  {
      y=((a[i]-sol)-1);
      if(y<0)y=0;
      y+=b[i];
   cout<<y<<" ";
  }

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef bitset<15>mask;
ll n,x,y,z,a[1000000],k,sol,sum,MOD=1000000007,b[1000000];
vector<ll>v;
pair<ll,ll>p[1000000];
map<ll,ll>m;
string s1,s2,s;
ll fastpow(ll num, ll p) {
  if (p == 0)
    return 1;
  if (p % 2)
    return (num % MOD * fastpow(num, p - 1)) % MOD;
  ll a = fastpow(num, p / 2);
  return (a * a) % MOD;
}
inline ll fixMod(ll a, ll m){
  return (a%m + m)%m;
}
inline ll floor(ll a, ll b){
  return (a - fixMod(a, b))/b;
}

int main()
{
  cin>>n>>k;
  for(int i=0;i<n;i++)
    cin>>a[i];
  if(n==1)
  {
      cout<<a[0]-1<<endl;
      return 0;
  }
  for(int i=0;i<n;i++)if(a[i]>=k)b[i]+=(a[i]-k)+1,a[i]-=b[i];
  for(int i=0;i<n;i++)
  {
      if(a[i]+(n-1)>k)
      {
          b[i]+=(a[i]+(n-1))-k;
          a[i]-=(a[i]+(n-1))-k;
      }
  }
  for(int i=0;i<n;i++)sum+=a[i];
  sol=sum-k;
  for(int i=0;i<n;i++)
  {
      x=(a[i]-sol)-1;
      if(x<0)x=0;
      x+=b[i];
     cout<<x<<" ";
  }

    return 0;
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

int n, s, u, t;
int a[100100];

int main () {
    cin >> n >> s;
    for ( int i = 1; i <= n; i++) {
        cin >> a[i];
        t = t + a[i];
    }
    for ( int i = 1; i <= n; i++) {
        cout << max(0, a[i] - t + s - 1) + max(0,a[i] - s - 1 + n) << " ";
    }
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

int n, s, u, t;
int a[100100];

int main () {
    cin >> n >> s;
    for ( int i = 1; i <= n; i++) {
        cin >> a[i];
        t = t + a[i];
    }
    for ( int i = 1; i <= n; i++) {
        cout << max(0, a[i] - t + s - 1) + max(0,a[i] - s + 1 + n) << " ";
    }
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

int n, s, u, t;
int a[200200];

int main () {
    cin >> n >> s;
    for ( int i = 1; i <= n; i++) {
        cin >> a[i];
        t = t + a[i];
    }
    for ( int i = 1; i <= n; i++) {
        cout << max(0, a[i] - t + s - 1) + max(0,a[i] - s - 1 + n) << " ";
    }
}


#include <iostream>
#include <algorithm>
#include <math.h>
#include <stdio.h>

using namespace std;

long long n, s, u, t;
long long a[200200];

int main () {
    cin >> n >> s;
    for ( int i = 1; i <= n; i++) {
        cin >> a[i];
        t = t + a[i];
    }
    for ( int i = 1; i <= n; i++) {
        cout << max(0ll, a[i] - t + s - 1) + max(0ll,a[i] - s - 1 + n) << " ";
    }
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define sf(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mod 1000000007LL

void solve()
{
ll n,A;
cin>>n>>A;

ll s=0;
ll a[n];
for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }

    if(n==1)
    {
        cout<<n-1;
        return ;
    }

for(int i=0;i<n;i++)
{
    ll vmax = min(s-n-1LL,a[i]);
    ll vmin = max(1LL,(A-s+a[i]));
        cout<<vmin-1+a[i]-vmax<<" ";
}

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
// cin>>t;
    while(t--)
    {
     solve();

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define sf(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mod 1000000007LL

void solve()
{
ll n,A;
cin>>n>>A;

ll s=0;
ll a[n];
for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }

    if(n==1)
    {
        cout<<a[i]-1;
        return ;
    }

for(int i=0;i<n;i++)
{
    ll vmax = min(A-(n-1LL),a[i]);
    ll vmin = max(1LL,(A-s+a[i]));
        cout<<vmin-1+a[i]-vmax<<" ";
}

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
// cin>>t;
    while(t--)
    {
     solve();

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define sf(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mod 1000000007LL

void solve()
{
ll n,A;
cin>>n>>A;

ll s=0;
ll a[n];
for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }

for(int i=0;i<n;i++)
{
    ll sl = s- a[i];
    if(sl>=A)
        cout<<0<<" ";
    else
        cout<<A-sl-1<<" ";
}

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
// cin>>t;
    while(t--)
    {
     solve();

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define sf(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mod 1000000007LL

void solve()
{
ll n,A;
cin>>n>>A;

ll s=0;
ll a[n];
for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }

    if(n==1)
    {
        cout<<a[0]-1;
        return ;
    }

for(int i=0;i<n;i++)
{
    ll vmax = min(s-n-1LL,a[i]);
    ll vmin = max(1LL,(A-s+a[i]));
        cout<<vmin-1+a[i]-vmax<<" ";
}

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
// cin>>t;
    while(t--)
    {
     solve();

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define sf(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mod 1000000007LL

void solve()
{
ll n,A;
cin>>n>>A;

ll s=0;
ll a[n];
for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }

    if(n==1)
    {
        cout<<a[0]-1;
        return ;
    }

for(int i=0;i<n;i++)
{
    ll vmax = min(A-(n-1LL),a[i]);
    ll vmin = max(1LL,(A-s+a[i]));
        cout<<vmin-1+a[i]-vmax<<" ";
}

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
// cin>>t;
    while(t--)
    {
     solve();

    }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ld long double
#define sf(x) scanf("%I64d",&x)
#define pf(x) printf("%d",x)
#define ALL(x) x.begin(),x.end()
#define pb push_back
#define mod 1000000007LL

void solve()
{
ll n,A;
cin>>n>>A;

ll s=0;
ll a[n];
for(int i=0;i<n;i++)
    {
        cin>>a[i];
        s+=a[i];
    }

    if(n==1)
    {
        cout<<n-1;
        return ;
    }

for(int i=0;i<n;i++)
{
    ll vmax = max(s-n-1LL,a[i]);
    ll vmin = max(1LL,(A-s+a[i]));
        cout<<vmin-1+a[i]-vmax<<" ";
}

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
   //freopen("input.txt","r",stdin);
    int t=1 ;
// cin>>t;
    while(t--)
    {
     solve();

    }
    return 0;
}


#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define x first
#define y second
#define deb(x) cout<< #x <<" "<<x<<"\n";
#define deb1(x,y) cout<< #x<<" "<<x<<" "<<#y<<" "<<y<<"\n";
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sz(a) (int)a.size()
const ll MOD=1e9+9;
const int MAX=2e5+1;
const double EPS=1e-10;
int dcmp(double x,double y)
{return (fabs(x-y)<=EPS)?0:(x<y)?1:-1;}
void init(){
	ios
	///cout << fixed << setprecision(15);
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
int main()
{
	init();
	ll n,sum;
	cin>>n>>sum;
	ll s=0;
	vector<ll>a(n);
	for(int i=0;i<n;i++)
		{cin>>a[i];s+=a[i];}
	if(n==1)
	{
		if(sum<=n)
		{
			cout<<a[0]-1;
		}
		else{
			cout<<a[0];
		}
		return 0;
	}
	for(int i=0;i<n;i++)
	{
		ll mini=n-1;
		ll maxa=s-a[i];
		if(sum<mini)
			cout<<a[i]<<" ";
		else{
			if(sum<=maxa)
				cout<<max(sum-mini-1,a[i]-1)<<" ";
			else
				cout<<min((sum-maxa)-1,a[i])<<" ";
		}
	}

}


#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define x first
#define y second
#define deb(x) cout<< #x <<" "<<x<<"\n";
#define deb1(x,y) cout<< #x<<" "<<x<<" "<<#y<<" "<<y<<"\n";
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sz(a) (int)a.size()
const ll MOD=1e9+9;
const int MAX=2e5+1;
const double EPS=1e-10;
int dcmp(double x,double y)
{return (fabs(x-y)<=EPS)?0:(x<y)?1:-1;}
void init(){
	ios
	///cout << fixed << setprecision(15);
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
int main()
{
	init();
	ll n,sum;
	cin>>n>>sum;
	ll s=0;
	vector<ll>a(n);
	for(int i=0;i<n;i++)
		{cin>>a[i];s+=a[i];}
	for(int i=0;i<n;i++)
	{
		ll mini=n-1;
		ll maxa=s-a[i];
		if(sum>maxa)
		{
			ll k=0;
			if(sum-a[i]>maxa)
				k=a[i];
			else
				k=sum-maxa-1;
			if(sum-a[i]<mini)
			{
				k+=mini-sum+a[i];
			}
			cout<<k<<" ";
		}
		else if(sum>=mini)
		{
			ll k=0;
			if(sum-a[i]<mini)
			{
				k+=mini-sum+a[i];
			}
			cout<<k<<" ";
		}
		else{
			cout<<a[i]<<" ";
		}

	}

}


#include<bits/stdc++.h>
#define ios ios_base::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define ll long long
#define pb push_back
#define mk make_pair
#define x first
#define y second
#define deb(x) cout<< #x <<" "<<x<<"\n";
#define deb1(x,y) cout<< #x<<" "<<x<<" "<<#y<<" "<<y<<"\n";
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define sz(a) (int)a.size()
const ll MOD=1e9+9;
const int MAX=2e5+1;
const double EPS=1e-10;
int dcmp(double x,double y)
{return (fabs(x-y)<=EPS)?0:(x<y)?1:-1;}
void init(){
	ios
	///cout << fixed << setprecision(15);
	#ifndef ONLINE_JUDGE
		freopen("input.txt","r",stdin);
		freopen("output.txt","w",stdout);
	#endif
}
int main()
{
	init();
	ll n,sum;
	cin>>n>>sum;
	ll s=0;
	vector<ll>a(n);
	for(int i=0;i<n;i++)
		{cin>>a[i];s+=a[i];}
	if(n==1)
	{
		if(sum<=a[0])
		{
			cout<<a[0]-1;
		}
		else{
			cout<<a[0];
		}
		return 0;
	}
	for(int i=0;i<n;i++)
	{
		ll mini=n-1;
		ll maxa=s-a[i];
		if(sum<mini)
			cout<<a[i]<<" ";
		else{
			if(sum<=maxa)
				cout<<max(sum-mini-1,a[i]-1)<<" ";
			else
				cout<<min((sum-maxa)-1,a[i])<<" ";
		}
	}

}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=202020;

INT a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	INT A; cin>>A;

	INT sum=0;
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum+=a[i];
	}

	for(int i=1;i<=n;i++){
		INT mx=A-n+1;
		INT mn=A-sum+a[i];
//		cout<<mn<<" "<<mx<<endl;
		if(mn<=1 && a[i]<=mx)cout<<0<<" ";
		else if(1<=mn && mx<=a[i])cout<<a[i]-mx+mn-1<<" ";
		else if(mx<1 || mn>a[i])cout<<a[i]<<" ";
		else cout<<abs(mn-1)<<" ";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;
typedef long long INT;

#define NN 200010
int d[NN];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	int n;
	INT A;
	scanf("%d %I64d", &n, &A);
	INT tot = 0;
	for(int i=1; i<=n; i++){
		scanf("%d", &d[i]);
		tot += d[i];
	}
	for(int i=1; i<=n; i++){
		int x = d[i];
		int L = max(A + x - tot, 1LL);
		int R = min((INT)x, A - n + 1);
		if(R >= L) printf("%d ", x - (R - L + 1));
	}
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=202020;

INT a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	INT A; cin>>A;

	INT sum=0;
	for(int i=1;i<=n;i++){
		scanf("%d",a+i);
		sum+=a[i];
	}

	for(int i=1;i<=n;i++){
		INT mx=A-n+1;
		INT mn=A-sum+a[i];
//		cout<<mn<<" "<<mx<<endl;
		if(mn<=1 && a[i]<=mx)cout<<0<<" ";
		else if(1<=mn && mx<=a[i])cout<<a[i]-mx+mn-1<<" ";
		else if(mx<1 || mn>a[i])cout<<a[i]<<" ";
		else if(mn>1)cout<<mn-1<<" ";
		else cout<<mx-1<<" ";
	}

	return 0;
}


#include <bits/stdc++.h>
#define NN 200100
using namespace std;
typedef long long INT;

INT a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif
	INT n, sm, sum = 0;
	scanf("%I64d%I64d", &n, &sm);
	for(int i = 1; i <= n; i++) scanf("%I64d", &a[i]), sum += a[i];

	for(int i = 1; i <= n; i++){
		INT st = max(1ll, sm-(sum-a[i]));
		INT ed = min(a[i], sm - (n-1));
		INT ans = a[i] - (ed - st + 1);
		printf("%I64d ", ans);
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define NN 210000
vector <int> ans;
typedef __int64 INT;
int d[NN], b[NN];
int main(){
#ifndef ONLINE_JUDGE
	freopen("A.in", "r", stdin);
	freopen("test.out", "w", stdout);
#endif
	int n;
	INT a; scanf("%d%I64d", &n, &a);
	INT sum = 0;
	for(int i = 0; i < n; i++) scanf("%d", &d[i]), sum += d[i];
	for(int i = 0; i < n; i++){
		if(a > sum - d[i]) b[i] += (a - (sum - d[i]) - 1);
		if(a - (n - 1) < d[i]) b[i] += (d[i] - (a - (n - 1)));
//		b[i] = (sum - d[i]) - a;
	}
	for(int i = 0; i < n; i++){
		printf("%d%c", b[i], i == n - 1 ? '\n' : ' ');
	}
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=202020;

INT a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	INT A; cin>>A;

	INT sum=0;
	for(int i=1;i<=n;i++){
		scanf("%I64d",a+i);
		sum=sum+a[i];
	}

	for(int i=1;i<=n;i++){
		INT mx=A-n+1;
		INT mn=A-sum+a[i];

		mx=min(mx,a[i]);
		mn=max(mn,1LL);

		if(mx<mn)cout<<a[i]<<" ";
		else cout<<a[i]-mx+mn-1<<" ";
	}

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long INT;

const int NN=202020;

INT a[NN];

int main(){
#ifndef ONLINE_JUDGE
	freopen("in.in","r",stdin);
#endif

	int n; cin>>n;
	INT A; cin>>A;

	INT sum=0;
	for(int i=1;i<=n;i++){
		scanf("%I64d",a+i);
		sum=sum+a[i];
	}

	for(int i=1;i<=n;i++){
		INT mx=A-n+1;
		INT mn=A-sum+a[i];
//		cout<<mn<<" "<<mx<<endl;
		if(mn<=1 && a[i]<=mx)cout<<0<<" ";
		else if(1<=mn && mx<=a[i])cout<<a[i]-mx+mn-1<<" ";
		else if(mx<1 || mn>a[i])cout<<a[i]<<" ";
		else if(mn>=1)cout<<mn-1<<" ";
		else cout<<mx-1<<" ";
	}

	return 0;
}


#include<bits/stdc++.h>
#define inf 0x3f3f3f3f
#define pb push_back
#define y second
#define x first

using namespace std;
using INT=long long;
using VI=vector<int>;
using pii=pair<int, int>;
template<typename A, typename B> void smin(A &a, const B &b) {if(a>b) a=b;}
template<typename A, typename B> void smax(A &a, const B &b) {if(a<b) a=b;}

int d[202020];

int main() {
#ifndef ONLINE_JUDGE
	freopen("in.in", "r", stdin);
	freopen("out.out", "w", stdout);
#endif

	int n; INT A; scanf("%d %I64d", &n, &A);
	INT sum=0;
	for(int i=1; i<=n; i++) {
		scanf("%d", d+i);
		sum+=d[i];
	}

	for(int i=1; i<=n; i++) {
		INT mn=n-1, mx=sum-d[i];
		INT L=max(1LL, A-mx);
		INT R=min((INT)d[i], A-mn);
		printf("%d ", d[i]-R+L-1);
	}
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

    long long int n,m,i,j,a[100005];

    cin>>n>>m;
    for(i=1;i<=n;i++)
        cin>>a[i];

    for(i=1;i<=n;i++)
        cout<<m+1<<" ";
}


#include<bits/stdc++.h>
using namespace std;

int main()
{

    long long int n,m,i,j,a[200005],s=0,x,y,mn,mx;

    cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i];
     s=s+a[i];
    }
if(n==1){
    cout<<a[1]-1<<endl;
    return 0;
}
if(s==m){
    for(i=1;i<=n;i++)
        cout<<a[i]-1<<" ";
}
else{
for(i=1;i<=n;i++){
    mn=n-1;
    mx=s-a[i];
    if(mn+a[i]<m){
        if(mx<m){
            y=m-mx-1;
            cout<<y<<" ";
        }
        else{
            cout<<0<<" ";
        }
    }
    else{
        x=m-mn;
        y=a[i]-x;
        cout<<y<<" ";
    }
}
}

}


#include<bits/stdc++.h>
using namespace std;

int main()
{

    long long int n,m,i,j,a[200005],s=0,x,y,mn,mx,z=0;

    cin>>n>>m;
    for(i=1;i<=n;i++){
        cin>>a[i];
     s=s+a[i];
    }
if(n==1){
    cout<<a[1]-1<<endl;
    return 0;
}
if(s==m){
    for(i=1;i<=n;i++)
        cout<<a[i]-1<<" ";
}
else{
for(i=1;i<=n;i++){
    mn=n-1;
    mx=s-a[i];
    if(mn+a[i]<=m){
        if(mx<m){
            y=m-mx-1;
            cout<<y<<" ";
        }
        else{
            cout<<0<<" ";
        }
    }
    else{
          if(mx<m) z=m-mx-1;
        x=m-mn;
        y=a[i]-x+z;
        cout<<y<<" ";

    }
}
}

}