#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int MAXN = 1e5 + 5;
ll x;
string s;
int ans[MAXN];
void init() {
  ;
  ;
}
void solve(int casei) {
  ll n = s.length();
  s = "#" + s;
  for (int i = 1; i <= n; ++i) {
    ans[i] = -1;
  }
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '0') {
      if (i + x <= n) ans[i + x] = 0;
      if (i - x >= 1) ans[i - x] = 0;
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (s[i] == '1') {
      bool flg = false;
      if (i + x <= n && ans[i + x] != 0) ans[i + x] = 1, flg = true;
      if (i - x >= 1 && ans[i - x] != 0) ans[i - x] = 1, flg = true;
      if (flg == false) {
        cout << "-1\n";
        return;
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (ans[i] == -1) ans[i] = 1;
  }
  for (int i = 1; i <= n; ++i) cout << ans[i];
  cout << endl;
}
int main() {
  int Case = 1;
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> Case;
  for (int casei = 1; casei <= Case; ++casei) {
    cin >> s >> x;
    init();
    solve(casei);
  }
  return 0;
}
