/*                  RIV.CPP - by Zeljko Svedic (65 lines)                  */

#include <stdio.h>
#include <stdlib.h>

FILE *in=fopen("riv.in","rt"),*out=fopen("riv.out","wt");
int n,a[1002],zbr[1002],cur[2][1002],max,nmax;

void ucitaj(void)
{
	fscanf(in,"%i",&n);

	for(int tm,i=1;i<=n;i++) {
		fscanf(in,"%i%i",&a[i],&tm);
		zbr[i]=a[i]+tm;
		cur[0][i]=cur[1][i]=0;
	}

	a[0]=a[n+1]=zbr[0]=n+1; cur[0][0]=1; cur[1][0]=0;
}

int set(int sto, int gdje, int kada)
{
	if(gdje<0||gdje>n)
		if(gdje==n+1) { fprintf(out,"%i\n",kada); return 1; }else return 0;

	int os=kada%zbr[gdje];
	if(os>=1&&os<=a[gdje]) cur[sto][(nmax=gdje)]=1; else cur[sto][gdje]=0;

	return 0;
}

void rijesi(void)
{
	int br,brmt,brppmt,i,j;

	for(nmax=0,br=0;br<=n;br++)
	{
		brmt=br%2;
		brppmt=(br+1)%2;

		max=nmax; nmax=-1;

		for(i=0;i<=max;i++)
			if(cur[brmt][i])
			{
				for(j=(i-5 > nmax+1) ? i-5 : nmax+1 ;j<=i+5;j++)
					if(set(brppmt,j,br+1)) return;

				cur[brmt][i]=0;
			}
	}

	fprintf(out,"NO\n");
}

void main(void)
{
	int x,br;

	fscanf(in,"%i",&x);
	for(br=0;br<x;br++) { ucitaj(); rijesi(); }

	fclose(in); fclose(out);
}