Discussion About Next Greater Element

This is Discussion thread about Next Greater Element

The code is compiling and running but when i’m subitting the code it’s showing wrong answer while the output is right.
import java.util.*;
public class Main {

public static void main(String args[]) throws Exception {
	// Your Code Here
	Scanner s = new Scanner(System.in);
	
	int t = s.nextInt();
	while(t-- > 0){
		
		int N=s.nextInt();
		int[] nums=new int[N];
		for(int i=0;i<N;i++){
			nums[i]=s.nextInt();
		}
        
        Main obj = new Main();
        StacksUsingArrays stack = obj.new StacksUsingArrays();
		int[] ans=NextGreater(nums, stack);
		for(int i=0;i<ans.length;i++){
			System.out.println(nums[i]+","+ans[i]+" ");
		}
// 		System.out.println();
	}

}

public static int[] NextGreater(int[] nums,StacksUsingArrays stack) throws Exception {
    
  //Write your code here
		int n = nums.length;
	stack.push(nums[0]);
	int m = 0;
	int[] arr = new int[n];
	int element, next;

	for (int i = 1; i < n; i++) {

		if (!stack.isEmpty()) {
			next = nums[i];
			element = stack.pop();
			while (element < next) {
				arr[m] = next;
				m++;
				if (stack.isEmpty()) {
					break;
				}
				element = stack.pop();
			}
			if (element > next) {
				stack.push(element);
			}
			stack.push(next);

		}
	}
	while (!stack.isEmpty()) {
		stack.pop();
		arr[m] = -1;
		m++;
	}

	return arr;
}

private class StacksUsingArrays {
	private int[] data;
	private int tos;

	public static final int DEFAULT_CAPACITY = 10;

	public StacksUsingArrays() throws Exception {
		// TODO Auto-generated constructor stub
		this(DEFAULT_CAPACITY);
	}

	public StacksUsingArrays(int capacity) throws Exception {
		if (capacity <= 0) {
			System.out.println("Invalid Capacity");
		}
		this.data = new int[capacity];
		this.tos = -1;
	}

	public int size() {
		return this.tos + 1;
	}

	public boolean isEmpty() {
		if (this.size() == 0) {
			return true;
		} else {
			return false;
		}
	}

	public void push(int item) throws Exception {
		if (this.size() == this.data.length) {
			throw new Exception("Stack is Full");
		}
		this.tos++;
		this.data[this.tos] = item;
	}

	public int pop() throws Exception {
		if (this.size() == 0) {
			throw new Exception("Stack is Empty");
		}
		int retVal = this.data[this.tos];
		this.data[this.tos] = 0;
		this.tos--;
		return retVal;
	}

	public int top() throws Exception {
		if (this.size() == 0) {
			throw new Exception("Stack is Empty");
		}
		int retVal = this.data[this.tos];
		return retVal;
	}

	public void display() throws Exception {
		if (this.size() == 0) {
			throw new Exception("Stack is Empty");
		}
		for (int i = this.tos; i >= 0; i--) {
			System.out.println(this.data[i]);
		}

	}

}

}