How do I retain the query string after login?
I think, my suggestion is :-


alter login form
add a hidden field for destination URL.
add submit handler  like $form['#submit'][] = 'destination_redirect_handler'
use drupal_goto('destination_redirect_url') in this destination_redirect_handler