#include
#include
#define ll long long
using namespace std;
long long a[15],b[15];
long long gcd(long long a,long long b)
{
return b==0?a:gcd(b,a%b);
}
long long ex_gcd(long long a,long long b,long long &x,long long &y)
{
if(b==0)
{
x=1;
y=0;
return a;
}
long long ans;
ans=ex_gcd(b,a%b,x,y);
long long t;
t=x;
x=y;
y=t-(a/b)*y;
return ans;
}
ll china( ll m,ll n) //下标[l,r] 方程x%m=a;
{
for(int i=2; i<=m; i++)
{
ll A = a[1], B = a[i], d, x, y, c = b[i]-b[1];
d=ex_gcd(A,B,x,y);
if(c%d)return 0;
ll mod = a[i]/d;
ll K = ((x*c/d)%mod+mod)%mod;
b[1] = a[1]*K + b[1];
a[1] = a[1]*a[i]/d;
}
b[1]=(b[1]+a[1])%a[1];
if(b[1]>n)return 0;
return (n-b[1])/a[1]+1-(b[1]==0?1:0);//要求正整数 减去0
}
int main()
{
int t;
cin>>t;
while(t--)
{
long long n;
int m;
cin>>n>>m;
for(int i=1; i<=m; i++)
cin>>a[i];
for(int i=1; i<=m; i++)
cin>>b[i];
cout<