#include #include #include using namespace std; const int N=50001; struct BIT { int a[N]; int n; void clear(int nn) { n=nn; for (int i=1;i<=n;i++) a[i]=0; } int lb(int i) { return i&-i; } void set(int i,int x) { for (;i<=n;i+=lb(i)) a[i]+=x; } int get(int i) { int ans=0; for (;i>0;i-=lb(i)) ans+=a[i]; return ans; } }; int a[N]; pair b[N]; BIT c; int n,m; long long gcd(long long a,long long b) { while (b) { long long c=a%b; a=b; b=c; } return a; } int main() { int t,i,x,y,j; scanf("%d",&t); while (t--) { scanf("%d%d",&n,&m); for (i=0;i