when the incoming url is a query, in which function does WP begin to work with it?

Any url that doesn’t point to a file/directory that actually exists is redirected (by .htaccess) to the index.php in the root of your WordPress install.

index.php is what actually loads WordPress and its settings etc. It then calls the function wp();, which is responsible for initialising WordPress’ actually handling of the request.

wp() is little more than a wrapper for the wp-class class. In particular, the main() method:

function main($query_args="") {
    $this->init();
    $this->parse_request($query_args);
    $this->send_headers();
    $this->query_posts();
    $this->handle_404();
    $this->register_globals();
    do_action_ref_array('wp', array(&$this));
}

The parse_request() method turns the request (handling pretty permalinks, internal rewrites and extra arguments –i.e. interpreting ?attachment=1) into ‘query_vars‘ (variables concerning the query). The query_posts() method then turns that into an actual WP_Query object (the globals $wp_the_query and $wp_query – which are initialised in wp-settings.php).