1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstdlib>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <unordered_set>
  10. #include <unordered_map>
  11. #include <queue>
  12. #include <ctime>
  13. #include <cassert>
  14. #include <complex>
  15. #include <string>
  16. #include <cstring>
  17. #include <chrono>
  18. #include <random>
  19. #include <bitset>
  20. #include <array>
  21. using namespace std;
  22.  
  23. #ifdef LOCAL
  24. #define eprintf(...) {fprintf(stderr, __VA_ARGS__);fflush(stderr);}
  25. #else
  26. #define eprintf(...) 42
  27. #endif
  28.  
  29. using ll = long long;
  30. using ld = long double;
  31. using uint = unsigned int;
  32. using ull = unsigned long long;
  33. template<typename T>
  34. using pair2 = pair<T, T>;
  35. using pii = pair<int, int>;
  36. using pli = pair<ll, int>;
  37. using pll = pair<ll, ll>;
  38. mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
  39. ll myRand(ll B) {
  40. return (ull)rng() % B;
  41. }
  42.  
  43. #define pb push_back
  44. #define mp make_pair
  45. #define all(x) (x).begin(),(x).end()
  46. #define fi first
  47. #define se second
  48.  
  49. clock_t startTime;
  50. double getCurrentTime() {
  51. return (double)(clock() - startTime) / CLOCKS_PER_SEC;
  52. }
  53.  
  54. const int N = 2002;
  55. int SUCC = 0;
  56. int n;
  57. pii ord[N];
  58. int d[N];
  59. int dp[N][2 * N + 3];
  60. int par[N][2 * N + 3];
  61. char g[N][N];
  62.  
  63. void printTree() {
  64. int sum = 0;
  65. int X = 0;
  66. for (int i = 0; i < n; i++) {
  67. sum += d[i];
  68. X ^= d[i];
  69. ord[i] = mp(d[i], i);
  70. }
  71. assert(X == 0);
  72. assert(sum >= 2 * n - 2);
  73. assert(sum < 2 * n + 6);
  74. sort(ord, ord + n);
  75. reverse(ord, ord + n);
  76. for (int i = 0; i < n; i++) {
  77. for (int j = 0; j < n; j++)
  78. g[i][j] = '0';
  79. g[i][n] = '\0';
  80. }
  81. int p = 0;
  82. vector<int> a = vector<int>();
  83. if (sum != 2 * n - 2) {
  84. sum = (sum - (2 * n - 2)) / 2;
  85. assert(ord[3].first >= 3);
  86. assert(ord[0].first >= 4);
  87. sum += 3;
  88. for (int i = 0; sum > 0 && i < 4; i++)
  89. for (int j = i + 1; sum > 0 && j < 4; j++) {
  90. int v = ord[i].second, u = ord[j].second;
  91. g[v][u] = g[u][v] = '1';
  92. d[v]--;
  93. d[u]--;
  94. sum--;
  95. }
  96. p = 4;
  97. for (int i = 0; i < 4; i++) {
  98. int v = ord[i].second;
  99. for (int j = 0; j < d[v]; j++)
  100. a.push_back(v);
  101. }
  102. } else {
  103. p = 1;
  104. for (int i = 0; i < 1; i++) {
  105. int v = ord[i].second;
  106. for (int j = 0; j < d[v]; j++)
  107. a.push_back(v);
  108. }
  109. }
  110. for (int i = p; i < n; i++) {
  111. int v = ord[i].second;
  112. assert(!a.empty());
  113. int u = a.back();
  114. a.pop_back();
  115. g[v][u] = g[u][v] = '1';
  116. for (int j = 1; j < d[v]; j++)
  117. a.push_back(v);
  118. }
  119. printf("YES\n");
  120. for (int i = 0; i < n; i++) {
  121. printf("%s\n", g[i]);
  122. }
  123. SUCC++;
  124. }
  125.  
  126. void reduceFour() {
  127. int sum = 0, X = 0;
  128. for (int i = 0; i < n; i++) {
  129. sum += d[i];
  130. X ^= d[i];
  131. }
  132. assert(X == 0);
  133. assert(sum >= 2 * n - 2);
  134. assert(sum < 3 * n);
  135. while(sum >= 2 * n + 6) {
  136. int k = 30;
  137. for (int i = 0; i < n; i++) {
  138. if (d[i] < 8) continue;
  139. int x = (d[i] >> 2) << 2;
  140. while(x & ((1 << k) - 1)) k--;
  141. }
  142. if (k < 30) {
  143. int v = -1, u = -1;
  144. for (int i = 0; i < n; i++) {
  145. if (d[i] < 8) continue;
  146. if (((d[i] >> k) & 1) == 0) continue;
  147. if (v == -1) {
  148. v = i;
  149. } else if (u == -1) {
  150. u = i;
  151. }
  152. }
  153. assert(v != -1);
  154. if (u != -1) {
  155. sum -= 8;
  156. d[v] -= 4;
  157. d[u] -= 4;
  158. continue;
  159. }
  160. assert(k == 2);
  161. u = 0;
  162. while(u < n && (d[u] < 4 || d[u] >= 8)) u++;
  163. assert(u < n);
  164. int x = d[v] ^ d[u] ^ 3;
  165. assert(x < d[v]);
  166. assert((d[v] - x) + (d[u] - 3) <= 8);
  167. sum -= d[v] - x + d[u] - 3;
  168. d[v] = x;
  169. d[u] = 3;
  170. continue;
  171. }
  172. int v = -1, u = -1;
  173. for (int i = 0; i < n; i++) {
  174. if (d[i] < 4) continue;
  175. if ((d[i] & 2) == 0) continue;
  176. if (v == -1) {
  177. v = i;
  178. } else if (u == -1) {
  179. u = i;
  180. }
  181. }
  182. if (u != -1) {
  183. sum -= 4;
  184. d[v] -= 2;
  185. d[u] -= 2;
  186. continue;
  187. }
  188. v = -1; u = -1;
  189. for (int i = 0; i < n; i++) {
  190. if (d[i] < 4) continue;
  191. if ((d[i] & 1) == 0) continue;
  192. if (v == -1) {
  193. v = i;
  194. } else if (u == -1) {
  195. u = i;
  196. }
  197. }
  198. if (u != -1) {
  199. sum -= 2;
  200. d[v] -= 1;
  201. d[u] -= 1;
  202. continue;
  203. }
  204. for (int i = 0; i < n; i++)
  205. ord[i] = mp(d[i], i);
  206. sort(ord, ord + n);
  207. reverse(ord, ord + n);
  208. int p = 0;
  209. while(p < n && ord[p].first >= 4) p++;
  210. assert(p % 2 == 0);
  211. while(sum >= 2 * n + 6 && p > 0) {
  212. p -= 2;
  213. int v = ord[p].second, u = ord[p + 1].second;
  214. int x = d[v] ^ d[u];
  215. sum -= d[v] + d[u];
  216. if (x == 3) {
  217. d[v] = 2;
  218. d[u] = 1;
  219. } else {
  220. d[v] = 3;
  221. d[u] = 3 ^ x;
  222. }
  223. sum += d[v] + d[u];
  224. }
  225. return;
  226. }
  227. }
  228.  
  229. bool checkThree(int a, int b, int c, int ones, int twos) {
  230. int sum = a + b + c + ones * 1 + twos * 2;
  231. if (sum < 2 * n - 2) return false;
  232. assert(sum % 2 == 0);
  233. if (a < b) swap(a, b);
  234. if (b < c) swap(b, c);
  235. if (a < b) swap(a, b);
  236. assert(a > 1);
  237. if (b == 1) {
  238. return true;
  239. }
  240. if (c == 1) {
  241. if (twos > 0) {
  242. c = 2;
  243. ones++;
  244. twos--;
  245. }
  246. }
  247. if (c == 1) {
  248. return sum == 2 * n - 2;
  249. }
  250. return (sum - (2 * n - 2)) / 2 <= 1 + twos;
  251. }
  252.  
  253. void printThree() {
  254. /*
  255. eprintf("print 3\n");
  256. for (int i = 0; i < n; i++)
  257. eprintf("%d ", d[i]);
  258. eprintf("\n");
  259. */
  260. int sum = 0;
  261. int X = 0;
  262. for (int i = 0; i < n; i++) {
  263. sum += d[i];
  264. X ^= d[i];
  265. ord[i] = mp(d[i], i);
  266. }
  267. assert(X == 0);
  268. assert(sum >= 2 * n - 2);
  269. sort(ord, ord + n);
  270. reverse(ord, ord + n);
  271. assert(ord[3].first <= 2);
  272. if (ord[0].first == 2) {
  273. if (sum == 2 * n) {
  274. d[ord[0].second] = d[ord[1].second] = 1;
  275. sum -= 2;
  276. }
  277. }
  278. if (sum == 2 * n - 2) {
  279. printTree();
  280. return;
  281. }
  282. for (int i = 0; i < n; i++) {
  283. for (int j = 0; j < n; j++)
  284. g[i][j] = '0';
  285. g[i][n] = '\0';
  286. }
  287. int A = ord[0].second, B = ord[1].second, C = ord[2].second;
  288. sum = (sum - (2 * n - 2)) / 2;
  289. assert(sum > 0);
  290. assert(d[C] >= 2);
  291. g[A][B] = g[B][A] = g[A][C] = g[C][A] = g[B][C] = g[C][B] = '1';
  292. d[A] -= 2;
  293. d[B] -= 2;
  294. d[C] -= 2;
  295. sum--;
  296. int p = 3;
  297. while(sum > 1) {
  298. assert(p < n);
  299. int v = ord[p++].second;
  300. assert(d[v] == 2);
  301. if (d[A] < d[B]) swap(A, B);
  302. if (d[B] < d[C]) swap(B, C);
  303. if (d[A] < d[B]) swap(A, B);
  304. assert(d[A] + d[B] >= 2);
  305. if (d[B] == 0) {
  306. assert(p < n);
  307. int u = ord[p++].second;
  308. assert(d[u] == 2);
  309. g[A][v] = g[v][A] = g[A][u] = g[u][A] = g[v][u] = g[u][v] = '1';
  310. d[A] -= 2;
  311. d[v] -= 2;
  312. d[u] -= 2;
  313. sum--;
  314. } else {
  315. g[A][v] = g[B][v] = g[v][A] = g[v][B] = '1';
  316. sum--;
  317. d[A]--;
  318. d[B]--;
  319. d[v] -= 2;
  320. }
  321. }
  322. if (d[A] < d[B]) swap(A, B);
  323. if (d[B] < d[C]) swap(B, C);
  324. if (d[A] < d[B]) swap(A, B);
  325. if (p < n && ord[p].first == 2) {
  326. int v = ord[p].second;
  327. d[A]--;
  328. g[A][v] = g[v][A] = '1';
  329. p++;
  330. while(p < n && ord[p].first == 2) {
  331. int u = ord[p++].second;
  332. g[v][u] = g[u][v] = '1';
  333. v = u;
  334. }
  335. if (sum == 0) {
  336. assert(p < n);
  337. int u = ord[p++].second;
  338. g[v][u] = g[u][v] = '1';
  339. } else {
  340. if (d[B] > 0) {
  341. d[B]--;
  342. g[B][v] = g[v][B] = '1';
  343. } else {
  344. assert(d[A] > 0);
  345. d[A]--;
  346. g[A][v] = g[v][A] = '1';
  347. }
  348. }
  349. }
  350. while(p < n) {
  351. int v = ord[p].second;
  352. assert(d[v] == 1);
  353. if (d[B] > 0) swap(A, B);
  354. if (d[C] > 0) swap(A, C);
  355. assert(d[A] > 0);
  356. d[A]--;
  357. g[A][v] = g[v][A] = '1';
  358. p++;
  359. }
  360. printf("YES\n");
  361. for (int i = 0; i < n; i++) {
  362. printf("%s\n", g[i]);
  363. }
  364. SUCC++;
  365. }
  366.  
  367. void solve() {
  368. //eprintf("SOLVE\n");
  369. scanf("%d", &n);
  370. for (int i = 0; i < n; i++) {
  371. scanf("%d", &ord[i].first);
  372. ord[i].second = i;
  373. }
  374. if (n == 2) {
  375. printf("YES\n01\n10\n");
  376. SUCC++;
  377. return;
  378. }
  379. if (n == 3) {
  380. printf("NO\n");
  381. return;
  382. }
  383. sort(ord, ord + n);
  384. reverse(ord, ord + n);
  385. int ones = 0;
  386. while(ones < n - 3 && ord[n - 1 - ones].first == 1) ones++;
  387. int twos = n - 3 - ones;
  388. for (int x = 0; x <= 1 && x <= twos; x++) {
  389. int nones = ones + x, ntwos = twos - x;
  390. int X = (nones & 1) ^ ((ntwos & 1) << 1);
  391. for (int c = 1; c <= ord[2].first; c++)
  392. for (int b = 1; b <= ord[1].first; b++) {
  393. int a = b ^ c ^ X;
  394. if (a == 0 || a > ord[0].first) continue;
  395. if (!checkThree(a, b, c, nones, ntwos)) continue;
  396. d[ord[0].second] = a;
  397. d[ord[1].second] = b;
  398. d[ord[2].second] = c;
  399. for (int i = 3; i < n - nones; i++)
  400. d[ord[i].second] = 2;
  401. for (int i = n - nones; i < n; i++)
  402. d[ord[i].second] = 1;
  403. printThree();
  404. return;
  405. }
  406. }
  407. if (ord[3].first <= 2) {
  408. printf("NO\n");
  409. return;
  410. }
  411. int sum = 0;
  412. for (int i = 0; i < n; i++)
  413. sum += ord[i].first;
  414. if (sum >= 3 * n) {
  415. for (int i = 0; i + 1 < n; i += 2)
  416. ord[i].first = ord[i + 1].first;
  417. if (n & 1) {
  418. ord[n - 1].first = 1;
  419. if (ord[0].first % 2 == 0) {
  420. ord[0].first--;
  421. ord[1].first--;
  422. }
  423. ord[0].first--;
  424. }
  425. sum = 0;
  426. for (int i = 0; i < n; i++)
  427. sum += ord[i].first;
  428. assert(sum >= 2 * n - 2);
  429. assert(sum % 2 == 0);
  430. for (int i = 0; i + 1 < n; i += 2) {
  431. int x = (ord[i].first - 1);
  432. x = min(x, (sum - (2 * n - 2)) / 2);
  433. if ((n & 1) && i == 0) x -= x & 1;
  434. sum -= 2 * x;
  435. ord[i].first -= x;
  436. ord[i + 1].first -= x;
  437. }
  438. assert(sum == 2 * n - 2);
  439. for (int i = 0; i < n; i++)
  440. d[ord[i].second] = ord[i].first;
  441. printTree();
  442. } else {
  443. for (int i = 0; i <= n; i++)
  444. for (int x = 0; x < 2 * ord[0].first; x++)
  445. dp[i][x] = -1;
  446. dp[n][0] = 0;
  447. for (int i = n - 1; i >= 0; i--)
  448. for (int x = 0; x < 2 * ord[i].first; x++) {
  449. if (dp[i + 1][x] == -1) continue;
  450. for (int y = (i <= 3 ? 3 : 1); y <= ord[i].first; y++) {
  451. int z = x ^ y;
  452. int w = dp[i + 1][x] + y;
  453. if (w <= dp[i][z]) continue;
  454. dp[i][z] = w;
  455. par[i][z] = y;
  456. }
  457. }
  458. if (dp[0][0] < 2 * n - 2) {
  459. printf("NO\n");
  460. return;
  461. }
  462. int z = 0;
  463. for (int p = 0; p < n; p++) {
  464. int x = par[p][z];
  465. d[ord[p].second] = x;
  466. z ^= x;
  467. }
  468. reduceFour();
  469. bool all3 = true;
  470. int sum = 0;
  471. for (int i = 0; i < n; i++) {
  472. all3 &= d[i] <= 3;
  473. sum += d[i];
  474. }
  475. assert(sum >= 2 * n - 2);
  476. if (all3) {
  477. sum = (sum - (2 * n - 2)) / 2;
  478. int L3 = -1, L2 = -1;
  479. for (int i = 0; sum > 0 && i < n; i++) {
  480. if (d[i] == 1) continue;
  481. if (d[i] == 2) {
  482. if (L2 == -1) {
  483. L2 = i;
  484. } else {
  485. d[i]--;
  486. d[L2]--;
  487. sum--;
  488. L2 = -1;
  489. }
  490. } else if (d[i] == 3) {
  491. if (L3 == -1) {
  492. L3 = i;
  493. } else {
  494. d[i]--;
  495. d[L3]--;
  496. sum--;
  497. if (sum == 0) break;
  498. d[i]--;
  499. d[L3]--;
  500. sum--;
  501. L3 = -1;
  502. }
  503. } else assert(false);
  504. }
  505. assert(sum == 0);
  506. } else {
  507. assert(sum < 2 * n + 6);
  508. }
  509. printTree();
  510. }
  511. }
  512.  
  513. int main()
  514. {
  515. startTime = clock();
  516. // freopen("input.txt", "r", stdin);
  517. // freopen("output.txt", "w", stdout);
  518.  
  519. int t;
  520. scanf("%d", &t);
  521. while(t--) {
  522. solve();
  523. }
  524. fprintf(stderr, "YES = %d\n", SUCC);
  525.  
  526. return 0;
  527. }