Longest Collatz sequence using dynamic programming in Java
You memoized once, but you needed to do it twice

Suppose you started with the entire countarray filled in for free.  Your program would still time out because you have \$10^4\$ test cases, and for each test case, you are searching up to \$5*10^6\$ array entries for a total of \$5*10^{10}\$ operations.  In other words, your program runs in \$O(T*N)\$ time, which is too slow.

Think about what happens when the first test case is 5000000 and the second test case is 4999999.  When you found the answer for the first test case, you should have also found the answer for the second test case.  But you 
didn't memoize that anywhere.  If you just did a pass where you found the answer for each n once and saved the answers in a second memoization array, then each test case would take \$O(1)\$ time.  Then your total time would be \$O(T + N)\$, which is \$O(N)\$ to build the answer array and \$O(T)\$ to handle \$T\$ test cases.

Be careful of overflow

When computing the collatz sequence, it is easy to overflow a 32-bit integer.  I would recommend using a long instead of an int.  You might be overflowing and not knowing it because the if (temp &lt;= 1) check may be cutting the sequence short.