What URL rewriting rule should I write to retrieve a var in the URL?

The problem is in your rewrite rules function, it should be

function num_rewrite_rule(){
    add_rewrite_rule(
        'test/([0-9]+)/?$',
        'index.php?pagename=test&num=$matches[1]',
        'top'
    );
}
add_action( 'init', 'num_rewrite_rule' );

Reference

add_rewrite_rule

NOTE: Always remember to flush your permalinks by visiting **Settings – Permalinks **