#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(){
ifstream cin("f.txt");
int n,t1,t2;
cin>>n;
string s,abbreviation1,abbreviation2;
for (int i =0 ;i<n;++i)
{
cin>>t1;
for (int j = 0;j<t1;++j)
{
cin>>s;
abbreviation1 += s[0];
}
cin>>t2;
for (int k = 0;k<t2;++k)
{
cin>>s;
abbreviation2 += s[0];
}
if (abbreviation1.compare(abbreviation2) == 0)
cout<<"SAME"<<endl;
else
cout<<"DIFFERENT"<<endl;
abbreviation1 = "";
abbreviation2 = "";
}
return 0;
}