#include <queue>
#include <string>
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
const int N = 5005;
bool vis[N];
string ans;
int a[17],pre[N],ansAt[N],n,m,c;
int ctoi(char c)
{
return c >= '0' && c <= '9' ? c - '0' : c - 'A' + 10;
}
char itoc(int i)
{
return i <= 9 ? i + '0' : i + 'A' - 10;
}
bool bfs()
{
queue<int> q;
q.push(0);
while(!q.empty())
{
int cur = q.front();q.pop();
for(int i=0;i<m;i++)
{
int nxt = (cur * c + a[i]) % n;
if(cur == 0 && a[i] == 0 || vis[nxt])
continue;
vis[nxt] = true;
ansAt[nxt] = a[i];
pre[nxt] = cur;
if(nxt == 0)
return true;
q.push(nxt);
}
}
return false;
}
bool check()
{
ans = "";
int p = 0;
do
{
ans += itoc(ansAt[p]);
p = pre[p];
}while(p);
return (int)ans.size() <= 500;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(vis,false,sizeof(vis));
scanf("%d%d%d",&n,&c,&m);
for(int i=0;i<m;i++)
{
char num[3];
scanf("%s",num); //以免存入空格
a[i] = ctoi(num[0]);
}
sort(a,a+m);
if(n == 0)
{
puts(a[0] == 0 ? "0" : "So Sorry.");
continue;
}
if(bfs() && check())
{
for(int i=(int)ans.size()-1;i>=0;i--)
putchar(ans[i]);
puts("");
}
else
puts("So Sorry.");
}
return 0;
}