#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int SIZE = 52, CAP = (1 << 5), INF = 0x7f7f7f7f;
int dx[] = {1, -1, 0, 0};
int dy[] = {0, 0, -1, 1};
struct pos
{
int x, y, s;
} beg, end, tmp, nxt;
int r, c, key;
int status[SIZE][SIZE], blood[SIZE][SIZE][CAP];
char maps[SIZE][SIZE];
int bfs()
{
int ans = INF;
beg.s = 0;
blood[beg.x][beg.y][beg.s] = 0;
queue<pos> q;
q.push(beg);
while (!q.empty())
{
beg = q.front();
q.pop();
for (int i = 0; i < 4; i++)
{
nxt.x = beg.x + dx[i], nxt.y = beg.y + dy[i], nxt.s = beg.s;
if (nxt.x > 0 && nxt.y > 0 && nxt.x <= r && nxt.y <= c && maps[nxt.x][nxt.y] != '#')
{
if (maps[nxt.x][nxt.y] >= 'A' && maps[nxt.x][nxt.y] <= 'E')
if (!((1 << (maps[nxt.x][nxt.y] - 'A')) & nxt.s))
continue;
nxt.s |= status[nxt.x][nxt.y];
int delt = 0;
if (maps[nxt.x][nxt.y] >= '1' && maps[nxt.x][nxt.y] <= '9')
delt = maps[nxt.x][nxt.y] - '0';
if (maps[nxt.x][nxt.y] >= 'A' && maps[nxt.x][nxt.y] <= 'E')
{
if (!((1 << (maps[nxt.x][nxt.y] - 'A')) & nxt.s))
continue;
if (blood[nxt.x][nxt.y][nxt.s] > blood[beg.x][beg.y][beg.s] + delt)
{
blood[nxt.x][nxt.y][nxt.s] = blood[beg.x][beg.y][beg.s] + delt;
q.push(nxt);
}
}
else if (blood[nxt.x][nxt.y][nxt.s] > blood[beg.x][beg.y][beg.s] + delt)
{
blood[nxt.x][nxt.y][nxt.s] = blood[beg.x][beg.y][beg.s] + delt;
q.push(nxt);
}
if (maps[nxt.x][nxt.y] == 'T')
ans = min(ans, blood[nxt.x][nxt.y][nxt.s]);
}
}
}
if (ans != INF)
return ans;
return -1;
}
int main()
{
int txt, x, y;
scanf("%d", &txt);
while (txt--)
{
scanf("%d %d %d", &r, &c, &key);
memset(status, 0, sizeof (status));
memset(blood, 0x7f, sizeof (blood));
for (int i = 1; i <= r; i++)
scanf("%s", maps[i] + 1);
for (int i = 0; i < key; i++)
{
scanf("%d %d", &x, &y);
status[x][y] |= (1 << i);
}
for (int i = 1; i <= r; i++)
for (int j = 1; j <= c; j++)
if (maps[i][j] == 'S')
beg.x = i, beg.y = j;
printf("%d\n", bfs());
}
return 0;
}