Here is the problem that I am trying to solve:
Task Given a string, S, of length N that is indexed from 0 to N-1 , print its even-indexed and odd-indexed characters as space-separated strings on a single line.
Input Format
The first line contains an integer, T (the number of test cases). Each line i of the T subsequent lines contain a String, S.
Sample Input
2
Hacker
Rank  
Sample Output
Hce akr
Rn ak  
My problem with the code is that the first line (Hce akr) is getting printed but the next one is not. I read some similar problems and put in a nextLine() before the start of the loop (inputting the Strings) but then only the second output is displayed (Rn ak). I don't understand where I am going wrong.
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T=sc.nextInt();
    if((T>=1)&&(T<=10))
        {
        String s[]=new String[T];
        int i;
        for(i=0;i<T;i++)
          {
           s[i]=sc.nextLine();                
        }
        int flag=0;
        for(i=1;i<T;i++)
            {
            for(int j=0;j<s[i].length();j=j+2)
                {
                System.out.print(s[i].charAt(j));
                if((j+2)>(s[i].length()-1))
                {
                    System.out.print(" ");
                    j=-1;
                    flag++;
                }
                if(flag==2)
                    break;
            }
                System.out.println();
           }
    }
}
 
     
    