How to group posts by months and add pagination?

Try this code

                                            <?php

                                        $catnam = '1';

                                        $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                                        // the total of registers to show on each page
                                        $showp = 4;

                                        wp_get_archives("type=monthly&showposts=$showp&paged=$paged");     


                                        while (have_posts() ) : the_post();?>

                                         <h4><a href="https://wordpress.stackexchange.com/questions/131770/<?php echo get_permalink(); ?>"><?php the_title();?></a></h4>

                                         <?php endwhile;?>

                                         <?php
                                            global $wp_rewrite;
                                            $paginate_base = get_pagenum_link(1);
                                            if (strpos($paginate_base, '?') || ! $wp_rewrite->using_permalinks()) {
                                                $paginate_format="";
                                                $paginate_base = add_query_arg('paged', '%#%');
                                            } else {
                                                $paginate_format = (substr($paginate_base, -1 ,1) == "https://wordpress.stackexchange.com/" ? '' : "https://wordpress.stackexchange.com/") .
                                                    user_trailingslashit('page/%#%/', 'paged');;
                                                $paginate_base .= '%_%';
                                            }

                                            echo '<div id="pagination">';
                                            echo paginate_links( array(
                                                'base' => $paginate_base,
                                                'format' => $paginate_format,
                                                'total' => $wp_query->max_num_pages,
                                                'mid_size' => 10,
                                                'current' => ($paged ? $paged : 1),
                                                'type' => '',
                                                'prev_text' => __('<p class="readmore">&laquo; Previous</p>', 'default'),
                                                'next_text' => __('<p class="readmore">Next &raquo;</p>', 'default'),
                                            ));
                                            echo "</div>";
                                        ?>