Help me in solving STACK07 problem

My issue

Can some one review my code and tell why i am getting an empty line in the first line of output and the rest of testcases are correct.

My code

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
		int t = sc.nextInt();
		while(t-->0){
		    String exp = sc.nextLine();
		    Stack<Character> stack = new Stack<>();
		    String ans = "";
		    
		    for(int i = 0; i < exp.length(); i++){
		        char c = exp.charAt(i);
		        if(c >= 'a' && c <= 'z'){
		            ans += c;
		        }
		        
		        else if(c == '('){
		            stack.push(c);
		        }
		        
		        else if(c == ')'){
		            while(stack.peek() != '('){
		                ans += stack.peek();
		                stack.pop();
		            }stack.pop();
		        }
		        
		        else {
		            stack.push(c);
		        }
		        
		    }
		    
		    System.out.println(ans);
		   
		}

	}
}



Learning course: Stacks and Queues
Problem Link: Practice problem - Transform the Expression Practice Problem in Stacks and Queues - CodeChef

@achauhan8785
plzz refer the following java code

import java.util.Scanner;
import java.util.Stack;

class InfixToPostfix {
    static int getPrecedence(char op) {
        switch (op) {
            case '+':
            case '-':
                return 1;
            case '*':
            case '/':
                return 2;
            case '^':
                return 3;
            default:
                return 0;
        }
    }

    static String infixToPostfix(String infix) {
        Stack<Character> operatorStack = new Stack<>();
        StringBuilder postfix = new StringBuilder();

        for (char c : infix.toCharArray()) {
            int precedence = getPrecedence(c);

            if (Character.isLetterOrDigit(c)) {
                postfix.append(c);
            } else if (c == '(') {
                operatorStack.push(c);
            } else if (c == ')') {
                while (!operatorStack.isEmpty() && operatorStack.peek() != '(') {
                    postfix.append(operatorStack.pop());
                }
                operatorStack.pop(); // Remove the '('
            } else {
                while (!operatorStack.isEmpty() && precedence <= getPrecedence(operatorStack.peek())) {
                    postfix.append(operatorStack.pop());
                }
                operatorStack.push(c);
            }
        }

        while (!operatorStack.isEmpty()) {
            postfix.append(operatorStack.pop());
        }
        return postfix.toString();
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int t = scanner.nextInt();
        scanner.nextLine(); // Consume newline

        while (t-- > 0) {
            String infixExpression = scanner.nextLine();
            String postfixExpression = infixToPostfix(infixExpression);
            System.out.println(postfixExpression);
        }
    }
}

Thanks for sharing. I just added sc.nextLine() after taking testcase inputs and it worked.