Pre - calculate the frequency array
freq [ MAX ] [ n ] = { 0 } ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) cout << firstNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) << endl ; return 0 ; }