#include <bits/stdc++.h>
using namespace std;
int n, T, tmp[20];
long long vl, fir, sec, up, ex[20], g[20], bi[20], gi[20];
char str[20][13];
int main() {
  int i, j, s, p, q, t;
  scanf("%d", &t);
  for (int tst = 0; tst < t; tst++) {
    cin >> n >> vl;
    for (i = 0; i < n; i++) scanf("%s", str[i]);
    for (i = 0; i <= n; i++) {
      if (i == 0)
        ex[i] = 1;
      else
        ex[i] = ex[i - 1] * 10LL;
    }
    up = ex[n];
    for (i = 0; i <= n; i++) g[i] = up + 1;
    memset(gi, 0, sizeof(gi));
    g[n] = 0;
    memset(bi, 0, sizeof(bi));
    bi[n] = 0;
    for (i = n - 1; i >= 0; i--) {
      T = 10;
      for (j = 0; j < 10; j++) {
        if (str[i][j] != str[i][0]) break;
      }
      if (j >= 10)
        T = 1;
      else {
        for (j = 0; j < 10; j++) {
          if (str[i][j] != str[i][j % 2]) break;
        }
        if (j >= 10)
          T = 2;
        else {
          for (j = 0; j < 10; j++) {
            if (str[i][j] != str[i][j % 5]) break;
          }
          if (j >= 10) T = 5;
        }
      }
      tmp[i] = T;
      int id = vl / ex[n - 1 - i] % 10, dx = 0, ch;
      for (j = 0; j < 10; j++) {
        if (id == j) continue;
        for (s = 0; s + id < 10 && s + j < 10; s++) {
          if (str[i][id + s] != str[i][s + j]) break;
        }
        if (dx < s) {
          dx = s;
          ch = j;
        } else if (dx == s && ch > j)
          ch = j;
      }
      if (dx > 0) {
        gi[i] = (id + dx) * ex[n - 1 - i];
        if ((tmp[i + 1] < 10 || (dx == 1))) {
          for (j = id + 1; j <= id + dx; j++) {
            if (str[i][j % 10] != str[i][id]) break;
          }
          if (j > id + dx) gi[i] += max(0LL, bi[i + 1] - ex[n - i - 1]);
        }
        gi[i] = min(gi[i], up);
      } else {
        gi[i] = gi[i + 1] + id * ex[n - 1 - i];
        gi[i] = min(gi[i], (vl / ex[n - 1 - i] % 10 + 1) * ex[n - 1 - i]);
      }
      dx = 0;
      for (j = 0; j < 10; j++) {
        if (id == j) continue;
        for (s = 0; s + id < 10; s++) {
          if (str[i][id + s] != str[i][(j + s) % 10]) break;
        }
        if (dx < s) dx = s;
      }
      if (dx > 0) {
        g[i] = ((id + dx)) * ex[n - 1 - i];
        if ((tmp[i + 1] < 10 || (dx == 1))) {
          for (j = id + 1; j <= id + dx; j++) {
            if (str[i][j % 10] != str[i][id]) break;
          }
          if (j > id + dx) g[i] += max(0LL, bi[i + 1] - ex[n - i - 1]);
        }
        g[i] = min(g[i], up);
      } else {
        g[i] = gi[i + 1] + id * ex[n - 1 - i];
        g[i] = min(g[i], (vl / ex[n - 1 - i] % 10 + 1) * ex[n - 1 - i]);
      }
      dx = 0;
      for (j = 0; j < 10; j++) {
        if (id == j) continue;
        for (s = 0; s + id < 20 && j + s < 20; s++) {
          if (str[i][(id + s) % 10] != str[i][(j + s) % 10]) break;
        }
        if ((id + s >= 10) && dx < s) dx = s;
      }
      if (dx > 0) {
        bi[i] = (id + dx) * ex[n - 1 - i];
        if ((tmp[i + 1] < 10 || dx == 1)) {
          for (j = id + 1; j <= id + dx; j++) {
            if (str[i][j % 10] != str[i][id]) break;
          }
          if (j > id + dx) bi[i] += max(0LL, bi[i + 1] - ex[n - 1 - i]);
        }
        bi[i] = min(bi[i], up);
      }
    }
    cout << gi[0] - vl << endl;
  }
  return 0;
}
