Challenge problem

Can anyone tell me how to optimize my code?
I have passed two test cases but for third one it is saying time limit exceeded.
My code is:

import java.util.*;

public class Main {

public static void main(String args[]) {
		Scanner scn = new Scanner(System.in);
	int n = scn.nextInt();
	if (n <= 1000) {
		for (int i = 0; i < n; i++) {
			int a = scn.nextInt();
			int b = scn.nextInt();
			if (a <= 1000000 && b <= 1000000) {
				country_visit(a, b);
			}
		}
	}
}
private static void country_visit(int n, int m) {
	int i,j,countp=0,count;
	for(i=n;i<=m;i++) {
		//countp=0;
		count=0;
		for(j=1;j<=i;j++) {
			if(i%j==0) {
				count++;
			}
		}
		if(count==2) {
			countp++;
		}
	}
	System.out.println(countp);
}

}

Hey @VinayakSingh11111
apply sieve of eratosthenes