import itertools
import gurobipy as gp
from gurobipy import GRB
import sys

def to_int(x): return int(round(x))

def solve_one():
    l = int(input())
    n = int(input())
    dlzky = [ int(_) for _ in input().split() ]
    pocty = [ int(_) for _ in input().split() ]

    patterns = set( [ tuple() ] )
    for i in range(n):
        new_patterns = set()
        for pat in patterns:
            new_patterns.add(pat)
            pat = list(pat)
            while True:
                pat.append(dlzky[i])
                if pat.count(dlzky[i]) > pocty[i]: break
                if sum(pat) > l: break
                new_patterns.add( tuple(pat) )
        patterns = new_patterns
        print(f'after {dlzky[i]}: {len(patterns)} patterns', file=sys.stderr)

    patterns = list(patterns)
    patterns = [ [ pat.count(d) for d in dlzky ] for pat in patterns ]
    p = len(patterns)
    print(f'final count: {p} patterns', file=sys.stderr)

    celkova_dlzka = sum( d*p for d,p in zip(dlzky, pocty) )
    t = (celkova_dlzka + l - 1) // l
    print(f'celkova_dlzka {celkova_dlzka} lower bound t {t}', file=sys.stderr)

    env = gp.Env(empty=True)
    env.setParam('OutputFlag', 0)
    env.start()
    model = gp.Model('tyce_knapsack', env=env)
    model.setParam('MIPGap', 0.0)
    model.setParam('IntegralityFocus', 1)
    model.setParam('IntFeasTol', 1e-9)
    kolko_vars = model.addVars(p, vtype=GRB.INTEGER, name='kolko')

    for i in range(p): model.addConstr( kolko_vars[i] >= 0 )
    for j in range(n): model.addConstr( gp.quicksum( kolko_vars[i] * patterns[i][j] for i in range(p) if patterns[i][j] ) >= pocty[j] )

    model.setObjective( gp.quicksum(kolko_vars[i] for i in range(p)), GRB.MINIMIZE );
    model.optimize()
    if model.status != GRB.OPTIMAL: return False
    kolko = model.getAttr('Xn', kolko_vars)
    kolko = [ to_int(kolko[i]) for i in range(p) ]

    t = to_int( model.objVal )
    print(t)
    print(sum(x > 0 for x in kolko))
    for i in range(p):
        if kolko[i] == 0: continue
        tyc = [ f'{patterns[i][j]}x{dlzky[j]}' for j in range(n) if patterns[i][j] ]
        print( kolko[i], *tyc )

tc = int( input() )
for test in range(tc): solve_one()
