ICM2004 help

import java.io.;
import java.util.
;
class Main {
public static void main(String[] args) {

    FastReader scan=new FastReader();
    int t=scan.nextInt();
    while(t-->0){
        int n=scan.nextInt();
        String s=scan.nextLine();
        StringBuilder sb=new StringBuilder(s);
        int q=scan.nextInt();
        while(q-->0){
            int type=Integer.parseInt(scan.next());
            if(type==1){
                int l=Integer.parseInt(scan.next());
                int r=Integer.parseInt(scan.next());
                String cc=scan.next();
                char c=cc.charAt(0);
                for(int i=l-1;i<r;i++){
                    sb.setCharAt(i,c);
                }
                //System.out.println(sb.toString());
            }else{
                int l=Integer.parseInt(scan.next());
                int r=Integer.parseInt(scan.next());
                long p=Integer.parseInt(scan.next());
                long v=Integer.parseInt(scan.next());
                //System.out.println(l+" "+r+" "+p+" "+v);
                long mod=1000000007;
                for(int i=l-1;i<r;i++){
                    if(sb.charAt(i)=='A'){
                        //System.out.println(p+" "+v);
                        long a=(p-v+mod)%mod;
                        long b=(p+v)%mod;
                        p=a;v=b;
                    }else if(sb.charAt(i)=='B'){
                        long a=(p+v)%mod;
                        long b=(v-p+mod)%mod;
                        p=a;v=b;
                    }
                }
                System.out.println(p+" "+v);
            }
        }
    }   
}

}
class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st==null || !st.hasMoreElements()) {
try {
st=new StringTokenizer(br.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try{
str=br.readLine();
}
catch(IOException e){
e.printStackTrace();
}
return str;
}
}

Time Limit exceeded… but worked fine with test case.

@suman_18733097 any suggestions…

I am sorry, Even I am unable to solve the problem. @ssjgz may help you with Optimisation. Basically, the problem is from a Long Challenge. The Problems from Long Challenge are bit more difficult than the ones in short contests. You may checkout the editorial of any problem you are unable to solve. Usually, link to editorial is mentioned in the Problem details section, at the bottom.