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

// Function to print Next Greater Element for each element of the array
void nextGreater(int a[], int n) 
{ 
    stack<int> s;
    int i=0,j=1;
    for(;i<n;i++){
        // int j=i+1;
        while(a[j]<=a[i]){
            if(j==n){
                break;
            }
            j++;
        }
        if(j>=n){
            s.push(-1);
            continue;
        }
        s.push(a[j]);
        // cout<<"pushing "<<a[j]<<" for "<<a[i]<<endl;
    }

    stack<int> x;
    while(!s.empty()){
        x.push(s.top());
        s.pop();
    }
    s=x;

    for(int i=0;i<n;i++){
        cout<<a[i]<<','<<s.top()<<endl;
        s.pop();
    }
} 

// The Main Function
int main() 
{ 
    int t;
    cin>>t;
    while(t--){
        int n;
        cin>>n;
        int arr[n];
        for(int i=0; i<n; i++){
            cin>>arr[i];
        }
        nextGreater(arr, n); 
    }
	
	return 0; 
} 