#include <bits/stdc++.h>
using namespace std;
const int mod = 1000000007;
const int LIM = 100005;
long long power(long long x, long long n) {
  long long res = 1;
  while (n) {
    if (n & 1) {
      res = res * x % mod;
    }
    x = x * x % mod;
    n >>= 1;
  }
  return (res % mod);
}
int32_t main() {
  long long tt = 1, n, x, k, y, z, i, j;
  cin >> tt;
  while (tt--) {
    string s, w = "", second = "";
    cin >> s;
    cin >> x;
    n = s.length();
    for (i = 0; i < n; i++) {
      w += "1";
      second += "0";
    }
    for (i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i + x < n) w[i + x] = '0';
        if (i - x >= 0) w[i - x] = '0';
      }
    }
    long long f = 0;
    for (i = 0; i < n; i++) {
      if (w[i] == '1') {
        if (i + x < n) second[i + x] = '1';
        if (i - x >= 0) second[i - x] = '1';
      }
    }
    if (second == s)
      cout << w << '\n';
    else
      cout << -1 << '\n';
  }
}
