#include <bits/stdc++.h>
using namespace std;
priority_queue<pair<long long, long long>, vector<pair<long long, long long> >,
               greater<pair<long long, long long> > >
    pq;
priority_queue<long long, vector<long long>, greater<long long> > pq1;
long long result(long long a, long long b, long long p) {
  long long ans = 1;
  if (b == (-1)) b = p - 2;
  while (b) {
    if (b & 1) {
      ans = (ans * a) % p;
    }
    a = (a * a) % p;
    b >>= 1;
  }
  return ans;
}
long long T = 1, n, m, k;
int main() {
  if (fopen("input.txt", "r"))
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> T;
  while (T--) {
    string s, w;
    cin >> s;
    w = s;
    long long x;
    cin >> x;
    n = s.size();
    for (int i = 0; i < n; i++) w[i] = '1';
    for (int i = 0; i < n; i++) {
      if (s[i] == '0') {
        if (i - x >= 0) w[i - x] = '0';
        if (i + x < n) w[i + x] = '0';
      }
    }
    string p = "";
    for (int i = 0; i < n; i++) {
      if ((i - x >= 0 && w[i - x] == '1') || (i + x < n && w[i + x] == '1')) {
        p.push_back('1');
      } else {
        p.push_back('0');
      }
    }
    if (p == s) {
      cout << w << "\n";
    } else {
      cout << "-1\n";
    }
  }
  return 0;
}
