#include <bits/stdc++.h>
using namespace std;
int dx[4] = {-1, 0, 0, 1};
int dy[4] = {0, -1, 1, 0};
bool sortbysec(const pair<int, int> &a, const pair<int, int> &b) {
  if (a.first == b.first) return a.second < b.second;
  return (a.first < b.first);
}
long long a[10000005];
void sieve() {
  for (int i = 1; i < 10000005; i++) {
    for (int j = i; j <= 10000005; j += i) {
      a[j]++;
    }
  }
}
long long n, w, h;
bool g(long long x) { return (x / w) * (x / h) >= n; }
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long test;
  cin >> test;
  while (test) {
    int n, x, y;
    string s, w = "";
    cin >> s >> x;
    n = s.length();
    for (int i = 0; i < n; i++) {
      w += "1";
    }
    for (int 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';
      }
    }
    bool flag = true;
    for (int i = 0; i < n; i++) {
      if (s[i] == '1') {
        if ((i + x >= n || w[i + x] == '0') && (i - x < 0 || w[i - x] == '0')) {
          flag = false;
        }
      }
    }
    if (!flag)
      cout << "-1" << endl;
    else
      cout << w << endl;
    test--;
  }
}
