Pre - calculate the frequency array
freq = new int [ MAX , n ] ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { Console . WriteLine ( lastNonRepeating ( str , n , queries [ i , 0 ] , queries [ i , 1 ] ) ) ; } } }