没啥好说的……
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<stack>
#include<queue>
#include<fstream>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<cmath>
#define CLR(x) memset(x,0,sizeof(x))
#define ll long long
#define eps 1e-6
#define pow2(x) ((x)*(x))
#define forto(i,n) for(int i=0;i<n;i++)
#define for1to(i,n) for(int i=1;i<=n;i++)
using namespace std;
int a[11],b[11];
int gcd(int a,int b)
{
if (a<b) swap(a,b);
int tp;
while (b)
{
tp=a%b;
a=b;
b=tp;
}
return a;
}
int lcd(int a,int b)
{
return (a*b)/gcd(a,b);
}
int main()
{
cin.sync_with_stdio(false);
#ifndef ONLINE_JUDGE
freopen("test.txt","r",stdin);
#endif
int Total;
cin>>Total;
while (Total--)
{
int n,m;
cin>>n>>m;
forto(i,m)
cin>>a[i];
forto(i,m)
cin>>b[i];
int temp(1);
forto(i,m)
{
temp=lcd(temp,a[i]);
}
int base(0);
for1to(i,temp)
{
if (i>n)
break;
bool flag(false);
forto(j,m)
{
if ((i%a[j])!=b[j])
{
flag=true;
break;
}
}
if (!flag)
{
base=i;
break;
}
}
if (!base)
cout<<0<<endl;
else
cout << 1+ ((n-base)/temp)<<endl;
}
return 0;
}