/* package whatever; // don't place package name! */

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

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
	public static void main (String[] args) throws java.lang.Exception
	{
	    List<List<String>> combList = new ArrayList<>();
	    combList.add(Arrays.asList("a1", "a2", "a3"));
	    combList.add(Arrays.asList("b1", "b2"));
	    combList.add(Arrays.asList());
	    combList.add(Arrays.asList("c1", "c2", "c3"));
	
	    combinations(combList, 0, 0, "");
	}
	
	public static void combinations(List<List<String>> combList, int listIndex, int itemIndex, String result)
	{
	    // Am I at the bottom of the y-axis?
	    if(listIndex < combList.size())
	    {
	        //Am I at the bottom of the x-axis?
	        if(itemIndex < combList.get(listIndex).size())
	        {
	            List<String> curList = combList.get(listIndex);
	            StringBuilder sb = new StringBuilder();
	            sb.append(result).append(curList.get(itemIndex)).append(" ");
	            combinations(combList, listIndex + 1, 0, sb.toString());
	            combinations(combList, listIndex, itemIndex + 1, result);
	        }else if (combList.get(listIndex).isEmpty()){
	            combinations(combList, listIndex + 1, 0, result);
	        }
	        return;
	    }
	    System.out.println(result);
	    //return; - redundant as last instruction of method
	}
}