#include <cassert>
#include <iostream>
#include <vector>
#include "lp_lib.h" // The lp_solve header file
using namespace std;

bool over_pocet(int t, int l, const vector<int> &tyce) {
    int n = tyce.size();
    cerr << "over_pocet: " << t << " tyci dlzky " << l << " na " << n << " casti" << endl;

    lprec *lp = make_lp(0, t*n); // Create a new LP problem with 0 rows (constraints) and t*n columns (variables)
    assert(lp != NULL);
    for (int j=0; j<t; ++j) for (int i=0; i<n; ++i) { char name[30]; snprintf(name,29,"x_%d_%d",j,i); set_col_name(lp, n*j+i+1, name ); }

    // define the objective function: just zeros
    vector<REAL> objective_coefs(t*n+1, 0);
    set_obj_fn( lp, objective_coefs.data() );
    set_maxim(lp);

    // set all variables to binary
    for (int i=0; i<t*n; ++i) set_binary(lp, i+1, TRUE);

    // add the constraints
    for (int i=0; i<n; ++i) {
        vector<REAL> constraint(t*n+1, 0);
        for (int j=0; j<t; ++j) constraint[n*j+i+1] = 1;
        add_constraint(lp, constraint.data(), EQ, 1);
    }
    for (int j=0; j<t; ++j) {
        vector<REAL> constraint(t*n+1, 0);
        for (int i=0; i<n; ++i) constraint[n*j+i+1] = tyce[i];
        add_constraint(lp, constraint.data(), LE, l);
    }

    // debug: print
    // print_lp(lp);

    // optimize
    set_verbose(lp, IMPORTANT);
    set_epsb(lp, 1e-11);
    set_epsint(lp, 1e-10);
    // set_scaling(lp, SCALE_NONE);
    // set_presolve(lp, PRESOLVE_ROWS | PRESOLVE_COLS | PRESOLVE_LINDEP, get_presolveloops(lp));
    int result_code = solve(lp);
    if (result_code != OPTIMAL) { delete_lp(lp); return false; }

    cout << t << endl << t << endl;

    // get the primal solution (pre-presolving)
    int n_rows = get_Norig_rows(lp), n_cols = get_Norig_columns(lp);
    cerr << "n_rows " << n_rows << " n_cols " << n_cols << endl;

    vector<REAL> primal_sol(1 + n_rows + n_cols);
    get_primal_solution(lp, primal_sol.data());
    vector<REAL> var_values(n_cols);
    for (int i=0; i<n_cols; ++i) var_values[i] = primal_sol[1+n_rows+i];

    cerr << "vars: ";
    for (int i=0; i<n*t; ++i) cerr << static_cast<int>(var_values[i] + .1) << " ";
    cerr << endl;
    
    for (int j=0; j<t; ++j) {
        cout << 1;
        for (int i=0; i<n; ++i) {
            int x = static_cast<int>(var_values[n*j+i] + .1);
            if (x) cout << " " << tyce[i];
        }
        cout << endl;
    }
    delete_lp(lp);
    return true;
}

void solve_one() {
    int l, n;
    cin >> l >> n;
    vector<int> dlzky(n), pocty(n);
    for (int i=0; i<n; ++i) cin >> dlzky[i];
    for (int i=0; i<n; ++i) cin >> pocty[i];
    long long celkova_dlzka = 0;
    for (int i=0; i<n; ++i) celkova_dlzka += 1LL * dlzky[i] * pocty[i];
    int t = (celkova_dlzka + l - 1) / l;
    cerr << "l = " << l << " celkova_dlzka = " << celkova_dlzka << " min_t = " << t << endl;

    vector<int> tyce;
    for (int i=0; i<n; ++i) for (int j=0; j<pocty[i]; ++j) tyce.push_back( dlzky[i] );

    while (!over_pocet(t, l, tyce)) {
        cerr << t << " failed" << endl;
        ++t;
    }
}

int main() {
    int tc;
    cin >> tc;
    while (tc--) solve_one();
}
