fix bug #6 for key prefix detection in qual case

Also, add regression tests for qual cases.