import java.util.* ;
import java.io.* ;
import java.math.* ;
class smack {
static long mod= pow( 10 ,9 ) + 7 ;
static class Query implements Comparable< Query>
{
private int l,r,in,size,k;
public Query( int l, int r,int k,int in)
{
this .l = l;
this .r = r;
this .in = in;
this .k = k;
this .size = l/ block;
}
@Override
public int compareTo( Query o)
{
// TODO Auto-generated method stub
if ( this .size != o.size )
return this .size - o.size ;
else
return this .r - o.r ;
}
}
static int block,freq[ ] ,above[ ] ;
void solve( )
{
int t= 1 ;
while ( t--!= 0 )
{
int n= ni( ) ;
int a[ ] = na( n) ;
int q= ni( ) ;
int temp[ ] = a.clone ( ) ;
int count= 0 ;
hm.put ( temp[ 0 ] ,++ count) ;
for ( int i= 1 ; i< n; i++ )
{
if ( temp[ i] != temp[ i- 1 ] )
hm.put ( temp[ i] ,++ count) ;
}
for ( int i= 0 ; i< n; i++ )
a[ i] = hm.get ( a[ i] ) ;
freq= new int [ n+ 1 ] ;
above= new int [ n+ 1 ] ;
Query p[ ] = new Query[ q] ;
for ( int i= 0 ; i< q; i++ )
p[ i] = new Query( ni( ) - 1 ,ni( ) - 1 ,1 ,i) ;
int ans[ ] = new int [ q] ;
int curl= 0 ,curr=- 1 ;
for ( int i= 0 ; i< q; i++ )
{
int l= p[ i] .l ;
int r= p[ i] .r ;
while ( curr> r)
{
delete( a[ curr] ) ;
curr--;
}
while ( curr< r)
{
curr++;
add( a[ curr] ) ;
}
while ( curl< l)
{
delete( a[ curl] ) ;
curl++;
}
while ( curl> l)
{
curl--;
add( a[ curl] ) ;
}
ans[ p[ i] .in ] = above[ 0 ] - above[ p[ i] .k ] ;
}
for ( int i= 0 ; i< q; i++ )
out.println ( ans[ i] ) ;
}
}
public static void delete( int n)
{
if ( freq[ n] != 0 ) {
freq[ n] --;
above[ freq[ n] ] --;
}
}
public static void add( int n)
{
above[ freq[ n] ] ++;
freq[ n] ++;
}
static long d, x, y;
void extendedEuclid( long A, long B) {
if ( B == 0 ) {
d = A;
x = 1 ;
y = 0 ;
}
else {
extendedEuclid( B, A% B) ;
long temp = x;
x = y;
y = temp - ( A/ B) * y;
}
}
long modInverse( long A,long M) //A and M are coprime
{
extendedEuclid( A,M) ;
return ( x% M+ M) % M; //x may be negative
}
public static void mergeSort( int [ ] arr, int l ,int r) {
if ( ( r- l) >= 1 ) {
int mid = ( l+ r) / 2 ;
mergeSort( arr,l,mid) ;
mergeSort( arr,mid+ 1 ,r) ;
merge( arr,l,r,mid) ;
}
}
public static void merge( int arr[ ] , int l, int r, int mid) {
int n1 = ( mid- l+ 1 ) , n2 = ( r- mid) ;
int left[ ] = new int [ n1] ;
int right[ ] = new int [ n2] ;
for ( int i = 0 ; i< n1; i++ ) left[ i] = arr[ l+ i] ;
for ( int i = 0 ; i< n2; i++ ) right[ i] = arr[ mid+ 1 + i] ;
int i = 0 , j = 0 , k = l;
while ( i< n1 && j< n2) {
if ( left[ i] > right[ j] ) {
arr[ k++ ] = right[ j++ ] ;
}
else {
arr[ k++ ] = left[ i++ ] ;
}
}
while ( i< n1) arr[ k++ ] = left[ i++ ] ;
while ( j< n2) arr[ k++ ] = right[ j++ ] ;
}
public static void mergeSort( long [ ] arr, int l ,int r) {
if ( ( r- l) >= 1 ) {
int mid = ( l+ r) / 2 ;
mergeSort( arr,l,mid) ;
mergeSort( arr,mid+ 1 ,r) ;
merge( arr,l,r,mid) ;
}
}
public static void merge( long arr[ ] , int l, int r, int mid) {
int n1 = ( mid- l+ 1 ) , n2 = ( r- mid) ;
long left[ ] = new long [ n1] ;
long right[ ] = new long [ n2] ;
for ( int i = 0 ; i< n1; i++ ) left[ i] = arr[ l+ i] ;
for ( int i = 0 ; i< n2; i++ ) right[ i] = arr[ mid+ 1 + i] ;
int i = 0 , j = 0 , k = l;
while ( i< n1 && j< n2) {
if ( left[ i] > right[ j] ) {
arr[ k++ ] = right[ j++ ] ;
}
else {
arr[ k++ ] = left[ i++ ] ;
}
}
while ( i< n1) arr[ k++ ] = left[ i++ ] ;
while ( j< n2) arr[ k++ ] = right[ j++ ] ;
}
static class Pair implements Comparable< Pair> {
int x,y,k,i;
Pair ( int x,int y) {
this .x = x;
this .y = y;
}
public int compareTo( Pair o) {
return this .x - o.x ;
}
public boolean equals
( Object o
) { if ( o instanceof Pair) {
Pair p = ( Pair) o;
return p.x == x && p.y == y && p.k == k;
}
return false ;
}
@Override
return "(" + x + " " + y + " " + k+ " " + i+ " )" ;
}
}
public static boolean isPal
( String s
) { for ( int i= 0 , j= s.length ( ) - 1 ; i<= j; i++ ,j-- ) {
if ( s.charAt ( i) != s.charAt ( j) ) return false ;
}
return true ;
}
StringBuilder sb= new StringBuilder( s) ;
sb.reverse ( ) ;
return sb.toString ( ) ;
}
public static long gcd( long x,long y) {
if ( x% y== 0 )
return y;
else
return gcd( y,x% y) ;
}
public static int gcd( int x,int y) {
if ( x% y== 0 )
return y;
else
return gcd( y,x% y) ;
}
public static long gcdExtended( long a,long b,long [ ] x) {
if ( a== 0 ) {
x[ 0 ] = 0 ;
x[ 1 ] = 1 ;
return b;
}
long [ ] y= new long [ 2 ] ;
long gcd= gcdExtended( b% a, a, y) ;
x[ 0 ] = y[ 1 ] - ( b/ a) * y[ 0 ] ;
x[ 1 ] = y[ 0 ] ;
return gcd;
}
public static int abs( int a,int b) {
return ( int ) Math .
abs ( a
- b
) ; }
public static long abs( long a,long b) {
return ( long ) Math .
abs ( a
- b
) ; }
public static int max( int a,int b) {
if ( a> b)
return a;
else
return b;
}
public static int min( int a,int b) {
if ( a> b)
return b;
else
return a;
}
public static long max( long a,long b) {
if ( a> b)
return a;
else
return b;
}
public static long min( long a,long b) {
if ( a> b)
return b;
else
return a;
}
public static long pow( long n,long p,long m) {
long result = 1 ;
if ( p== 0 )
return 1 ;
if ( p== 1 )
return n;
while ( p!= 0 )
{
if ( p% 2== 1 )
result *= n;
if ( result>= m)
result%= m;
p >>= 1 ;
n*= n;
if ( n>= m)
n%= m;
}
return result;
}
public static long pow( long n,long p) {
long result = 1 ;
if ( p== 0 )
return 1 ;
if ( p== 1 )
return n;
while ( p!= 0 )
{
if ( p% 2== 1 )
result *= n;
p >>= 1 ;
n*= n;
}
return result;
}
public static void debug
( Object ...
o ) { }
solve( ) ;
out.flush ( ) ;
}
public void run( ) {
try {
new smack( ) .run ( ) ;
e.printStackTrace ( ) ;
}
}
} , "1" , 1 << 26 ) .start ( ) ;
}
private byte [ ] inbuf = new byte [ 1024 ] ;
public int lenbuf = 0 , ptrbuf = 0 ;
private int readByte( ) {
if ( lenbuf == - 1 )
throw new InputMismatchException( ) ;
if ( ptrbuf >= lenbuf) {
ptrbuf = 0 ;
try {
lenbuf = is.read ( inbuf) ;
throw new InputMismatchException( ) ;
}
if ( lenbuf <= 0 )
return - 1 ;
}
return inbuf[ ptrbuf++ ] ;
}
private boolean isSpaceChar( int c) {
return ! ( c >= 33 && c <= 126 ) ;
}
private int skip( ) {
int b;
while ( ( b = readByte( ) ) != - 1 && isSpaceChar( b) ) ;
return b;
}
private double nd( ) {
return Double .
parseDouble ( ns
( ) ) ; }
private char nc( ) {
return ( char ) skip( ) ;
}
int b = skip( ) ;
StringBuilder sb = new StringBuilder( ) ;
while ( ! ( isSpaceChar( b) ) ) { // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint ( b) ;
b = readByte( ) ;
}
return sb.toString ( ) ;
}
private char [ ] ns( int n) {
char [ ] buf = new char [ n] ;
int b = skip( ) , p = 0 ;
while ( p < n && ! ( isSpaceChar( b) ) ) {
buf[ p++ ] = ( char ) b;
b = readByte( ) ;
}
return n
== p
? buf
: Arrays .
copyOf ( buf, p
) ; }
private char [ ] [ ] nm( int n, int m) {
char [ ] [ ] map = new char [ n] [ ] ;
for ( int i = 0 ; i < n; i++ )
map[ i] = ns( m) ;
return map;
}
private int [ ] na( int n) {
int [ ] a = new int [ n] ;
for ( int i = 0 ; i < n; i++ )
a[ i] = ni( ) ;
return a;
}
private int ni( ) {
int num = 0 , b;
boolean minus = false ;
while ( ( b = readByte( ) ) != - 1 && ! ( ( b >= '0' && b <= '9' ) || b == '-' ) )
;
if ( b == '-' ) {
minus = true ;
b = readByte( ) ;
}
while ( true ) {
if ( b >= '0' && b <= '9' ) {
num = num * 10 + ( b - '0' ) ;
} else {
return minus ? - num : num;
}
b = readByte( ) ;
}
}
private long nl( ) {
long num = 0 ;
int b;
boolean minus = false ;
while ( ( b = readByte( ) ) != - 1 && ! ( ( b >= '0' && b <= '9' ) || b == '-' ) )
;
if ( b == '-' ) {
minus = true ;
b = readByte( ) ;
}
while ( true ) {
if ( b >= '0' && b <= '9' ) {
num = num * 10 + ( b - '0' ) ;
} else {
return minus ? - num : num;
}
b = readByte( ) ;
}
}
}
ICAgIGltcG9ydCBqYXZhLnV0aWwuKjsKICAgIGltcG9ydCBqYXZhLmlvLio7CiAgICBpbXBvcnQgamF2YS5tYXRoLio7CiAgICBjbGFzcyBzbWFjayB7CiAgICAJSW5wdXRTdHJlYW0gaXM7CiAgICAJUHJpbnRXcml0ZXIgb3V0OwogICAgCXN0YXRpYyBsb25nIG1vZD1wb3coMTAsOSkrNzsKICAgIAlzdGF0aWMgY2xhc3MgUXVlcnkgaW1wbGVtZW50cyBDb21wYXJhYmxlPFF1ZXJ5PgogICAgCXsKICAgIAkJcHJpdmF0ZSBpbnQgbCxyLGluLHNpemUsazsJCiAgICAJCXB1YmxpYyBRdWVyeShpbnQgbCwgaW50IHIsaW50IGssaW50IGluKSAKICAgIAkJeyAKICAgIAkJCXRoaXMubCA9IGw7CiAgICAJCQl0aGlzLnIgPSByOwogICAgCQkJdGhpcy5pbj1pbjsKICAgIAkJCXRoaXMuaz1rOwogICAgCQkJdGhpcy5zaXplPWwvYmxvY2s7CiAgICAJCX0KICAgIAkJQE92ZXJyaWRlCiAgICAJCXB1YmxpYyBpbnQgY29tcGFyZVRvKFF1ZXJ5IG8pIAogICAgCQl7CiAgICAJCQkvLyBUT0RPIEF1dG8tZ2VuZXJhdGVkIG1ldGhvZCBzdHViCiAgICAJCQlpZih0aGlzLnNpemUhPW8uc2l6ZSkK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RXVjbGlkKGxvbmcgQSwgbG9uZyBCKSB7CiAgICAJICAgIGlmKEIgPT0gMCkgewogICAgCSAgICAgICAgZCA9IEE7CiAgICAJICAgICAgICB4ID0gMTsKICAgIAkgICAgICAgIHkgPSAwOwogICAgCSAgICB9CiAgICAJICAgIGVsc2UgewogICAgCSAgICAgICAgZXh0ZW5kZWRFdWNsaWQoQiwgQSVCKTsKICAgIAkgICAgICAgIGxvbmcgdGVtcCA9IHg7CiAgICAJICAgICAgICB4ID0geTsKICAgIAkgICAgICAgIHkgPSB0ZW1wIC0gKEEvQikqeTsKICAgIAkgICAgfQogICAgCX0KICAgIAlsb25nIG1vZEludmVyc2UobG9uZyBBLGxvbmcgTSkgLy9BIGFuZCBNIGFyZSBjb3ByaW1lCiAgICAJewogICAgCSAgICBleHRlbmRlZEV1Y2xpZChBLE0pOwogICAgCSAgICByZXR1cm4gKHglTStNKSVNOyAgICAvL3ggbWF5IGJlIG5lZ2F0aXZlCiAgICAJfQogICAgCXB1YmxpYyBzdGF0aWMgdm9pZCBtZXJnZVNvcnQoaW50W10gYXJyLCBpbnQgbCAsaW50IHIpewogICAgCQlpZigoci1sKT49MSl7CiAgICAJCQlpbnQgbWlkID0gKGwrcikvMjsKICAgIAkJCW1lcmdlU29ydChhcnIsbCxtaWQpOwogICAgCQkJbWVyZ2VTb3J0KGFycixtaWQrMSxyKTsKICAgIAkJCW1lcmdlKGFycixsLHIsbWlkKTsKICAgIAkJfQogICAgCX0KICAgIAlwdWJsaWMgc3RhdGljIHZvaWQgbWVyZ2UoaW50IGFycltdLCBpbnQgbCwgaW50IHIsIGludCBtaWQpewogICAgCQlpbnQgbjEgPSAobWlkLWwrMSksIG4yID0gKHItbWlkKTsKICAgIAkJaW50IGxlZnRbXSA9IG5ldyBpbnRbbjFdOwogICAgCQlpbnQgcmlnaHRbXSA9IG5ldyBpbnRbbjJdOwogICAgCQlmb3IoaW50IGkgPTAgO2k8bjE7aSsrKSBsZWZ0W2ldID0gYXJyW2wraV07CiAgICAJCWZvcihpbnQgaSA9MCA7aTxuMjtpKyspIHJpZ2h0W2ldID0gYXJyW21pZCsxK2ldOwogICAgCQlpbnQgaSA9MCwgaiA9MCwgayA9IGw7CiAgICAJCXdoaWxlKGk8bjEgJiYgajxuMil7CiAgICAJCQlpZihsZWZ0W2ldPnJpZ2h0W2pdKXsKICAgIAkJCQlhcnJbaysrXSA9IHJpZ2h0W2orK107CiAgICAJCQl9CiAgICAJCQllbHNlewogICAgCQkJCWFycltrKytdID0gbGVmdFtpKytdOwogICAgCQkJfQogICAgCQl9CiAgICAJCXdoaWxlKGk8bjEpIGFycltrKytdID0gbGVmdFtpKytdOwogICAgCQl3aGlsZShqPG4yKSBhcnJbaysrXSA9IHJpZ2h0W2orK107CiAgICAJfQogICAgCXB1YmxpYyBzdGF0aWMgdm9pZCBtZXJnZVNvcnQobG9uZ1tdIGFyciwgaW50IGwgLGludCByKXsKICAgIAkJaWYoKHItbCk+PTEpewogICAgCQkJaW50IG1pZCA9IChsK3IpLzI7CiAgICAJCQltZXJnZVNvcnQoYXJyLGwsbWlkKTsKICAgIAkJCW1lcmdlU29ydChhcnIsbWlkKzEscik7CiAgICAJCQltZXJnZShhcnIsbCxyLG1pZCk7CiAgICAJCX0KICAgIAl9CiAgICAJcHVibGljIHN0YXRpYyB2b2lkIG1lcmdlKGxvbmcgYXJyW10sIGludCBsLCBpbnQgciwgaW50IG1pZCl7CiAgICAJCWludCBuMSA9IChtaWQtbCsxKSwgbjIgPSAoci1taWQpOwogICAgCQlsb25nIGxlZnRbXSA9IG5ldyBsb25nW24xXTsKICAgIAkJbG9uZyByaWdodFtdID0gbmV3IGxvbmdbbjJdOwogICAgCQlmb3IoaW50IGkgPTAgO2k8bjE7aSsrKSBsZWZ0W2ldID0gYXJyW2wraV07CiAgICAJCWZvcihpbnQgaSA9MCA7aTxuMjtpKyspIHJpZ2h0W2ldID0gYXJyW21pZCsxK2ldOwogICAgCQlpbnQgaSA9MCwgaiA9MCwgayA9IGw7CiAgICAJCXdoaWxlKGk8bjEgJiYgajxuMil7CiAgICAJCQlpZihsZWZ0W2ldPnJpZ2h0W2pdKXsKICAgIAkJCQlhcnJbaysrXSA9IHJpZ2h0W2orK107CiAgICAJCQl9CiAgICAJCQllbHNlewogICAgCQkJCWFycltrKytdID0gbGVmdFtpKytdOwogICAgCQkJfQogICAgCQl9CiAgICAJCXdoaWxlKGk8bjEpIGFycltrKytdID0gbGVmdFtpKytdOwogICAgCQl3aGlsZShqPG4yKSBhcnJbaysrXSA9IHJpZ2h0W2orK107CiAgICAJfQogICAgCSBzdGF0aWMgY2xhc3MgUGFpciBpbXBsZW1lbnRzIENvbXBhcmFibGU8UGFpcj57CiAgICAJCSAKICAgIAkgICAgICAgaW50IHgseSxrLGk7CiAgICAJICAgICAgICAKICAgIAkJUGFpciAoaW50IHgsaW50IHkpewogICAgCQkJdGhpcy54PXg7CiAgICAJCQl0aGlzLnk9eTsKICAgIAkJfQogICAgCSAgICAgICAgCiAgICAJCXB1YmxpYyBpbnQgY29tcGFyZVRvKFBhaXIgbykgewogICAgCQkJCQlyZXR1cm4gdGhpcy54LW8ueDsKICAgIAkJfQogICAgCSAKICAgIAkgICAgICAgIHB1YmxpYyBib29sZWFuIGVxdWFscyhPYmplY3QgbykgewogICAgCSAgICAgICAgICAgIGlmIChvIGluc3RhbmNlb2YgUGFpcikgewogICAgCSAgICAgICAgICAgICAgICBQYWlyIHAgPSAoUGFpcilvOwogICAgCSAgICAgICAgICAgICAgICByZXR1cm4gcC54ID09IHggJiYgcC55ID09IHkgJiYgcC5rPT1rOwogICAgCSAgICAgICAgICAgIH0KICAgIAkgICAgICAgICAgICByZXR1cm4gZmFsc2U7CiAgICAJICAgICAgICB9CiAgICAJICAgICAgICAgICAKICAgIAkgCiAgICAJICAgICAgICBAT3ZlcnJpZGUKICAgIAkgICAgICAgIHB1YmxpYyBTdHJpbmcgdG9TdHJpbmcoKSB7CiAgICAJICAgICAgICAgICAgcmV0dXJuICAiKCIreCArICIgIiArIHkgKyIgIitrKyIgIitpKyIgKSI7CiAgICAJICAgICAgICB9CiAgICAJICAgIAogICAgCSAgICB9IAogICAgCSAgICAKICAgIAkgICAgcHVibGljIHN0YXRpYyBib29sZWFuIGlzUGFsKFN0cmluZyBzKXsKICAgIAkgICAgICAgIGZvcihpbnQgaT0wLCBqPXMubGVuZ3RoKCktMTtpPD1qO2krKyxqLS0pewogICAgCSAgICAgICAgICAgICAgICBpZihzLmNoYXJBdChpKSE9cy5jaGFyQXQoaikpIHJldHVybiBmYWxzZTsKICAgIAkgICAgICAgIH0KICAgIAkgICAgICAgIHJldHVybiB0cnVlOwogICAgCSAgICB9CiAgICAJICAgIHB1YmxpYyBzdGF0aWMgU3RyaW5nIHJldihTdHJpbmcgcyl7CiAgICAJCQlTdHJpbmdCdWlsZGVyIHNiPW5ldyBTdHJpbmdCdWlsZGVyKHMpOwogICAgCQkJc2IucmV2ZXJzZSgpOwogICAgCQkJcmV0dXJuIHNiLnRvU3RyaW5nKCk7CiAgICAJICAgIH0KICAgIAkgICAgCiAgICAJICAgIHB1YmxpYyBzdGF0aWMgbG9uZyBnY2QobG9uZyB4LGxvbmcgeSl7CiAgICAJCWlmKHgleT09MCkKICAgIAkJCXJldHVybiB5OwogICAgCQllbHNlCiAgICAJCQlyZXR1cm4gZ2NkKHkseCV5KTsKICAgIAkgICAgfQogICAgCSAgICAKICAgIAkgICAgcHVibGljIHN0YXRpYyBpbnQgZ2NkKGludCB4LGludCB5KXsKICAgIAkJaWYoeCV5PT0wKQogICAgCQkJcmV0dXJuIHk7CiAgICAJCWVsc2UgCiAgICAJCQlyZXR1cm4gZ2NkKHkseCV5KTsKICAgIAkgICAgfQogICAgCSAgICAKICAgIAkgICAgcHVibGljIHN0YXRpYyBsb25nIGdjZEV4dGVuZGVkKGxvbmcgYSxsb25nIGIsbG9uZ1tdIHgpewogICAgCSAgICAgICAgCiAgICAJICAgICAgICBpZihhPT0wKXsKICAgIAkgICAgICAgICAgICB4WzBdPTA7CiAgICAJICAgICAgICAgICAgeFsxXT0xOwogICAgCSAgICAgICAgICAgIHJldHVybiBiOwogICAgCSAgICAgICAgfQogICAgCSAgICAgICAgbG9uZ1tdIHk9bmV3IGxvbmdbMl07CiAgICAJICAgICAgICBsb25nIGdjZD1nY2RFeHRlbmRlZChiJWEsIGEsIHkpOwogICAgCSAgICAgICAgCiAgICAJICAgICAgICB4WzBdPXlbMV0tKGIvYSkqeVswXTsKICAgIAkgICAgICAgIHhbMV09eVswXTsKICAgIAkgICAgICAgIAogICAgCSAgICAgICAgcmV0dXJuIGdjZDsKICAgIAkgICAgfQogICAgCSAgICAKICAgIAkgICAgcHVibGljIHN0YXRpYyBpbnQgYWJzKGludCBhLGludCBiKXsKICAgIAkJcmV0dXJuIChpbnQpTWF0aC5hYnMoYS1iKTsKICAgIAkgICAgfQogICAgCSAKICAgIAkgICAgcHVibGljIHN0YXRpYyBsb25nIGFicyhsb25nIGEsbG9uZyBiKXsKICAgIAkJcmV0dXJuIChsb25nKU1hdGguYWJzKGEtYik7CiAgICAJICAgIH0KICAgIAkgICAgCiAgICAJICAgIHB1YmxpYyBzdGF0aWMgaW50IG1heChpbnQgYSxpbnQgYil7CiAgICAJCWlmKGE+YikKICAgIAkJCXJldHVybiBhOwogICAgCQllbHNlCiAgICAJCQlyZXR1cm4gYjsKICAgIAkgICAgfQogICAgCSAKICAgIAkgICAgcHVibGljIHN0YXRpYyBpbnQgbWluKGludCBhLGludCBiKXsKICAgIAkJaWYoYT5iKQogICAgCQkJcmV0dXJuIGI7CiAgICAJCWVsc2UgCiAgICAJCQlyZXR1cm4gYTsKICAgIAkgICAgfQogICAgCSAgICAKICAgIAkgICAgcHVibGljIHN0YXRpYyBsb25nIG1heChsb25nIGEsbG9uZyBiKXsKICAgIAkJaWYoYT5iKQogICAgCQkJcmV0dXJuIGE7CiAgICAJCWVsc2UKICAgIAkJCXJldHVybiBiOwogICAgCSAgICB9CiAgICAJIAogICAgCSAgICBwdWJsaWMgc3RhdGljIGxvbmcgbWluKGxvbmcgYSxsb25nIGIpewogICAgCQlpZihhPmIpCiAgICAJCQlyZXR1cm4gYjsKICAgIAkJZWxzZSAKICAgIAkJCXJldHVybiBhOwogICAgCSAgICB9CiAgICAJIAogICAgCSAgICBwdWJsaWMgc3RhdGljIGxvbmcgcG93KGxvbmcgbixsb25nIHAsbG9uZyBtKXsKICAgIAkJIGxvbmcgIHJlc3VsdCA9IDE7CiAgICAJCSAgaWYocD09MCkKICAgIAkJICAgIHJldHVybiAxOwogICAgCQlpZiAocD09MSkKICAgIAkJICAgIHJldHVybiBuOwogICAgCQl3aGlsZShwIT0wKQogICAgCQl7CiAgICAJCSAgICBpZihwJTI9PTEpCiAgICAJCSAgICAgICAgcmVzdWx0ICo9IG47CiAgICAJCSAgICBpZihyZXN1bHQ+PW0pCiAgICAJCSAgICByZXN1bHQlPW07CiAgICAJCSAgICBwID4+PTE7CiAgICAJCSAgICBuKj1uOwogICAgCQkgICAgaWYobj49bSkKICAgIAkJICAgIG4lPW07CiAgICAJCX0KICAgIAkJcmV0dXJuIHJlc3VsdDsKICAgIAkgICAgfQogICAgCSAgICAKICAgIAkgICAgcHVibGljIHN0YXRpYyBsb25nIHBvdyhsb25nIG4sbG9uZyBwKXsKICAgIAkJbG9uZyAgcmVzdWx0ID0gMTsKICAgIAkJICBpZihwPT0wKQogICAgCQkgICAgcmV0dXJuIDE7CiAgICAJCWlmIChwPT0xKQogICAgCQkgICAgcmV0dXJuIG47CiAgICAJCXdoaWxlKHAhPTApCiAgICAJCXsKICAgIAkJICAgIGlmKHAlMj09MSkKICAgIAkJICAgICAgICByZXN1bHQgKj0gbjsJICAgIAogICAgCQkgICAgcCA+Pj0xOwogICAgCQkgICAgbio9bjsJICAgIAogICAgCQl9CiAgICAJCXJldHVybiByZXN1bHQ7CiAgICAJICAgIH0KICAgIAkgICAgcHVibGljIHN0YXRpYyB2b2lkIGRlYnVnKE9iamVjdC4uLiBvKSB7CiAgICAJCQlTeXN0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