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



class Solution
{
    public:
    //Function to find the next greater element for each element of the array.
    vector<long long> nextLargerElement(vector<long long> arr, int n){
        // Your code here
        vector<long long> v;
        stack<long long> s;
        for(int i=n-1;i>=0;i--)
        {
            
            if(s.empty())
            {
               v.push_back(-1);
              
                
            }
            else if(!s.empty() &&s.top()>arr[i])
            {
            v.push_back(s.top());
            }
            else if(!s.empty() && s.top()<=arr[i])
            {
                while(!s.empty() && s.top()<=arr[i])
                {
                    s.pop();
                }
                if(s.size()==0)
                {
                    v.push_back(-1);
                }else{
                    v.push_back(s.top());
                }
            }
            s.push(arr[i]);
        }
        reverse(v.begin(),v.end());
        return v;
    }
};



int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        
        int n;
        cin>>n;
        vector<long long> arr(n);
        for(int i=0;i<n;i++)
            cin>>arr[i];
        
        Solution obj;
        vector <long long> res = obj.nextLargerElement(arr, n);
        for (long long i : res) cout << i << " ";
        cout<<endl;
    }
	return 0;
}


//Expected Time Complexity : O(N)
//Expected Auxiliary Space : O(N)
